In previous version of C++ standard (C++03) we was able to do following array elements assignment:
But we have not been able to do similar assignments to vector:
In order to assign above three elements to vector we had to invoke push_back() method three times:
C++11 present feature which help with above inconvenience. This feature can be achieved thanks to initializer_list template. In C++11, vector implementation has assignment operator taking initializer_list template as parameter:
Thanks to that we can use following assignments to vector:
The same way we can overload assignement operator of any of own class, as well as constructor or any function. Such definition can can take initializer_list template as parameter. Then we will be able to pass initialization list having syntax:
Of course STL vector container has implemented constructor having initializer_list as parameter, too. So you can also initialize vector that way:
Having that knowledge and understanding two previously described new features of C++11:
- range-based for loop
- auto type
We can simplify following code written in C++03:
to following quick and simplified form in C++11:
Comfortable, more readable and time saving, right? I agree.
But we have not been able to do similar assignments to vector:
In order to assign above three elements to vector we had to invoke push_back() method three times:
C++11 present feature which help with above inconvenience. This feature can be achieved thanks to initializer_list template. In C++11, vector implementation has assignment operator taking initializer_list template as parameter:
Thanks to that we can use following assignments to vector:
The same way we can overload assignement operator of any of own class, as well as constructor or any function. Such definition can can take initializer_list template as parameter. Then we will be able to pass initialization list having syntax:
{ element1, element2, element3,...}as parameter of such function.
Of course STL vector container has implemented constructor having initializer_list as parameter, too. So you can also initialize vector that way:
Having that knowledge and understanding two previously described new features of C++11:
- range-based for loop
- auto type
We can simplify following code written in C++03:
to following quick and simplified form in C++11:
Comfortable, more readable and time saving, right? I agree.
Comments
Post a Comment