【问题标题】:How to find values in a string and convert them to integers?如何在字符串中查找值并将它们转换为整数?
【发布时间】:2014-08-10 00:29:39
【问题描述】:

我的字符串是“我有 5 支铅笔,汤姆有 3 支铅笔”。 如何找到字符串中的值,然后将它们转换为整数?

【问题讨论】:

    标签: c++ string int


    【解决方案1】:

    您将需要 std::string 和 regex(std::regex 目前无法使用,因此请使用 boost)来分隔数字。适合您的典型算法是:

    1. 创建 boost::regex 来分隔数字。
    2. 使用 boost::regex_search 获取您的号码。
    3. 使用 std::stoi 将字符串转换为整数。

    示例代码为:

    #include <iostream>
    #include <string>
    #include <boost/regex.hpp>
    
    int main ()
    {
      std::string s ("I have 5 pencils and Tom have 3 pencils");
      boost::smatch m;
      boost::regex e("[0-9]+");
    
      std::cout << "Target sequence: " << s << std::endl;
      std::cout << "Regular expression: /[0..9]+/" << std::endl;
      std::cout << "The following matches and submatches were found:" << std::endl;
    
      while (boost::regex_search (s, m, e))
      {
        for (auto x : m)
          std::cout << std::stoi(x) << " ";
    
        std::cout << std::endl;
        s = m.suffix().str();
      }
    
       return 0;
    }
    

    简单的 Makefile 来构建它:

    CC=g++
    FLAGS=-std=c++11
    LIBS=-lboost_regex
    
    test: test.cpp
            $(CC) $(FLAGS) $(LIBS) test.cpp -o test
    

    您应该安装 boost 或使用您选择的正则表达式库。

    【讨论】:

    • 现在我们要解决问题了。哦,还有stoi sucks
    • @rubenvb,是的,如果您不控制输入,则 std::stoi 很烂。但是由于输入由 boost::regex 或任何其他正则表达式控制 - 你没有什么好担心的。另外try {} catch() 还在工作。所以捕捉异常并不难,对吧?
    • @Tanuki,除了正则表达式,我还可以使用其他方法吗?
    • @panadol,正则表达式是最简单的方法。如果您愿意,可以编写自己的标记器来查找数字序列。类似于: for (auto c : search_string) if (c >= '0' && c
    【解决方案2】:

    您可以尝试标记化。

    创建一个 Token 和一个 Token_stream 类。

    class Token
    {
    public:
        /* Implementation of the class */
    
    private:
        char token_type;    // (e.g. store a letter such as 'a' for alphabetic
                            // characters and a number such as '8' for numbers)
    
        char letter;    // store which letter or ...
        int number;     // ... which number
    }
    
    class Token_stream
    {
    public:
        /* Implementation */
    
    private:
        vector<Token> tokens;
    }
    

    将字符串中包含的每个字符存储在一个不同的 Token 中,然后将 Token 存储到 Token_stream 中。然后检查 Token_stream 中的每个 Token 以了解它的类型是数字还是字母字符。如果它是一个数字,而不仅仅是将它的值存储在整数向量中,否则检查流中的下一个 Token。

    使用 range-for 循环,检查字符串中存储的每个字符,如果是数字,则将其存储在 int 中。

    string s;
    getline(cin, s);
    
    vector<int> numbers;
    
    for (char c : s)
    {
        if (48 <= c && c <= 57)    // ASCII values for characters of numbers 48 == '0',
        {                          // 49 == '1', ..., 57 == '9'
            cin.putback(c);    // put c back into the standard input, so that you can 
                               // read it again as an int
    
            int n;
            cin >> n;
    
            numbers.push_back(n);
        }
    }
    

    【讨论】:

    • @user3194923,如何将值存储在整数向量中?
    • 我明白了。但是 int n 的作用是什么?
    • panadol,range-for 循环内的 int n 就在那里,因此您可以将数字的表示形式(您放回 cin 中)读取为实际整数。如果你只看下面的 int n,你可以看到你正在将 cin 的值读入 n。明白了吗?
    猜你喜欢
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多