C++ list
last modified September 7, 2022
In this article we show how to work with a list container in C++.
A list is a container which holds a sequence of elements. The elements have a specific type. A list allows efficient insertions and deletions at any location within the sequence.
A list does not have a random access operator; that is, it is not possible to
get a value from a list via an index operator []. (Vectors can be
used for random access of elements.)
C++ list size
The size of a list is determined with size.
#include <iostream>
#include <list>
using std::cout;
using std::list;
int main()
{
list<int> vals({1, 2, 3, 4, 5, 6, 7});
cout << "The list has " << vals.size() << " elements\n";
}
We initialize a list of integers and print its size.
list<int> vals({1, 2, 3, 4, 5, 6, 7});
We create a list of integer values with a list initializer. The type of
the list is specified between
<> brackets.
cout << "The list has " << vals.size() << " elements\n";
We print a message that says how many elements are in the list.
$ ./main The list has 7 elements
C++ map insert elements
The following example inserts new values.
#include <iostream>
#include <list>
using std::cout;
using std::endl;
using std::list;
using std::string;
int main()
{
list<string> words = {"sky", "car", "cup", "ocean", "war"};
list<string>::iterator it = words.begin();
words.insert(it, "falcon");
words.push_back("world");
words.push_front("water");
for (auto word : words)
{
cout << word << endl;
}
}
We have a list of words.
list<string>::iterator it = words.begin(); words.insert(it, "falcon");
We create an iterator which points to the beginning of a list. We insert a new element there.
words.push_back("world");
words.push_front("water");
We add a new element at the back with push_back and at the start
with push_front.
$ ./main water falcon sky car cup ocean war world
C++ list remove elements
The following example removes list elements.
#include <iostream>
#include <list>
using std::cout;
using std::endl;
using std::list;
using std::string;
int main()
{
list<string> words = {"sky", "car", "cup", "ocean", "war"};
words.remove("war");
for (auto word : words)
{
cout << word << endl;
}
words.clear();
cout << words.size() << endl;
}
We remove elements with remove and clear.
words.remove("war");
We remove the specified element with remove.
words.clear();
All elements are removed with clear.
$ ./main sky car cup ocean 0
C++ loop list
The following example loops over a list.
#include <iostream>
#include <list>
using std::cout;
using std::endl;
using std::list;
int main()
{
list<int> vals = {1, 2, 3, 4, 5, 6, 7};
for (auto it = vals.cbegin(); it != vals.cend(); ++it)
{
std::cout << *it << endl;
}
for (auto val : vals)
{
cout << val << endl;
}
}
We loop over a list of integers with a class for loop and a foreach loop.
for (auto it = vals.cbegin(); it != vals.cend(); ++it)
{
std::cout << *it << endl;
}
The list is iterated with a classic for loop. The cbegin returns a
constant iterator pointing to the first element in the container. The
cend returns constant iterator pointing to the past-the-end element
in the container.
for (auto val : vals)
{
cout << val << endl;
}
A foreach loop is an easy syntax for looping over containers.
$ ./main 1 2 3 4 5 6 7 1 2 3 4 5 6 7
C++ list remove_if
The remove_if function removes all elements that satisfy the given
predicate.
#include <iostream>
#include <string>
#include <list>
using std::cout;
using std::endl;
using std::list;
using std::string;
int main()
{
list<string> words = {"sky", "car", "cup", "ocean", "war"};
words.remove_if([](const string& e) {
return (e.starts_with("w"));
});
for (auto word : words)
{
cout << word << endl;
}
}
In the example, we remove all words from the list that start with "w".
$ clang++ main.cpp -o main -std=c++20 $ ./main sky car cup ocean
In this article, we have worked with the list container in C++.