12

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?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Drew L. Facchiano
  • 283
  • 3
  • 5
  • 12
  • `#include ` alone doesn't create any list. It only includes the relevant header file, but you still need to put code in place for creating, modifying, printing etc. the list. cppreference.com has example code for all kinds of standard containers, e.g. here for `std::list`: http://en.cppreference.com/w/cpp/container/list/push_back – jogojapan Apr 26 '13 at 06:20

5 Answers5

24

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:

#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";
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
9

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, " "));
}
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • 2
    +1, but it would be a good answer if you explained some of this. I doubt OP is in a position to grasp what is going on here. – juanchopanza Apr 26 '13 at 06:34
  • If I've read well, according to http://stackoverflow.com/questions/3804183/how-to-nicely-output-a-list-of-separated-strings your proposed solution will add one-too-many separator at the end. This may or may not be an issue, just FYI. – quetzalcoatl Apr 26 '13 at 10:22
  • 1
    @quetzalcoatl: That's a pretty well known problem. One fairly clean solution is to replace the `ostream_iterator` with an [`infix_ostream_iterator`](http://codereview.stackexchange.com/questions/13176/infix-iterator-code). – Jerry Coffin Apr 26 '13 at 13:08
3

For instance, for a list of 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.

john
  • 85,011
  • 4
  • 57
  • 81
2

You use an Iterator.

for(list<type>::iterator iter = list.begin(); iter != list.end(); iter++){
   cout<<*iter<<endl;
}
Brandon Ling
  • 3,841
  • 6
  • 34
  • 49
1

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

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

for (list<int>::const_iterator it = var.begin(); it != var.end(); ++it)
    cout << *it << endl;
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74