不,您不需要将它们转换为字符串.使用C++标准库的容器和算法实际上非常简单(只要分隔符是空格或一系列空格,这就可以工作):
#include <iterator>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v;
// An easy way to read a vector of integers from the standard input
std::copy(
std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::back_inserter(v)
);
// An easy wait to print the vector to the standard output
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1685 次 |
| 最近记录: |