Site icon Learn C++

How To Learn initializer_list (std::initializer_list) In Modern C++

How To Learn initializer list stdinitializer list In Modern C++

C++ has some really great features for being able to define different types of variables and structure. One of the most used and very useful arrays in C++ is the std::vector. If you know arrays in C and C++, then containers are a modern and very flexible form of arrays in C++. If you want to initialize containers like vector, list, map, etc. you need use std::initializer_list. It can be used with an appropriate C++ tool that compiler supports the C++11 standard and above like C++14, or C++17, etc. It is also very useful with templates and in this article, we describe the std::initializer_list.

In C++, if you have a lot of same parameters i.e. ages of each user) you can use data blocks to hold these data, for example you can use C arrays and structs , or modern containers of C++ vector (std::vector). array (std::array), list, map, etc.. std::vector is one of the most commonly used modern containers in C++.

What is a vector in modern C++?

Vectors (std::vector) are dynamic arrays included in <vector> library. They can resize themselves automatically when a member of a vector is inserted or deleted. Vectors are the same as dynamic arrays and these dynamic arrays of vectors are handled automatically by the container. Vectors are the way of Modern C++; their members are placed in the contiguous memory storage; thus they can be resized and can be accessed or traversed using iterators.

[crayon-671202992261c729867052/]

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

The std::initializer_list is used to make initialization of modern C++ containers (like vectors, lists, maps) and it is introduced in C++11. The method std::initializer_list<T> is to define a T type object which is a proxy object that provides access to an array of objects of type T

Here is the syntax for std::initializer_list (since C++11).

[crayon-6712029922625438884746/]

Is there a simple example of how to use std::initializer_list in modern C++?

For example, when we declare a vector with its members, it uses initializer_list

[crayon-6712029922627924936719/]

or you can use initializer_list to define array types as below.

[crayon-6712029922629333122956/]

or we can use same for string types, etc. as below.

[crayon-671202992262d130306554/]

Is there a template example of how to use std::initializer_list in modern C++?

template is a very powerful statement in C++ which simply defines the operations of a class, a function, an alias or a variable and lets the user apply the same template on different data types in those template operations. Templates are similar to macros in C++ except the compiler checks the types used before the template is expanded. initializer_list can be used in templates as below,

In an example, intilializer_list can be used in a class temple as below.

[crayon-671202992262e216241722/]

Is there a full example of std::initializer_list in modern C++?

Here is a full example of std::initializer_list in modern C++.

[crayon-6712029922630275482507/]

As you see initializer_list is a very lightweight feature for the containers of modern C++. It can be used with list, array, map and other containers too.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. 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 cross-platform 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