C++将字符串拆分为字符串和int

C++ Splitting a string into strings and ints

本文关键字:字符串 int 拆分 C++      更新时间:2023-10-16

如果我有一行来自std::cin的输入字符串,它由用空格分隔的字符串和int组成。将它们分离和存储的最有效方法是什么?

例如:

input: "Move 1 to 2" 
variables for storing: 
string a, b; 
int orig, dest;

编辑:

我已经按照建议应用了以下代码。然而,当我输入"move 9 on 1"时,似乎只有单词"move"正确地存储在向量strs中。

    string command;
    cin >> command;
    vector<int> ints;
    vector<string> strs;
    string strval;
    int intval;
    stringstream test;
    test.str(command);
    while(true) {
        if(test.eof())
            break;
        if(test >> strval) {
            strs.push_back(strval);
        }
        else if(test >> intval) {
            ints.push_back(intval);
        }
        else {
            cout << "error!" << endl;
        }
    }

问题已解决:
使用

getline(cin, command);

而不是

cin >> command;

我假设int和字符串的顺序是未知的。您可以利用cin到布尔的转换来决定是否检测到int。

基本上,(cin >> intValue)(其中intValueint)是一个表达式,如果接下来的几个字符构成可以放入int的有效数字,则返回true,否则返回false。同样的原理适用于其他类型,如string。这些可以用于if语句,如

int intValue;
if (cin >> intValue) {  //evaluates to true or false
// do something
} else {
// do something else
}

您可以使用while循环来解析整个输入,如下所示:

vector<int> ints; //container to store ints
vector<string> strings; //container to store ints
while(true) {
  int intValue;
  string stringValue;
  if(cin.eof())  //exit the loop when the end of the input is reached
    break;
  if(cin >> intValue) {  //if this is true, then an int was successfully read into intValue
    ints.push_back(intValue);
  } else if (cin >> stringValue) { //if this is true, int could not be read but string was successfully read
    strings.push_back(stringValue);
  } else {
    cout << "Error: unknown value read in, not recognized as int or string" << endl;
    exit(-1);
  }
}

编辑:

我刚读到你已经把这行写成了字符串。上面相同的解决方案也可以,只需使用字符串流而不是cin:

string line; //the line that you already have, initialized elsewhere
stringstream ss(line.str()); //convert the line to a stringstream, treat it similar to cin
vector<int> ints;  //container to store ints
vector<string> strings;  //container to store strings
while(true) {
  int intValue;
  string stringValue;
  if(ss.eof())
    break;
  if(ss >> intValue) {
    ints.push_back(intValue);
  } else if (ss >> stringValue) {
    strings.push_back(stringValue);
  } else {
    cout << "Error: unknown value read in, not recognized as int or string" << endl;
    exit(-1);
  }
}

在您的示例中,即线Move 1 to 2,矢量将包含12,矢量将包括Moveto

您正在查找解析文本。

你的"输入语法"是…未指定,但这里使用了一个Parser框架,例如Boost Spirit:

#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
namespace qi    = boost::spirit::qi;
struct Command { std::string name; int a, b; };
BOOST_FUSION_ADAPT_STRUCT(Command, (std::string, name)(int,a)(int,b))
int main()
{
    const std::string input("Move 2 to 4");
    auto f(begin(input)), l(end(input));
    Command parsed; 
    bool ok = qi::phrase_parse(f,l,
           qi::string("Move")     >> qi::int_ >> "to"   >> qi::int_
         | qi::string("Multiply") >> qi::int_ >> "by"   >> qi::int_
         | qi::string("Subtract") >> qi::int_ >> "from" >> qi::int_
         , qi::space, parsed);
    if (ok)   
    {
        std::cout << "parse successn";
        std::cout << "parsed: '" << parsed.name << "' with (" << parsed.a << ", " << parsed.b << ")" << "n";
    }
    else std::cerr << "parse failed: '" << std::string(f,l) << "'n";
    if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'n";
}

打印

parse success
parsed: 'Move' with (2, 4)