Containers are powerful data storage arrays in modern C++ and they are very useful to iterate and search data with their amazing methods and properties. A container is a holder object that stores data elements (a collection of data objects). std::vector
, std::array
, std::set
are these kinds of containers. There is another useful container, in this post, we explain what std::list is. Before that let’s remind ourselves what containers are in C++ programming and what are their types.
Table of Contents
What is a container in modern C++?
Containers are modern data storage arrays in modern C++ and they are very useful to iterate and search data with their amazing methods and properties.
A container is a holder object that stores data elements (a collection of data objects). They are implemented as a class template to define objects that can be used with modern rules of C++ (The rule of 6), they allow great flexibility in the different data types supported as elements, they can be used with int, float, double, etc. or with struct types, they can be used with other modern types of C++, lambdas and templates. Thus, the developer can create different data sets in memory, these can be static or dynamic, they are safe and optimized well.
What are the basic container types modern C++?
The C++ Standards library defines 4 container types.
- Sequence Containers ( vector, array, deque, list, forward_list )
- Associative Containers ( map, multimap, set, multiset )
- Unordered Associative Containers ( unordered_map, unordered_multimap, unordered_set, unordered_multiset )
- Container Adapters ( stack, queue, priority_queue )
If you want to learn more about these each type, here are some basic details,
What is std::list in modern C++?
The list
(std::list
) is a sequence container class template defined in <list> header that supports non-contiguous memory allocation. They are slower than the std::vector
but their insert and delete operations are faster. They do not support fast random access. They support bidirectional iteration, and they use much more memory space. They are easy to insert or delete elements from anywhere in the container.
In general, std::list is defined as below,
1 2 3 4 5 6 |
template < class T, class Allocator = std::allocator<T> > class list; |
Since C++17, std::list is improved and defined as below.
1 2 3 4 5 6 |
namespace pmr { template< class T > using list = std::list< T, std::pmr::polymorphic_allocator<T> >; } |
Is there a simple std::list example in modern C++?
Here is a simple std::list
declaration example in C++.
1 2 3 |
std::list<int> lst; |
we can define its member in definition as below.
1 2 3 |
std::list<int> lst = { 50, 100 }; |
Or we can use push_back
push_front
methods to add new members on runtime.
1 2 3 4 5 6 |
std::list<int> lst; lst.push_back(100); lst.push_front(50); |
What is std::list in modern C++?
Here is a full example about std::list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <list> int main() { std::list<int> lst; // List in integers lst.push_back(100); lst.push_back(200); lst.push_back(300); lst.push_front(50); for(auto L : lst) { std::cout << L << " , "; } std::cout << std::endl; system("pause"); return 0; } |
For more new details, you can check here, https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0646r1.pdf
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition