在 C++ 中将整数输入写入向量容器 [英] writing integer input to vector container in C++

查看:29
本文介绍了在 C++ 中将整数输入写入向量容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同样我们在数组中做

for (.....)
  cin>>a[i];

我们如何使用向量来做到这一点.我声明了一个整数向量

how we can do this using vectors. i declared a vector of integers

vector<int> v;

现在我需要从控制台获取输入并将它们添加到向量中.我正在使用向量,因为我不知道限制.

now i need to take inputs from console and add append those in vector.i am using vector because i donot know the limit.

推荐答案

从控制台向向量插入整数并打印所有内容:

To insert integer to a vector from console and print everything out:

int input;
vector<int> v;
while(cin >> input){
 v.push_back(input);
}

for(int i = 0; i<v.size(); i++){
 cout<< v[i] <<endl;
}

并且矢量还提供您打印出最大尺寸:

And vector also provides you to print out the max size with:

cout << v.max_size()<<endl;

这篇关于在 C++ 中将整数输入写入向量容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆