从 C++ 列表库中打印出列表的内容

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

Printing out contents of a list from the c++ list library

c++list

提问by Drew L. Facchiano

I wanted to print out the contents of a list for a simple program I'm writing. I'm using the built in list library

我想打印出我正在编写的一个简单程序的列表内容。我正在使用内置列表库

#include <list>

however, I've no idea how to print out the contents of this list in order to test/check the data within. How do I do this?

但是,我不知道如何打印此列表的内容以测试/检查其中的数据。我该怎么做呢?

回答by Jerry Coffin

If you have a recent compiler (one that includes at least a few C++11 features), you can avoid dealing with iterators (directly) if you want. For a list of "small" things like ints, you might do something like this:

如果您有最新的编译器(至少包含一些 C++11 功能的编译器),则可以根据需要避免(直接)处理迭代器。对于像ints这样的“小”事物列表,您可以执行以下操作:

#include <list>
#include <iostream>

int main() {
    list<int>  mylist = {0, 1, 2, 3, 4};

    for (auto v : mylist)
        std::cout << v << "\n";
}

If the items in the list are larger (specifically, large enough that you'd prefer to avoid copying them), you'd want to use a reference instead of a value in the loop:

如果列表中的项目较大(具体来说,足够大以至于您希望避免复制它们),您需要在循环中使用引用而不是值:

    for (auto const &v : mylist)
        std::cout << v << "\n";

回答by Martin York

Try:

尝试:

#include <list>
#include <algorithm>
#include <iterator>
#include <iostream>

int main()
{
    list<int>  l = {1,2,3,4};

    // std::copy copies items using iterators.
    //     The first two define the source iterators [begin,end). In this case from the list.
    //     The last iterator defines the destination where the data will be copied too
    std::copy(std::begin(l), std::end(l),

           // In this case the destination iterator is a fancy output iterator
           // It treats a stream (in this case std::cout) as a place it can put values
           // So you effectively copy stuff to the output stream.
              std::ostream_iterator<int>(std::cout, " "));
}

回答by john

For instance, for a list of int

例如,对于一个 int 列表

list<int> lst = ...;
for (list<int>::iterator i = lst.begin(); i != lst.end(); ++i)
    cout << *i << endl;

If you are working with list you better get used to iterators pretty quickly.

如果您正在使用列表,您最好很快就习惯迭代器。

回答by Brandon Ling

You use an Iterator.

您使用迭代器。

for(list<type>::iterator iter = list.begin(); iter != list.end(); iter++){
   cout<<*iter<<endl;
}

回答by Captain Obvlious

You can use iterators and a small forloop for this. Since you are simply outputting the values in the list you should use const_iteratorrather than iteratorto prevent accidentally modifying the object referenced by the iterator.

您可以for为此使用迭代器和一个小循环。由于您只是输出列表中的值,因此您应该使用const_iterator而不是iterator防止意外修改迭代器引用的对象。

Here is an example of how to iterate through variable varthat is a list of int's

这是一个如何迭代变量的示例,该变量varint's的列表

for (list<int>::const_iterator it = var.begin(); it != var.end(); ++it)
    cout << *it << endl;