C++ 如何在向量c ++中打印元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14639782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print elements in a vector c++
提问by TheNameHobbs
I am having trouble with my void print function to print out this vector. I'm not quite sure what it is talking about with "std::allocator. I get these errors:
我的无效打印功能无法打印出这个向量。我不太确定它在谈论什么“std::allocator。我收到这些错误:
st1.cpp: In function ‘void Print(std::vector<int, std::allocator<int> >)':
st1.cpp:51: error: declaration of ‘std::vector<int, std::allocator<int> > v' shadows a parameter
Here is the file:
这是文件:
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
void Initialize();
void Print();
int main()
{
stack<string> s1, s2;
s1.push("b");
s2.push("a");
if (s1.top() == s2.top())
{
cout << "s1 == s2" << endl;
}
else if (s1.top() < s2.top())
{
cout << "s1 < s2" << endl;
}
else if (s2.top() < s1.top())
{
cout << "s2 < s1" << endl;
}
else
{
return 0;
}
vector<int> v;
Initialize();
Print();
}
void Initialize(vector<int> v)
{
int input;
cout << "Enter your numbers to be evaluated: " << endl;
while(input != -1){
cin >> input;
v.push_back(input);
//write_vector(v);
}
}
void Print (vector<int> v){
vector<int> v;
for (int i=0; i<v.size();i++){
cout << v[i] << endl;
}
}
I just want to print v out to the screen. Any help?
我只想将 v 打印到屏幕上。有什么帮助吗?
采纳答案by billz
Your function declaration and definition are not consistent, you want to generate vector from Initialize
, you can do:
你的函数声明和定义不一致,你想从 生成向量Initialize
,你可以这样做:
void Initialize(vector<int>& v);
To print vector:
打印矢量:
void Print(const vector<int>& v);
Now you call:
现在你打电话:
vector<int> v;
Initialize(v);
Print(v);
Don't forget to change function definition of Initialize
, Print
to match the new signature I provided above.
Also you are redefining a local variable v
which shadows function parameter, you just need to comment out that line, also pass vector by const ref:
不要忘了更改的函数定义Initialize
,Print
以符合我上面提供的新的签名。此外,您正在重新定义一个v
隐藏函数参数的局部变量,您只需要注释掉该行,并通过 const ref 传递向量:
void Print (const vector<int>& v){
//vector<int> v;
for (int i=0; i<v.size();i++){
cout << v[i] << endl;
}
}
回答by Rapptz
You have to pass by const reference and remove the extraneous vector.
您必须通过 const 引用并删除无关向量。
void Print(const std::vector<int>& v){
for(unsigned i = 0; i< v.size(); ++i) {
std::cout << v[i] << std::endl;
}
}
Another way to print it out would be to use iterators like so:
另一种打印出来的方法是使用迭代器,如下所示:
void Print(const std::vector<int>& v) {
std::vector<int>::iterator it;
for(it = v.begin(); it != v.end(); ++it) {
std::cout << (*it) << '\n';
}
}
Or in C++11 you can do it like so:
或者在 C++11 中,你可以这样做:
void Print(const std::vector<int>& v) {
for(auto& i : v)
std::cout << i << '\n';
}
I don't think your Initialize()
function works like you expect it to. It seems to just make a copy and then discards it, not modifying any values of the existing vector.
我认为您的Initialize()
功能不像您期望的那样工作。它似乎只是制作一个副本然后丢弃它,而不是修改现有向量的任何值。