c++ - 如何从未知数量的 int 的行构造一个向量 // c++
问题描述
通常输入的数量是给定的,我可以用 for 循环来做,但不在那里:https ://codeforces.com/contest/469/problem/A
我拼命地尝试从一行 ints 构造一个向量,但我只知道 int 的数量不能高于 100。有 2 行输入。
如果我做了一个 Cin 的无限循环,当它到达行尾或输入端时我怎么能打破它?
解决方案
You might consider reading in the entire line as a string first:
std::string line;
std::getline (istream,name);
then splitting that string into your vector (boost example):
std::vector<int> split(std::string const& str)
{
using namespace std;
using namespace boost;
std::vector<int> results;
tokenizer<> tok(str);
for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
results.emplace_back(std::stoi(*beg));
}
return results;
}
推荐阅读
- c++ - 有 50% 的几率随机生成器连续出现 15 个数字,为什么?
- python - 我想在 python 中应用低通滤波器
- python - 如何从管理员端在生产中的 django 网站中插入数据
- c# - IHttpClientFactory通过web api读取数据,对象类型变成ValueKind = Number?
- python - 使用 Python docx 替换文本但保留图像
- reactjs - 如何在react js中由highchart绘制的折线图中获取选定的年份范围
- python - 在列名中使用日期时出现 Pandas KeyError
- visual-studio-code - 将视觉工作室重置为默认设置
- fonts - 如何在堆栈溢出的注释部分中发布代码片段
- c++ - 使用 librdkafka 在 kafka 生产者中将结构作为输入而不是字符串