
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library - <initializer_list>
The <initializer_list> in C++ provides a convenient way to initialize containers and user-defined types with a list of values. It allows for more flexible and readable code by enabling the initialization of collections with a comma separated list of elements.
The <initializer_list> is a lightweight proxy object that allows a constructor to accept a list of values without explicitly specifying the type. It is used when working with standard library containers such as vector, set and map.
Including <initializer_list> Header
To include the <initializer_list> header in your C++ program, you can use the following syntax.
#include <initializer_list>
Functions of <initializer_list> Header
Below is list of all functions from <initializer_list> header.
Sr.No. | Functions & Description |
---|---|
1 | size It returns the number of elements in the initializer_list. |
2 | begin It returns a pointer to the first element. |
3 | end It return a pointer to one past the last element. |
Adding Values from an Initializer List
In the following example, we are going to calculate the sum of the elements from the initializer_list.
#include <iostream> #include <initializer_list> int a(std::initializer_list < int > list) { int b = 0; for (auto x: list) { b += x; } return b; } int main() { int y = a({2,4,6,10}); std::cout << "Result : " << y << std::endl; return 0; }
Output
Output of the above code is as follows −
Result : 22
Combining Two Initializer Lists
Consider the following example, where we are going ti combine values from multiple initializer lists.
#include <iostream> #include <initializer_list> #include <vector> std::vector < int > a(std::initializer_list < int > x, std::initializer_list < int > y) { std::vector < int > b; b.insert(b.end(), x); b.insert(b.end(), y); return b; } int main() { auto b = a({1,3,5}, {2,4,6}); for (auto value: b) { std::cout << value << " "; } std::cout << std::endl; return 0; }
Output
Following is the output of the above code −
1 3 5 2 4 6