如何从命令行中读取以空格分隔的输入数字
How to read input numbers separated with spaces from command line c++
我有下面的代码:
std::cin >> N >> T;
std::vector<int> width;
for (int w = 0; w < N; w++)
{
int tmp;
std::cin >> tmp;
width.push_back(tmp);
}
如果我把3 2 3 4
作为控制台的输入,它被空白分隔并存储为矢量数组。
我的问题是它如何能够读取由空格分隔的cin
中的整数值?我以为你应该做4次std::cin >> tmp
输入?
你的意思是:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
int main( int argc, char **argv )
{
std::vector<int> v;
v.reserve( std::max( 0, argc - 1 ) );
for ( int i = 1; i < argc; i++ ) v.push_back( std::atoi( argv[i] ) );
//...
}
相关文章:
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 野牛/yacc 解析器在不被空格分隔时跳过 grammer - "unexpected $end"
- 从 stdio 获取一行空格分隔的整数,而不知道它们是多少 (C++)
- C++ 用空格和逗号分隔字符串
- 如何从文件中读取用空格分隔的数字
- 反转由空格分隔的字符串元素将返回比原始字符串更大的字符串
- 获取由空格分隔的未知大小的单行输入,并将其存储到整数数组中
- 如果用户输入两个或多个由空格分隔的字符串C++如何防止缓冲区溢出?
- 使用 fstream 在空格分隔文件中查看下一项(不仅仅是一个字符)的方法
- 非空格分隔的文本文件到 2D 矢量
- 如何在用空格分隔的行中获得无限的输入
- 如何从空格分隔的单个字符串中提取多个子字符串
- 使用 cin 读取逗号分隔的整数对 (x,y) 和任意空格
- 匹配由符号分隔的重复单词,忽略空格
- 如何使用符号分隔字符串而不是空格的输入字符串流
- 如何获得一些用空格分隔的字符