C++ 如何打印向量c+中的元素+;

C++ 如何打印向量c+中的元素+;,c++,arrays,vector,C++,Arrays,Vector,我的void print函数无法打印出这个向量。我不太清楚“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

我的void print函数无法打印出这个向量。我不太清楚“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
st1.cpp:在函数“void Print(std::vector)”中:
st1.cpp:51:错误:“std::vector v”的声明隐藏了一个参数
文件如下:

#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;
}
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void初始化();
作废打印();
int main()
{
堆栈s1、s2;
s1.推动(“b”);
s2.推动(“a”);
如果(s1.top()==s2.top())
{

cout您必须通过const引用并删除无关向量

void Print(const std::vector<int>& v){
    for(unsigned i = 0; i< v.size(); ++i) {
        std::cout << v[i] << std::endl;
    }
}
void打印(const std::vector&v){
for(无符号i=0;istd::cout您的函数声明和定义不一致,您想从
初始化
生成向量,您可以执行以下操作:

void Initialize(vector<int>& v);

我如何从main调用它?void Print(vector v)?你也看过编译器指向你的位置了吗?他不必通过const引用。是的,但这是一个很好的const正确性建议,因为他没有修改函数中的任何内容。
void Print(const std::vector<int>& v) {
    for(auto& i : v)
        std::cout << i << '\n';
}
void Initialize(vector<int>& v);
void Print(const vector<int>& v);
vector<int> v;
Initialize(v);
Print(v);
void Print (const vector<int>& v){
  //vector<int> v;
  for (int i=0; i<v.size();i++){
    cout << v[i] << endl;
  }
}