Push_back不起作用

时间:2013-08-01 15:02:02

标签: c++ vector push-back

尝试在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];

再一次,没有。我在google搜索时找到了最后一段代码,它显然对它的创建者有效。

一些在线编译器告诉我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];
    }

感谢您的时间和道歉,如果这是非常明显的。

6 个答案:

答案 0 :(得分:4)

如果输入值

1 2 3 4

你需要以 ctrl - z (或Unix中的 ctrl - d )结束eof以便停止输入。

代码可以正常使用(VS2012)

答案 1 :(得分:1)

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

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

vector<int> myvector;
int myint;

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

cout << myvector[0];

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

我不确定你到底想做什么。如果您只想输入一个数字,请不要使用循环(并且不需要向量)如果要输入一定数量的数字,则需要一个执行设定次数的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;
}

答案 2 :(得分:0)

你最终需要输入一个0才能摆脱循环。所以1 3 4 0会导致1.但是你永远不能使用这种方法在向量中加0。向量中只有[1,3,4]。

答案 3 :(得分:0)

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

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

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

您的计划适合我。

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

答案 4 :(得分: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
}

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

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

答案 5 :(得分:-1)

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

cout << myVec[0] << flush;

或者这个:

cout << myVec[0] << endl;