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

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

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

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

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


当前回答

 int stringToInt(std::string value) {    
 if(value.length() == 0 ) return 0; //tu zmiana..

 if (value.find(  std::string("NULL") ) != std::string::npos) {
     return 0;
  }
 
  if (value.find(  std::string("null") ) != std::string::npos) {
     return 0;
  }
 
 
int i;
std::stringstream stream1;
stream1.clear();
stream1.str(value);
stream1 >> i;
return i;

};

其他回答

为了更详尽(正如评论中要求的那样),我使用std::from_chars添加了c++ 17给出的解决方案。

std::string str = "10";
int number;
std::from_chars(str.data(), str.data()+str.size(), number);

如果您想检查转换是否成功:

std::string str = "10";
int number;
auto [ptr, ec] = std::from_chars(str.data(), str.data()+str.size(), number);
assert(ec == std::errc{});
// ptr points to chars after read number

此外,要比较所有这些解决方案的性能,请参阅以下快速工作台链接:https://quick-bench.com/q/GBzK53Gc-YSWpEA9XskSZLU963Y

(std::from_chars是最快的std::istringstream是最慢的)

你可以使用std::stringstream,这里有一个例子:

#include <iostream>
#include <sstream>
using namespace std;
string r;
int main() {
    cin >> r;
    stringstream tmp(r);
    int s;
    tmp >> s;
    cout << s;
    return 0;
}

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

string a = "25";

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

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

诚然,我的解决方案不适用于负整数,但它将从包含整数的输入文本中提取所有正整数。它使用numeric_only locale:

int main() {
        int num;
        std::cin.imbue(std::locale(std::locale(), new numeric_only()));
        while ( std::cin >> num)
             std::cout << num << std::endl;
        return 0;
}

输入文本:

 the format (-5) or (25) etc... some text.. and then.. 7987...78hjh.hhjg9878

输出整数:

 5
25
7987
78
9878

类numeric_only定义为:

struct numeric_only: std::ctype<char> 
{
    numeric_only(): std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table()
    {
        static std::vector<std::ctype_base::mask> 
            rc(std::ctype<char>::table_size,std::ctype_base::space);

        std::fill(&rc['0'], &rc[':'], std::ctype_base::digit);
        return &rc[0];
    }
};

完整在线演示:http://ideone.com/dRWSj

如果你不懂硬代码:)

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;
}