C++将字符串拆分为字符串和int
C++ Splitting a string into strings and ints
如果我有一行来自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)
(其中intValue
是int
)是一个表达式,如果接下来的几个字符构成可以放入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
,矢量将包含1
和2
,矢量将包括Move
和to
。
您正在查找解析文本。
你的"输入语法"是…未指定,但这里使用了一个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)
相关文章:
- 我如何获取从 C++ 到 C 的映射<字符串、int>#
- 构造<int>具有 2 个字符串文字的向量
- 字符串中int的加法运算
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- cin >> int 给定一个字符串将 int 赋值为 0
- 将字符串中的 int 转换为字母;
- 使用 switch,但我想用字符串而不是 int 进行选择
- 两个数组,int 和 string,如何显示带有对应 int 的字符串?
- 从字符串流中读取具有特定字段宽度的 int
- win 表单应用程序字符串^ 到 int
- C++程序在将 int 与 cin 一起使用时有效,但不能使用字符串
- 将子字符串字符从字符串值转换为 int,然后将其分配给 int 变量
- 将字符串转换为 int 后的随机值
- 如何检查字符串是否正确以将其转换为 int?
- 在 c++ 中逐个字符从字符串传递到 int 时不精确
- <int> 使用 fmt 库将向量转换为字符串
- 我正在尝试将 int 值插入字符串,而不是 1,我得到 10
- 为什么编译:字符串 = int
- C++ 隐式类型转换字符串 -> int?
- 如何在C++中连接字符串+int+字符串