Push_back不起作用

Push_back doesn't work

本文关键字:不起作用 back Push      更新时间:2023-10-16

在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];
    }

感谢您抽出时间,如果这很明显的话,请道歉。

如果输入值

1 2 3 4

您需要以一个ctrl-z(或Unix中的<kbd]ctrl>-d)结束,它是eof才能停止输入。

代码对我来说运行良好(VS2012)

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变为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;
}

您最终需要输入一个0才能脱离循环。因此,1 3 4 0将导致1。但是,使用这种方法永远不能在向量中放入0。矢量中只有[1,3,4]。

首先,push_back没有任何问题。:)

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

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

你的程序对我有效。

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

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

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

#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 errorn";
            return 1;   // error return
        }
    }
    cout << "read " << v.size() << " elementsn";
    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

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

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

cout << myVec[0] << flush;

或者这个:

cout << myVec[0] << endl;