13

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?

1
  • #include <list> 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: en.cppreference.com/w/cpp/container/list/push_back Commented Apr 26, 2013 at 6:20

5 Answers 5

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"; 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the very concise code. Can I ask if there is a name for this usage - writing auto v : mylist instead of auto itr = mylist.begin()? Thanks!
@yuqli: That's a "range based for loop".
Thanks - wasn't expecting such a quick reply under a five year old answer :-)
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, " ")); } 

3 Comments

+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.
If I've read well, according to stackoverflow.com/questions/3804183/… your proposed solution will add one-too-many separator at the end. This may or may not be an issue, just FYI.
@quetzalcoatl: That's a pretty well known problem. One fairly clean solution is to replace the ostream_iterator with an infix_ostream_iterator.
4

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.

Comments

2

You use an Iterator.

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

Comments

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; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.