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++.
Table of Contents
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.
1 2 3 |
std::vector<object type> variable_name; |
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).
1 2 3 |
template< class T > class initializer_list; |
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
1 2 3 |
std::vector<int> vec= {10,20,30,40,50}; // initialized with initializer_list (C++11 and above) |
or you can use initializer_list
to define array types as below.
1 2 3 |
std::initializer_list<int> scores = { 90, 80, 75, 90, 85 }; |
or we can use same for string types, etc. as below.
1 2 3 |
std::initializer_list<std::string> my_data2 = {"Jim","David","Ian","Yilmaz"}; |
Is there a template example of how to use std::initializer_list in modern C++?
A 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
template <class T> class mytemp { private: std::vector<T> vec; public: mytemp(std::initializer_list<T> n) : vec(n) { std::cout << "element size:" << n.size() << std::endl; } }; mytemp<int> obj = {10,20,30,40}; |
Is there a full example of std::initializer_list in modern C++?
Here is a full example of std::initializer_list
in modern C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> #include <vector> template <class T> class mytemp { private: std::vector<T> vec; public: mytemp(std::initializer_list<T> n) : vec(n) { std::cout << "element size:" << n.size() << std::endl; } }; int main() { std::vector<int> vec= {10,20,30,40,50}; std::initializer_list<int> scores = {90,80,75,90,85}; std::initializer_list<std::string> my_data2 = {"Jim","David","Ian","Yilmaz"}; mytemp<int> obj = {10,20,30,40}; system("pause"); return 0; } |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition