- Examples
This lesson presents two more examples of uniform initialization.
We'll cover the following...
{}
with different types and structures #
Press + to interact
#include <unordered_map>#include <string>#include <vector>struct MyStruct{int x;double y;};class MyClass{public:int x;double y;};struct Telephone{std::string name;int number;};Telephone getTelephone(){// Telephone("Rainer Grimm", 12345) createdreturn {"Rainer Grimm", 12345};}struct MyArray {public:MyArray(): data {1, 2, 3, 4, 5} {}private:const int data[5];};void getVector(const std::vector<int>& v){// some code}int main(){// built-in datatypes and stringsbool b{true};bool b2 = {true};int i{2011};int i2 = {2011};std::string s{"string"};std::string s2 = {"string"};// struct and classMyStruct basic{5, 3.2};MyStruct basic2 = {5, 3.2};MyClass alsoClass{5, 3.2};MyClass alsoClass2 = {5, 3.2};// C-Array// dynamic array initializationconst float * pData = new const float[4] { 1.5, 4, 3.5, 4.5 };// STL-Container// a vector of 1 elementstd::vector<int> oneElement{1};std::vector<int> oneElement2= {1};std::unordered_map<std::string, int> um { {"Dijkstra", 1972}, {"Scott", 1976}, {"Wilkes", 1967}, {"Hamming", 1968} };// special cases// brace initialization for a std::vectorgetVector({ oneElement[0], 5, 10, 20, 30 });// methodstd::vector<int> v {};v.insert(v.end(), { 99, 88, -1, 15 });// getTelephone returns an initializer listTelephone tel(getTelephone());}
Explanation #
-
The code shows us several instances of the
{}
-initializer being used with different entities. -
In line 23, the
getTelephone
function returns an initializer ...