0

在 Visual 2010、Visual 2012 和 CodeBlocks 上尝试过,但没有成功。在过去的 3 天里,我一直在互联网上寻找答案,但没有找到任何可以帮助我的东西。

我尝试了两种方法,第一种

    vector<int> mvec;
    int num;
    while(cin >> num)
       mvec.push_back(num);
    cout << mvec[0];

应该输出任何输入的第一个数字。相反,它什么也不做。如果我在输入数字序列之后或后面输入一个字母,它会打印第一个数字。

这个也试过

    vector<int> myvector;
    int myint;

    do {
      cin >> myint;
      myvector.push_back(myint);
    } while (myint);

    cout << myvector[0];

再次,什么都没有。我在谷歌搜索时发现了最后一个片段,它显然适用于它的创建者。

一些在线编译器告诉我,例如 1 3 4 的输出是 1。我发现一个说程序正在尝试打印一个空向量

至少有人可以尝试运行其中任何一个并告诉我它们是否有效?我将在下面包含一个完整的程序。

    #include <iostream>
    #include <vector>

    int main()
    {
        using namespace std;
        vector<int> mvec;
        int num = 0;
        while(cin >> num)
            mvec.push_back(num);
        cout << mvec[0];
    }

感谢您的时间,如果这很明显,我们深表歉意。

4

6 回答 6

4

如果输入值

1 2 3 4

您需要以 eof 结尾的ctrl- z(或ctrl-d在 Unix 中)以停止输入。

我的代码工作正常(VS2012)

于 2013-08-01T15:19:11.247 回答
1
vector<int> mvec;
int num;
while(cin >> num)
   mvec.push_back(num);
cout << mvec[0];

只要 cin >> num 成功,循环就会继续。换句话说,只有输入一个非数字,循环才会结束,然后打印出第一个数字。

vector<int> myvector;
int myint;

do {
  cin >> myint;
  myvector.push_back(myint);
} while (myint);

cout << myvector[0];

当使用预期为 bool 的整数时,值 0 变为 false,任何其他值变为 true。这个循环只会在输入 0 时停止,因为这会导致 myint 变为假。

我不确定你到底想做什么。如果您只想输入一个数字,请不要使用循环(并且不需要向量)如果您想输入一定数量的数字,您将需要一个执行一定次数的 for 循环。

int num;
cout << "How many numbers do you want to enter?" << endl;
if (! (cin >> num))
{
    cout << "Error expected a number" << endl;
    return -1;
}

vector<int> vec;
for (int i = 0; i < num; i++)
{
    int x;
    if (! (cin >> x))
        {
        cout << "Error expected a number" << endl;
        return -1;
    }
    vec.push_back(x);
}

for (int i = 0; i < vec.size(); i++)
{
    cout << "number entered: " << vec[i] << endl;
}
于 2013-08-01T15:33:32.020 回答
0

您最终需要输入 0 才能跳出循环。所以 1 3 4 0 会得到 1。但是你永远不能使用这种方法在向量中输入 0。向量中只有 [1, 3, 4]。

于 2013-08-01T15:06:18.167 回答
0

首先要做的事情- 没有任何问题push_back。:)

您希望如何终止while( cin >> num )循环?看起来您正在使用 Windows,因此<Ctrl-Z>会导致cin无效,因此终止 while 循环。在类似 Un*x 的系统上,它是<Ctrl-D>.

输入字母终止循环的原因cin是试图将结果放入数字变量中,并且无法将字符串解析为数字。

你的程序对我有用。

(注意:进入0不会终止循环)。

于 2013-08-01T15:19:39.153 回答
0

Bjarne Stroustrup 在他的网站上有一个 std::vector 和 push_back 的例子:

http://www.stroustrup.com/bs_faq2.html#simple-program

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
    vector<double> v;

    double d;
    while(cin>>d) v.push_back(d);   // read elements
    if (!cin.eof()) {       // check if input failed
        cin.clear();        // clear error state
        string s;
        cin >> s;       // look for terminator string
        if (s != "end") {
            cerr << "format error\n";
            return 1;   // error return
        }
    }

    cout << "read " << v.size() << " elements\n";

    reverse(v.begin(),v.end());
    cout << "elements in reverse order:\n";
    for (int i = 0; i<v.size(); ++i) cout << v[i] << '\n';

    return 0; // success return
}

您只需输入以空格分隔的数字,完成后输入end

它对初学者有好处,因为它不依赖于不同操作系统的可变文件结尾字符。

于 2013-08-01T15:53:52.800 回答
-1

您的标准输出缓冲区可能没有刷新。尝试这个:

cout << myVec[0] << flush;

或这个:

cout << myVec[0] << endl;
于 2013-08-01T15:20:54.453 回答