使用任意结构从C ++中的字符串中提取整数

时间:2014-04-22 23:37:41

标签: c++ string parsing split atoi

这似乎是一个应该易于搜索的问题,但是那里的任何答案似乎都被大量的问题所淹没,这些问题要求将转换字符串的更常见问题转化为整数

我的问题是:从std::strings中提取可能看起来像"abcd451efg""hel.lo42-world!""hide num134rs here?"的整数的简单方法我知道我可以使用isDigit自己手动解析字符串,但我想知道atoistoi等是否有更标准的方法。

上面的输出将是451,42和134.我们还可以假设字符串中只有一个整数(尽管一般解决方案不会受到伤害)。因此,我们不必担心像"abc123def456"这样的字符串。

Java以

的形式提供了一个简单的解决方案
Integer.parseInt(str.replaceAll("[\\D]", ""));

C ++有没有直截了当的东西?

3 个答案:

答案 0 :(得分:4)

你可以使用 string::find_first_of("0123456789")获取第一个数字的位置,然后string::find_last_of("0123456789")获取最后一个数字的位置,最后在两个位置定义的子字符串上使用atoi。我想不出更简单的东西(没有正则表达式)。

顺便说一句,这只适用于字符串中有一个数字的情况。

以下是一个例子:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
    string s = "testing;lasfkj358kdfj-?gt";
    size_t begin = s.find_first_of("0123456789");
    size_t end = s.find_last_of("0123456789");
    string num = s.substr(begin, end - begin + 1);
    int result = atoi(num.c_str());
    cout << result << endl;
} 

如果您有多个号码,可以将string::find_first_ofstring::find_first_not_of合并,以获取字符串中每个号码的开头和结尾。

此代码是一般解决方案:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    string s = "testing;lasfkj358kd46fj-?gt"; // 2 numbers, 358 and 46

    size_t begin = 0, end = 0; 

    while(end != std::string::npos)
    {
        begin = s.find_first_of("0123456789", end);
        if(begin != std::string::npos) // we found one
        {
            end = s.find_first_not_of("0123456789", begin);
            string num = s.substr(begin, end - begin);
            int number = atoi(num.c_str());
            cout << number << endl;
        }
    }
}

答案 1 :(得分:2)

即使有尾随的非数字,

atoi也可以从字符串中提取数字

int getnum(const char* str)
{
    for(; *str != '\0'; ++str)
    {
        if(*str >= '0' && *str <= '9')
            return atoi(str);
    }
    return YOURFAILURENUMBER;
}

答案 2 :(得分:1)

这是单程

#include <algorithm>
#include <iostream>
#include <locale>
#include <string>

int main(int, char* argv[])
{
  std::string input(argv[1]);

  input.erase(
    std::remove_if(input.begin(), input.end(), 
      [](char c) { return !isdigit(c, std::locale()); }),
    input.end()
  );

  std::cout << std::stoll(input) << '\n';
}

您还可以使用<functional>库来创建谓词

auto notdigit = not1(
  std::function<bool(char)>(
    bind(std::isdigit<char>, std::placeholders::_1, std::locale())
  )
);

input.erase(
  std::remove_if(input.begin(), input.end(), notdigit),
  input.end()
);

值得指出的是,到目前为止,其他两个答案硬编码数字检查,使用localeisdigit保证您的程序将根据当前全局识别数字区域设置。