如果我有一行std::cin
的输入字符串,它由字符串和由空格分隔的整数组成。将它们分开并存储的最有效方法是什么?
e.g:
input: "Move 1 to 2"
variables for storing:
string a, b;
int orig, dest;
修改
我已根据建议应用了以下代码。但是,当我输入“将9移动到1”时,似乎只有“移动”这个词被正确地存储在矢量中。
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;
答案 0 :(得分:2)
我将假设整数和字符串的顺序未知。您可以利用cin
转换为bool来决定是否检测到int。
基本上,(cin >> intValue)
(其中intValue
是int
)是一个表达式,如果接下来的几个字符构成一个可以放入{true
的有效数字,则返回int
{1}},否则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
,该向量将包含1
和2
,该向量将包含Move
和to
。
答案 1 :(得分:1)
您正在寻找 解析 文字。
你的“输入语法”是......未说明的,但这里有一个Parser框架,例如:提升精神:
#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)