Push_back不起作用
Push_back doesn't work
在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
不会终止循环)。
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;
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 我正在开发服务器,ip作为参数传递不起作用