Sea*_*ean 35 c++ templates function vector
我试图让用户输入放入向量中的数字,然后使用函数调用来输入数字,为什么这不起作用?我只能说出第一个号码.
template <typename T>
void write_vector(const vector<T>& V)
{
cout << "The numbers in the vector are: " << endl;
for(int i=0; i < V.size(); i++)
cout << V[i] << " ";
}
int main()
{
int input;
vector<int> V;
cout << "Enter your numbers to be evaluated: " << endl;
cin >> input;
V.push_back(input);
write_vector(V);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
jsi*_*ger 28
按原样,您只需读取一个整数并将其推入向量中.由于您可能想要存储多个整数,因此需要循环.例如,替换
cin >> input;
V.push_back(input);
Run Code Online (Sandbox Code Playgroud)
同
while (cin >> input)
V.push_back(input);
Run Code Online (Sandbox Code Playgroud)
只要有输入要抓住,这样做就是不断从cin中提取内注; 循环继续,直到cin找到EOF或尝试输入非整数值.另一种方法是使用sentinel值,但这会阻止您实际输入该值.例如:
while ((cin >> input) && input != 9999)
V.push_back(input);
Run Code Online (Sandbox Code Playgroud)
将读取,直到您尝试输入9999(或任何其他使cin无效的状态),此时循环将终止.
Jon*_*rdy 18
其他答案会让您不允许使用特定的数字,或者告诉用户输入非数字的内容以终止输入.也许更好的解决方案是使用std::getline()读取一行输入,然后用于std::istringstream从该行读取向量中的所有数字.
#include <iostream>
#include <sstream>
#include <vector>
int main(int argc, char** argv) {
std::string line;
int number;
std::vector<int> numbers;
std::cout << "Enter numbers separated by spaces: ";
std::getline(std::cin, line);
std::istringstream stream(line);
while (stream >> number)
numbers.push_back(number);
write_vector(numbers);
}
Run Code Online (Sandbox Code Playgroud)
此外,您的write_vector()实现可以替换为更惯用的std::copy()算法调用,以将元素复制std::ostream_iterator到std::cout:
#include <algorithm>
#include <iterator>
template<class T>
void write_vector(const std::vector<T>& vector) {
std::cout << "Numbers you entered: ";
std::copy(vector.begin(), vector.end(),
std::ostream_iterator<T>(std::cout, " "));
std::cout << '\n';
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用std::copy()几个方便的迭代器将值放入向量中,而无需显式循环:
std::copy(std::istream_iterator<int>(stream),
std::istream_iterator<int>(),
std::back_inserter(numbers));
Run Code Online (Sandbox Code Playgroud)
但这可能是矫枉过正的.
Naw*_*waz 12
你需要一个循环.这样做:
while (cin >> input) //enter any non-integer to end the loop!
{
V.push_back(input);
}
Run Code Online (Sandbox Code Playgroud)
或者使用这个惯用版:
#include <iterator> //for std::istream_iterator
std::istream_iterator<int> begin(std::cin), end;
std::vector<int> v(begin, end);
write_vector(v);
Run Code Online (Sandbox Code Playgroud)
你也可以改善你write_vector的:
#include <algorithm> //for std::copy
template <typename T>
void write_vector(const vector<T>& v)
{
cout << "The numbers in the vector are: " << endl;
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
}
Run Code Online (Sandbox Code Playgroud)
你有2个选择:
如果您知道向量的大小(在您的情况/示例中,您似乎知道它):
vector<int> V(size)
for(int i =0;i<size;i++){
cin>>V[i];
}
Run Code Online (Sandbox Code Playgroud)
如果你不这样做,你就无法在你的程序流程中得到它:
int helper;
while(cin>>helper){
V.push_back(helper);
}
Run Code Online (Sandbox Code Playgroud)
如果你知道向量的大小,你可以这样做:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (auto &it : v) {
cin >> it;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
如果您知道尺寸,请使用此
没有仅用于存储用户输入的临时变量
int main()
{
cout << "Hello World!\n";
int n;//input size
cin >> n;
vector<int>a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
//to verify output user input printed below
for (auto x : a) {
cout << x << " ";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您需要第二个整数。
int i,n;
vector<int> V;
cout << "Enter the amount of numbers you want to evaluate: ";
cin >> i;
cout << "Enter your numbers to be evaluated: " << endl;
while (V.size() < i && cin >> n){
V.push_back(n);
}
write_vector(V);
return 0;
Run Code Online (Sandbox Code Playgroud)
单行读取固定数量的数字到向量(C++ 11):
#include <algorithm>
#include <iterator>
#include <iostream>
#include <vector>
#include <cstddef>
int main()
{
const std::size_t LIMIT{5};
std::vector<int> collection;
std::generate_n(std::back_inserter(collection), LIMIT,
[]()
{
return *(std::istream_iterator<int>(std::cin));
}
);
return 0;
}
Run Code Online (Sandbox Code Playgroud)