我想将一个字符串转换为int,我不是指ASCII码。

为了快速回顾,我们将一个方程作为字符串传递。我们要把它分解,正确地格式化并解出线性方程。说到这里,我不能把一个字符串转换成int型。

我知道字符串的格式是(-5)或(25)等,所以它肯定是int型。但是我们如何从字符串中提取它呢?

我想到的一种方法是在字符串中运行一个for/while循环,检查一个数字,提取后面的所有数字,然后看看是否有前导'-',如果有,将int乘以-1。

对于这样一个小问题来说,这似乎有点过于复杂了。什么好主意吗?


当前回答

有很多答案,很多可能性。我在这里缺少的是一些通用方法,可以将字符串转换为不同的c++整型(short, int, long, bool,…) 我想出了以下解决方案:

#include<sstream>
#include<exception>
#include<string>
#include<type_traits>

using namespace std;

template<typename T>
T toIntegralType(const string &str) {
    static_assert(is_integral<T>::value, "Integral type required.");
    T ret;
    stringstream ss(str);
    ss >> ret;
    if ( to_string(ret) != str)
        throw invalid_argument("Can't convert " + str);
    return ret;
}

下面是用法示例:

string str = "123";
int x = toIntegralType<int>(str); // x = 123

str = "123a";
x = toIntegralType<int>(str); // throws exception, because "123a" is not int

str = "1";
bool y = toIntegralType<bool>(str); // y is true
str = "0";
y = toIntegralType<bool>(str); // y is false
str = "00";
y = toIntegralType<bool>(str); // throws exception

为什么不直接使用stringstream输出操作符将字符串转换为整型? 以下是答案: 假设字符串包含的值超出了预期整型的限制。例如,在windows 64上,max int是2147483647。 让我们给一个字符串赋值max int + 1: string str = "2147483648"。 现在,当将字符串转换为int类型时:

stringstream ss(str);
int x;
ss >> x;

X变成2147483647,这肯定是一个错误:字符串“2147483648”不应该被转换为int 2147483647。为integraltype提供的函数会发现这样的错误并抛出异常。

其他回答

有很多答案,很多可能性。我在这里缺少的是一些通用方法,可以将字符串转换为不同的c++整型(short, int, long, bool,…) 我想出了以下解决方案:

#include<sstream>
#include<exception>
#include<string>
#include<type_traits>

using namespace std;

template<typename T>
T toIntegralType(const string &str) {
    static_assert(is_integral<T>::value, "Integral type required.");
    T ret;
    stringstream ss(str);
    ss >> ret;
    if ( to_string(ret) != str)
        throw invalid_argument("Can't convert " + str);
    return ret;
}

下面是用法示例:

string str = "123";
int x = toIntegralType<int>(str); // x = 123

str = "123a";
x = toIntegralType<int>(str); // throws exception, because "123a" is not int

str = "1";
bool y = toIntegralType<bool>(str); // y is true
str = "0";
y = toIntegralType<bool>(str); // y is false
str = "00";
y = toIntegralType<bool>(str); // throws exception

为什么不直接使用stringstream输出操作符将字符串转换为整型? 以下是答案: 假设字符串包含的值超出了预期整型的限制。例如,在windows 64上,max int是2147483647。 让我们给一个字符串赋值max int + 1: string str = "2147483648"。 现在,当将字符串转换为int类型时:

stringstream ss(str);
int x;
ss >> x;

X变成2147483647,这肯定是一个错误:字符串“2147483648”不应该被转换为int 2147483647。为integraltype提供的函数会发现这样的错误并抛出异常。

要将字符串表示形式转换为整数值,可以使用std::stringstream。

如果转换的值超出整数数据类型的范围,则返回INT_MIN或INT_MAX。

此外,如果字符串值不能表示为有效的int数据类型,则返回0。

#include 
#include 
#include 

int main() {

    std::string x = "50";
    int y;
    std::istringstream(x) >> y;
    std::cout << y << '\n';
    return 0;
}

输出: 50

根据上面的输出,我们可以看到它从字符串数转换为整数数。

来源和更多的字符串int c++

如果你不懂硬代码:)

bool strCanBeInt(std::string string){
    for (char n : string) {
        if (n != '0' && n != '1' && n != '2' && n != '3' && n != '4' && n != '5'
            && n != '6' && n != '7' && n != '8' && n != '9') {
            return false;
        }
    }
    return true;
}

int strToInt(std::string string) {
    int integer = 0;
    int numInt;
    for (char n : string) {
        if(n == '0') numInt = 0;
        if(n == '1') numInt = 1;
        if(n == '2') numInt = 2;
        if(n == '3') numInt = 3;
        if(n == '4') numInt = 4;
        if(n == '5') numInt = 5;
        if(n == '6') numInt = 6;
        if(n == '7') numInt = 7;
        if(n == '8') numInt = 8;
        if(n == '9') numInt = 9;
        if (integer){
            integer *= 10;
        }
        integer += numInt;
    }
    return integer;
}

使用atoi函数将字符串转换为整数:

string a = "25";

int b = atoi(a.c_str());

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

在c++ 11中,有一些不错的新转换函数,从std::string转换为数字类型。

所以与其

atoi( str.c_str() )

你可以使用

std::stoi( str )

其中STR是你的数字std::string。

各种类型的数字都有对应的版本: 长stol(字符串),浮动stof(字符串),双stof(字符串),… 参见http://en.cppreference.com/w/cpp/string/basic_string/stol