我正在尝试让用户输入放入矢量中的数字, 然后使用函数调用cout数字,为什么这不是 工作?我只能说出第一个数字。
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;
}
答案 0 :(得分:27)
原样,你只是读取一个整数并将其推入向量中。由于您可能想要存储多个整数,因此需要循环。例如,替换
cin >> input;
V.push_back(input);
与
while (cin >> input)
V.push_back(input);
只要有输入要抓住,这样做就会不断地从cin中提取内注;循环继续,直到cin找到EOF或尝试输入非整数值。另一种方法是使用sentinel值,但这会阻止您实际输入该值。例如:
while ((cin >> input) && input != 9999)
V.push_back(input);
将读取,直到您尝试输入9999(或任何其他使cin无效的状态),此时循环将终止。
答案 1 :(得分:17)
其他答案会让您不允许使用特定号码,或者告诉用户输入非数字的内容以终止输入。也许更好的解决方案是使用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);
}
此外,您的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';
}
您还可以使用std::copy()
和几个方便的迭代器将值放入向量中,而不需要显式循环:
std::copy(std::istream_iterator<int>(stream),
std::istream_iterator<int>(),
std::back_inserter(numbers));
但这可能有点过头了。
答案 2 :(得分:11)
你需要一个循环。所以这样做:
while (cin >> input) //enter any non-integer to end the loop!
{
V.push_back(input);
}
或者使用这个惯用语:
#include <iterator> //for std::istream_iterator
std::istream_iterator<int> begin(std::cin), end;
std::vector<int> v(begin, end);
write_vector(v);
您还可以将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, " "));
}
答案 3 :(得分:8)
你有两个选择:
如果您知道矢量的大小(在您的情况/示例中,您似乎知道它):
vector<int> V(size)
for(int i =0;i<size;i++){
cin>>V[i];
}
如果你不这样做,你就无法在你的程序流程中得到它:
int helper;
while(cin>>helper){
V.push_back(helper);
}
答案 4 :(得分:5)
单线将固定数量的数字读入矢量(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;
}
答案 5 :(得分:4)
你需要第二个整数。
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;
答案 6 :(得分:3)
cin是在空格上分隔的,所以如果你试图将“1 2 3 4 5”转换成一个整数,你只能将1分配给整数,更好的选择是将输入和push_back包装在一个整数中。循环,并测试一个sentinel值,并在该sentinel值上调用您的write函数。比如
int input;
cout << "Enter your numbers to be evaluated, and 10000 to quit: " << endl;
while(input != 10000) {
cin >> input;
V.push_back(input);
}
write_vector(V);
答案 7 :(得分:3)
如果您知道向量的大小,则可以这样操作:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (auto &it : v) {
cin >> it;
}
}
答案 8 :(得分:3)
您可以在 for loop 的帮助下完成此操作 - &gt;在运行时询问用户(他想要输入多少输入),并像对待数组那样对待。
int main() {
int sizz,input;
std::vector<int> vc1;
cout<< "How many Numbers you want to enter : ";
cin >> sizz;
cout << "Input Data : " << endl;
for (int i = 0; i < sizz; i++) {//for taking input form the user
cin >> input;
vc1.push_back(input);
}
cout << "print data of vector : " << endl;
for (int i = 0; i < sizz; i++) {
cout << vc1[i] << endl;
}
}
答案 9 :(得分:2)
您可能想要阅读更多数字,而不仅仅是一个。 为此,您需要一个循环
int main()
{
int input = 0;
while(input != -1){
vector<int> V;
cout << "Enter your numbers to be evaluated: " << endl;
cin >> input;
V.push_back(input);
write_vector(V);
}
return 0;
}
注意,对于此版本,无法添加数字-1,因为它是“结束信号”。 只要您愿意,键入数字,键入-1时将中止。
答案 10 :(得分:2)
如果您知道尺寸,请使用此
没有临时变量仅用于存储用户输入
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;
}
答案 11 :(得分:2)
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
vector<string>V;
int num;
cin>>num;
string input;
while (cin>>input && num != 0) //enter any non-integer to end the loop!
{
//cin>>input;
V.push_back(input);
num--;
if(num==0)
{
vector<string>::iterator it;
for(it=V.begin();it!=V.end();it++)
cout<<*it<<endl;
};
}
return 0;
};
答案 12 :(得分:1)
#include<iostream>
#include<vector>
#include<sstream>
using namespace std;
int main()
{
vector<string> v;
string line,t;
getline(cin,line);
istringstream iss(line);
while(iss>>t)
v.push_back(t);
vector<string>::iterator it;
for(it=v.begin();it!=v.end();it++)
cout<<*it<<endl;
return 0;
}
答案 13 :(得分:1)
只需添加另一个变量。
int temp;
while (cin >> temp && V.size() < n){
V.push_back(temp);
}
答案 14 :(得分:1)
size()
的初始V
将为0,而int n包含任何随机值,因为您没有初始化它。
V.size() < n
可能是假的。
愚蠢的我错过了"Enter the amount of numbers you want to evaluate: "
如果您当时输入的n
小于V.size()
,则循环将终止。
答案 15 :(得分:1)
在这种情况下,您的while
循环将显示为
int i = 0;
int a = 0;
while (i < n){
cin >> a;
V.push_back(a);
++i;
}
答案 16 :(得分:0)
会更容易:
int main()
{
int input,n;
vector<int> V;
cout<<"Enter the number of inputs: ";
cin>>n;
cout << "Enter your numbers to be evaluated: " << endl;
for(int i=0;i<n;i++){
cin >> input;
V.push_back(input);
}
write_vector(V);
return 0;
}
答案 17 :(得分:0)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x,n;
cin>>x;
vector<int> v;
cout<<"Enter numbers:\n";
for(int i=0;i<x;i++)
{
cin>>n;
v.push_back(n);
}
//displaying vector contents
for(int p : v)
cout<<p<<" ";
}
在向量中获取输入的简单方法。
答案 18 :(得分:0)
这是我尝试过的两种方法。两者都很好用。
int main() {
int size,temp;
cin>>size;
vector<int> ar(size);
//method 1
for(auto i=0;i<size;i++)
{ cin>>temp;
ar.insert(ar.begin()+i,temp);
}
for (auto i:ar)
cout <<i<<" ";
//method 2
for(int i=0;i<size;i++)
{
cin>>ar[i];
}
for (auto i:ar)
cout <<i<<" ";
return 0;
}
答案 19 :(得分:0)
我遇到了类似的问题,这就是我的解决方法。正确使用和修改您的代码:
int main()
{
int input;
vector<int> V;
cout << "Enter your numbers to be evaluated: "
<< '\n' << "type "done" & keyboard Enter to stop entry"
<< '\n';
while ( (cin >> input) && input != "done") {
V.push_back(input);
}
write_vector(V);
return 0;
}