注册 登录
编程论坛 C++教室

新手求助~~多个vector对象的建立

ppt060984 发布于 2008-11-23 21:01, 1197 次点击
我要读入两个int型vector对象ivec1和ivec2
程序如下:
#include<iostream>
#include<vector>

using namespace std;

int main()
{
    //读入两个int型的vector对象
    int input1, input2;
    vector<int> ivec1, ivec2;

    cout << "Enter an int-type vector object:" << endl
        <<"ivec1 = "<< endl;
    while (cin >> input1)
        ivec1.push_back (input1);
    cout << endl;

    cout << "Enter another int-type vector object:" << endl
        <<"ivec2 = "<< endl;
    while (cin >> input2)
        ivec2.push_back (input2);
    cout << endl;
      
       reutrn 0;
}

可是怎么不对呢,只能读入ivec1,不能读入ivec2,直接就跳过去了。
8 回复
#2
zxwangyun2008-11-23 21:26
reutrn 0;
这样也能编译通过?
#3
zxwangyun2008-11-23 21:28
reutrn 0;
这样也能编译通过?
#4
zxwangyun2008-11-23 21:31
不好意思!
网速不行
多刷新了一次
#5
ppt0609842008-11-23 21:33
回复 第4楼 zxwangyun 的帖子
可以啊,我用的是VS2005
#6
ppt0609842008-11-23 21:39
这是C++ primer 上的一个题目的前一小部分。
我看了答案,把cin语句改为
cin >> input1;
while(input1 != 32767) // 答案给定了一个输入范围
{
  ivec1.push_back (input1);
  cin >> input1;
}

cin >> input2;
while(input2 != 32767)
{
  ivec1.push_back (input2);
  cin >> input2;
}

情况还一样,不知道怎么回事.....
#7
newyj2008-11-23 23:19
在两个循环 中间 加上cin.clear();
#8
aipb20072008-11-24 00:12
while (cin >> input2)
不是跳过去,而是这句判断为假。在第一次输入流退出循环时,流的状态已经变了,要清除流标记,最好看看msdn。
#9
ppt0609842008-11-24 23:48
可以了,谢谢楼上两位!!!
1