Site icon Learn C++

What Is forward_list (std::forward_list) In Modern C++?

What Is forward list stdforward list In Modern C++

The C++ programming language has a lot of options to add and modify data members with their amazing data holders, arrays, structs, pointers and containers. Containers are powerful data storage arrays in C++ and they are very useful to iterate and search. A container is a holder object that stores data elements (a collection of data objects). std::vector, std::array, std::set, std::list are these kinds of containers. There is another useful container std::forward_list, in this post, we explain what std::forward_list is. Before that let’s remind ourselves what containers are in C++ programming and what are their types.

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. 

If you want to learn more about these each type, here are some basic details,

What is std::forward_list in modern C++?

The forward_list (std::forward_list) is a sequence container class template defined in <forward_list> header that supports forward iterations, and faster insert and delete operations. The std::forward_list does not invalidate pointers or references of its members when you use these insertion methods or deletion methods as in std::list and std::deque, while the std::vector does. They support only forward iterations and do not support bidirectional iterations, and because of this, they use less memory space than the std::list. If you do not need backward iterations in a list, then it is much better to use std::forward_list

In general, std::forward_list is defined as below.

[crayon-6767719371722707169801/]

Since C++17, std::forward_list is improved and defined in the pmr namespace as below.

[crayon-676771937172c374078636/]

Is there a simple std::forward_list example in modern C++?

Here is a simple std::forward_list declaration example in C++.

[crayon-676771937172e203840207/]

we can define its member in definition as below.

[crayon-676771937172f050715121/]

Or we can use push_front methods to add new members on runtime.

[crayon-6767719371730227702971/]

and there is no push_back method because of being forward list.

What is std::forward_list in modern C++?

Here is a full example about std::forward_list

[crayon-6767719371731896413377/]

and the output will be as follows,

[crayon-6767719371732997996122/]

For more new details, please check these papers, p0646r1 and n2543

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.

Exit mobile version