C++11 New Container 1:array
The array was first seen in boost:http://www.boost.org/doc/libs/1_61_0/doc/html/array.html
It was originally intended to provide a fixed-length array allocated on the stack, and the template algorithm in STL could be used.
The use of array is as follows:
#include <string>#include<iterator>#include<iostream>#include<algorithm>#include<array>intMain () {//Construction uses aggregate initializationstd::array<int,3> a1{{1,2,3} };//double-braces required in c++11 (not in c++14)std::array<int,3> a2 = {1,2,3};//never required after =STD::ARRAY<STD::string,2> a3 = {std::string("a"),"b" }; //container operations is supportedStd::sort (A1.begin (), A1.end ()); Std::reverse_copy (A2.begin (), A2.end (), Std::ostream_iterator<int> (Std::cout," ")); Std::cout<<'\ n'; //ranged for loop is supported for(Constauto&s:a3) Std::cout<< s <<' ';}
New containers in c++11: Unordered_map unordered_set
The same component that comes to boost:http://www.boost.org/doc/libs/1_61_0/doc/html/unordered.html
In the early standard library STL, there was only a red-black tree map, not a hash map.
So boost provides the unordered component and enters the standard library in c++11.
UNORDERED_MAP provides a similar interface to map, except that map is ordered, and unordered_map is unordered because it uses hash map data structures.
Also, because map uses a red-black tree, the lookup performance is O (log (n)). Unordered_map uses hash map, so the lookup performance is O (1).
So in general, small-scale data suitable for the use of map (less than hundred W), and large-scale data for unordered_map (more than hundred W)
Unordered_map is used as follows:
#include <iostream>#include<string>#include<unordered_map>intMain () {//Create an unordered_map of three strings (that map to strings)STD::UNORDERED_MAP<STD::string, std::string> U = { {"RED","#FF0000"}, {"GREEN","#00FF00"}, {"BLUE","#0000FF"} }; //iterate and print keys and values of Unordered_map for(Constauto&n:u) {std::cout<<"key:["<< N.first <<"] value:["<< N.second <<"]\n"; } //Add New entries to the Unordered_mapu["BLACK"] ="#000000"; u[" White"] ="#FFFFFF"; //Output values by keyStd::cout <<"The HEX of color RED is:["<< u["RED"] <<"]\n"; Std::cout<<"The HEX of color BLACK is:["<< u["BLACK"] <<"]\n"; return 0;}
New container in c++11: Non-member Begin\end
Std::begin/std::end is not a container, but because the purpose of design std::begin/std::end should be to allow traditional C-style arrays to use the template algorithm in STL, so it is also presented here.
Std::begin/std::end is used as follows:
#include <iostream>#include<vector>#include<iterator>intMain () {std::vector<int> v = {3,1,4 }; Auto VI=Std::begin (v); Std::cout<< *vi <<'\ n'; intA[] = {-5,Ten, the }; Auto AI=Std::begin (a); Std::cout<< *ai <<'\ n';}
Abelkhan Technology Forum: http://abelkhan.com/forum.php, Welcome to Exchange technology
Learn about the new containers in c++11--c++11