C++C++11C++14C++17Introduction to C++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.

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).

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

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

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

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.

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

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

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.

How To Learn initializer_list  (std::initializer_list) In Modern C++. The C++ Builder Logo

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.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

C++C++11C++14C++17C++20Learn C++

Typical Declaration Of A Copy Assignment Operator Without std::swap