[英]How to format vector<string> and push_back() into other vectors?
Basically the sudo-code I'm trying to figure out how to do is like this: 基本上,我试图弄清楚的sudo代码是这样的:
vector<string> lines; // filled by txt file where each line is formatted: "string int"
vector<string> queries;
vector<int> counts;
for (int i = 0; i < lines.size(); i++){
format(lines[i], "%s %d", queries.push_back(%s), queries.push_back(%d));
}
I have no clue what function achieves this. 我不知道什么功能可以实现这一点。 I tried
scanf()
and strtok()
, but ran into problems with both. 我尝试了
scanf()
和strtok()
,但都遇到了问题。 Is there a better way? 有没有更好的办法?
scanf()
and strtok()
is C, not C++. scanf()
和strtok()
是C,而不是C ++。
In C++, you use std::istringstream
, and operator>>
, for example: 在C ++中,您使用
std::istringstream
和operator>>
,例如:
#include <sstream>
std::string line="apple 42";
std::istringstream i(line);
std::string fruit;
int count;
i >> fruit >> count;
if (i.fail())
{
// Parsing error, up to you what to do about it.
}
Once you have you string, and integer parsed, then you push it into your vectors, as usual. 字符串化并解析完整数后,便照常将其放入向量中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.