将任意数量的用户输入的整数存储到数组中

时间:2015-11-20 03:02:10

标签: c++ arrays cin

假设我有一些事情:

#include <iostream>
#include <cstdlib>   
#include <vector>
using namespace std;
const int MAX_EDGES = 4;
... 
int main() {
...
int edges[4];
cout << "Enter the outward-edges for node " << i+1 << ":" << endl;
cin >> edges[0] >> edges[1] >> edges[2] >> edges[3]; 
... 
} 

但是,我处于这样一种情况,即每个节点(在上面某处定义为结构)可能没有4&#34;向外边缘&#34;。因此,我希望用户只需输入此节点所连接的那些边,而不必输入-1或其他一些不表示外边缘的边。例如,现在我们可能有输入

2
3
-1 
-1 

表示节点i + 1(我们此刻在for循环中)有两个向外边缘,即节点2和3。 但是我希望能够只做2 3

任何帮助表示赞赏!干杯。

0 个答案:

没有答案