对于int数组,C ++中的gets()相当于什么? [英] What is the equivalent of gets() in C++ for int array ?

查看:299
本文介绍了对于int数组,C ++中的gets()相当于什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想接受一个变量int数组..通常我会声明一个char数组并使用gets输入数组但它不适用于int数组,即使我可以使用char数组输入int但仍然存在任何选择..谢谢



我的尝试:



i有搜索互联网但无法找到答案。

i want to take in a variable int array..normally i would declare a char array and use gets to input the array but it wont work for int array even though i can use the char array for inputting int but still is there any alternative..thanx

What I have tried:

i have searched internet but couldn't find the answer.

推荐答案

正如所建议的,您的代码必须执行循环。

我会使用 vector ,而不是数组,例如

As suggested, your code have to perform a loop.
I would use a vector, instead of an array, e.g.
 #include <iostream>
 #include <vector>
 using namespace std;

int main()
{
  vector<int> v;

  for (;;)
  {
    int i;
    cin >> i;
    if ( cin.eof()) break;
    v.push_back(i);
  }


  for (const auto & x : v)
    cout << x << " ";
  cout << endl;
}


这篇关于对于int数组,C ++中的gets()相当于什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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