【问题标题】:printing error message after third substring is found找到第三个子字符串后打印错误消息
【发布时间】:2020-08-25 00:24:13
【问题描述】:

您好,我是 C++ 新手。我有一个关于字符串拆分的问题。

例如,我有一个这样的字符串

std::string str = "jump 110 5";

字符串“jump”、“110”和“5”之间可以有尽可能多的空格。

我想将 110 保存在 int 变量中,如果在数字 110 之后应该出现另一个数字或字符,则循环应该中断。

到目前为止,我已经删除了所有空格并将 110 保存在一个变量中并将其打印出来,而数字 5 被忽略了。

如何在 110 之后中断或打印错误消息,说明字符串无效?

这是我的代码:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main() {

    std::string str = "jump 110 5";
    size_t i = 0;
    for ( ; i < str.length(); i++ ){ if ( isdigit(str[i]) ) break; }    

    str = str.substr(i, str.length() - i );    

    int id = atoi(str.c_str());
    std::cout<<id;

     return 0;    
}

【问题讨论】:

    标签: c++ string spaces


    【解决方案1】:

    编辑:第二次尝试.. 这是我写的将值解析为向量的内容。如果向量有一组以上的数字,它会打印并出错。

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <vector>
    
    
    int main() {
    
        std::string str = "jump 110 5";
        size_t i = 0;
        for (; i < str.length(); i++)
        {
            if (isdigit(str[i]))
                break;
        }
    
        str = str.substr(i, str.length() - i);
    
    
        // Parse str to id
        std::stringstream sstr;
        sstr << str;
        std::string tempStr;
        std::vector<size_t> id;   
    
        while (std::getline(sstr, tempStr, ' '))
        {
            std::cout << tempStr << std::endl;
            id.push_back(std::stoi(tempStr));
        }
    
        // print an error if additional numbers are in id
        if (id.size() > 1)
        {
            std::cout << "Error: " << id[1];
        }
        else
        {
            std::cout << "You're good";
        }
    
        return 0;
    }
    

    注意:如果 110 和 5 之间有多个空格,则会出错。

    原答案: 如果您希望id 具有值110 5,那么 id 不应该是 int 类型,因为它不能包含空格。如果您希望 id 具有值1105,那么您需要在将其分配给id 之前去掉字符串“110 5”中的空格,否则在分配给id 时该值将被截断.

    这就是我为摆脱空格使 id 等于 1105 所做的。

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    
    int main() {
    
        std::string str = "jump 110 5";
        size_t i = 0;
        for (; i < str.length(); i++)
        {
            if (isdigit(str[i]))
                break;
        }
    
        str = str.substr(i, str.length() - i);
    
        // get rid of spaces
        std::string tempStr;
        for (int i = 0; i < str.length(); i++)
        {
            if (str[i] != ' ')
            {
                tempStr.push_back(str[i]);
            }
        }
    
        // Set the str object to tempStr
        str = tempStr;
    
        int id = atoi(str.c_str());
        std::cout << id;
    
    
        return 0;
    }
    

    如果您确实需要将id 保留为int 并将1105 分开,那么我建议将id 设置为std::vector 并将字符串解析为向量。

    【讨论】:

    • 感谢您的回答。但是我希望 Id 保持 110,如果在 110 之后还有另一个数字,那么应该打印一个错误
    • @konoha 嗯好的,我想我现在更了解你了。在这种情况下,您需要将字符串解析为 int 向量。我会自己编写代码来演示,但我不太擅长解析。
    • 我根据自己的想法更新了我的答案。让我知道你的想法
    • 非常感谢,感谢您抽出宝贵时间为我提供答案:)
    猜你喜欢
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2021-01-16
    • 2018-12-28
    相关资源
    最近更新 更多