在 C++ 中显示矢量容器的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9751932/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:15:44  来源:igfitidea点击:

Displaying contents of a vector container in C++

c++stlvector

提问by heapuser

The following is a C++ program using STL vector container. Just wanted to know why the display() function is not printing the vector contents to the screen. If displaying the size() line is commented out, display() function works fine.

下面是一个使用 STL 向量容器的 C++ 程序。只是想知道为什么 display() 函数没有将矢量内容打印到屏幕上。如果显示 size() 行被注释掉,则 display() 函数工作正常。

#include <iostream>
#include <vector>

using namespace std;

void display(vector<int> &v)
{
    for(int i; i<v.size(); i++)
    {
        cout << v[i] << " ";
    }
    cout << "\n" << endl;
}

int main()
{
    vector<int> v;
    cout << "Size of Vector=" << v.size() << endl;

    //Putting values into the vector
    int x;
    cout << "Enter five integer values" << endl;
    for(int i; i<5; i++)
    {
        cin >> x;
        v.push_back(x);
    }
    //Size after adding values
    cout << "Size of Vector=" << v.size() << endl;

    //Display the contents of vector
    display(v);

    v.push_back(6);

    //Size after adding values
    cout << "Size of Vector=" << v.size() << endl;

    //Display the contents of vector
    display(v);

}

Output:

输出:

Size of Vector=0

Enter five integer values

1

2

3

4

5

Size of Vector=5


Size of Vector=6

回答by 111111

There is an idiomatic way for printing a vector out.

有一种惯用的方式打印矢量。

#include <algorithm>
#include <iterator>

//note the const
void display_vector(const vector<int> &v)
{
    std::copy(v.begin(), v.end(),
        std::ostream_iterator<int>(std::cout, " "));
}

This way is safe and doesn't require you to keep track of the vectors size or anything like that. It is also easily recognisable to other C++ developers.

这种方式是安全的,不需要您跟踪向量大小或类似的东西。它也很容易被其他 C++ 开发人员识别。

This method works on other container types too that do not allow random access.

此方法也适用于不允许随机访问的其他容器类型。

std::list<int> l;
//use l

std::copy(l.begin(), l.end(),
          std::ostream_iterator<int>(std::cout, " "));

This works both ways with input too consider the following:

这适用于输入的两种方式也请考虑以下事项:

#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    std::cout << "Enter int end with q" << std::endl;
    std::vector<int> v; //a deque is probably better TBH
    std::copy(std::istream_iterator<int>(std::cin),
              std::istream_iterator<int>(),
              std::back_inserter<int>(v));

    std::copy(v.begin(), v.end(),
              std::ostream_iterator<int>(std::cout, " "));
}

This version doesn't require any hard coding of size or manual management of the actual elements.

此版本不需要任何大小的硬编码或实际元素的手动管理。

回答by YXD

You are not initializing your variables. for(int i = 0;not for(int i;

您没有初始化变量。for(int i = 0;不是for(int i;

回答by jjf

I think this is the easiest way to go:

我认为这是最简单的方法:

#include <iostream>
#include <vector>

using namespace std;

int main(){
    vector<int> v;
    int x;
    cout << "Enter five integer values" << endl;
    for(int i=0; i<5; i++)
    {
        cin >> x;
        v.push_back(x);
    }

    for (int i = 0; i < (int)v.size(); i++)
        cout<< v.at(i) <<endl;

}

回答by Spandyie

I have found printing using for_each() very easy to understand and intuitive

我发现使用 for_each() 打印非常容易理解和直观

#include<vector>
#include<algorithm>
using namespace std;

main(){
vector<int> foo_bar{1,2,3,4,5};
auto print_array = [](const auto& o) {cout << o << " "; };
for_each(foo_bar.begin(), foo_bar.end(), print_array);
}

回答by Nahid Hasan

If you use compiler versions g++ 11 or more than then you simply use:

如果您使用编译器版本 g++ 11 或更高版本,则只需使用:

#include <iostream>
#include <vector>

using namespace std;

int main(){
   vector<int> v;
   int x;
   cout << "Enter five integer values" << endl;
   for(int i=0; i<5; i++)
   {
        cin >> x;
        v.push_back(x);
   }

   for (auto i: v)
      cout<< i <<endl;

}