C++ 向后推不';行不通

C++ 向后推不';行不通,c++,vector,push-back,C++,Vector,Push Back,在Visual 2010、Visual 2012和代码块上试用,但未成功。在过去的三天里,我一直在网上漫游寻找答案,但没有找到任何帮助 我试过两种方法,第一种 vector<int> mvec; int num; while(cin >> num) mvec.push_back(num); cout << mvec[0]; 向量mvec; int-num; while(cin>>num) mvec.推回(num)

在Visual 2010、Visual 2012和代码块上试用,但未成功。在过去的三天里,我一直在网上漫游寻找答案,但没有找到任何帮助

我试过两种方法,第一种

    vector<int> mvec;
    int num;
    while(cin >> num)
       mvec.push_back(num);
    cout << mvec[0];
向量mvec;
int-num;
while(cin>>num)
mvec.推回(num);
cout>myint;
myvector.push_back(myint);
}while(myint);
cout>num)
mvec.推回(num);

cout您最终需要输入一个0来打破循环。因此1 3 4 0将导致1。但是你不能用这种方法在向量中加0。向量中只有[1,3,4]个字符。

最终需要输入0才能跳出循环。因此1 3 4 0将导致1。但是你不能用这种方法在向量中加0。向量中只有[1,3,4]个值。

如果输入值

1 2 3 4
您需要以ctrl-z(或Unix中的ctrl-d)结束,它是eof,以便停止输入

如果您输入值,我(VS2012)可以很好地运行代码。

1 2 3 4
您需要以ctrl-z(或Unix中的ctrl-d)结束,它是eof,以便停止输入


代码对我来说很好(VS2012)

第一件事优先-向后推(push_)没有什么问题。

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

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

你的程序对我有用


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

首先要做的事情-将
推回没有什么问题。
:)

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

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

你的程序对我有用


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

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

cout << myVec[0] << flush;

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

cout << myVec[0] << flush;
cout
向量mvec;
int-num;
while(cin>>num)
mvec.推回(num);
cout>num成功。换句话说,只有输入一个非数字,循环才会结束,然后打印出第一个数字

vector<int> myvector;
int myint;

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

cout << myvector[0];
向量myvector;
int-myint;
做{
cin>>myint;
myvector.push_back(myint);
}while(myint);
cout
向量mvec;
int-num;
while(cin>>num)
mvec.推回(num);
cout>num成功。换句话说,只有输入一个非数字,循环才会结束,然后打印出第一个数字

vector<int> myvector;
int myint;

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

cout << myvector[0];
向量myvector;
int-myint;
做{
cin>>myint;
myvector.push_back(myint);
}while(myint);

库特Bjarne Stroustrup有一个std::vector的例子,并将其推回到他的网站上:

#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
向量v;
双d;
while(cin>>d)v.push_back(d);//读取元素
如果(!cin.eof()){//检查输入是否失败
cin.clear();//清除错误状态
字符串s;
cin>>s;//查找终止符字符串
如果(s!=“结束”){

cerrBjarne Stroustrup在他的网站上有一个std::vector的例子:

#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
向量v;
双d;
while(cin>>d)v.push_back(d);//读取元素
如果(!cin.eof()){//检查输入是否失败
cin.clear();//清除错误状态
字符串s;
cin>>s;//查找终止符字符串
如果(s!=“结束”){

cerr您遇到的问题是循环永远不会退出。这是您需要做出的设计决策。您如何知道用户何时完成输入?找到答案,然后应该很容易解决。您遇到的问题是循环永远不会退出。这是您需要做出的设计决策。您如何知道用户何时完成输入输入完毕吗?找出答案,然后它应该很容易修复。这适用于问题中的第二个代码段(带有
do
while(myint)
)这适用于问题中的第二个代码段(带有
do
while(myint)
std::cout
main
返回时将刷新
std::cout
main
返回时将刷新。
vector<int> myvector;
int myint;

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

cout << myvector[0];
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;
}
#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
}