繁体   English   中英

C ++将字符串拆分为字符串和整数

[英]C++ Splitting a string into strings and ints

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

例如:

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

编辑:

我已按照建议应用以下代码。 但是,当我输入“将9移动到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;

我将假设整数和字符串的顺序未知。 您可以利用cin到bool的转换来确定是否检测到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);
  }
}

编辑:

我刚刚读到您已经将该行作为字符串。 上面相同的解决方案将起作用,只是使用stringstream而不是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

您正在寻找解析文本。

您的“输入语法”是...未指定,但是这里有一个解析器框架,例如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 success\n";
        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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM