Print a std::list in C++
This post will discuss how to print a std::list
in C++.
Lists are sequence containers that are implemented as a doubly linked list and allow iteration in both directions and take O(1) time for insertion and deletion. They are usually slower than other standard sequence containers (arrays or vectors) to retrieve any item.
There are many ways to print a list in C++, which are covered below:
1. Using range-based for-loop
The recommended approach is to use the range-based for-loop to print elements in the list container. Please note that this will only work with C++11
, and later, with C++98
, we can use iterators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <list> void print(std::list<std::string> const &list) { for (auto const &i: list) { std::cout << i << std::endl; } } int main() { std::list<std::string> list = { "blue", "red", "green" }; print(list); return 0; } |
Output:
blue
red
green
2. Using std::copy
function
Another elegant solution is to use std::copy
to copy the list’s contents to the output stream (in this case std::cout
) with the help of the output iterator std::ostream_iterator
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <list> #include <algorithm> #include <iterator> void print(std::list<std::string> const &list) { std::copy(list.begin(), list.end(), std::ostream_iterator<std::string>(std::cout, "\n")); } int main() { std::list<std::string> list = { "blue", "red", "green" }; print(list); return 0; } |
Output:
blue
red
green
With C++17, we can use std::copy
with std::experimental::ostream_joiner which is defined in header <experimental/iterator>
. It is a single-pass output iterator which can write successive objects into the std::cout
, using the <<
operator, separated by a delimiter between every two objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <list> #include <experimental/iterator> void print(std::list<std::string> const &list) { std::copy(list.begin(), list.end(), std::experimental::make_ostream_joiner(std::cout, " ")); } int main() { std::list<std::string> list = { "blue", "red", "green" }; print(list); return 0; } |
Output:
blue
red
green
3. Using std::for_each
function
We can also use the std::for_each
STL algorithm, which accepts an input range defined by two iterators and applies a specified function on each element in that range. The specified function may be a unary function, or an object of a class overloading the ()
operator or a lambda expression.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <iostream> #include <list> #include <algorithm> void print(const int &i) { std::cout << i << std::endl; } struct myclass { void operator() (int i) { std::cout << i << std::endl; } } ob; void print_list(std::list<std::string> const &list) { // specify a lambda expression std::for_each(list.begin(), list.end(), [](const auto &e) { std::cout << e << std::endl; }); // or specify a function // std::for_each(list.begin(), list.end(), print); // or pass an object of a class overloading the ()operator // std::for_each(list.begin(), list.end(), ob); } int main() { std::list<std::string> list = { "blue", "red", "green" }; print_list(list); return 0; } |
Output:
blue
red
green
4. Using Iterator
We can also use the iterators to print a list. Since we’re not modifying the contents of the list inside the while loop, consider using the const_iterator
, which is returned by cbegin()
and cend()
. Before C++11, we can use begin()
and end()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <list> void print(std::list<std::string> const &list) { for (auto it = list.cbegin(); it != list.cend(); it++) { std::cout << *it << std::endl; } } int main() { std::list<std::string> list = { "blue", "red", "green" }; print(list); return 0; } |
Output:
blue
red
green
5. Overloading <<
Operator
The output streams (such as cout
) use the insertion (<<)
operator, which can be overloaded to accept a list object. We basically need to overload the <<
operator to recognize an ostream object on the left and a list object on the right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <list> std::ostream &operator<<(std::ostream &os, const std::list<std::string> &list) { for (auto const &i: list) { os << i << std::endl; } return os; } int main() { std::list<std::string> list = { "blue", "red", "green" }; std::cout << list; return 0; } |
Output:
blue
red
green
That's all about printing a std::list
in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)