Learn about the new containers in c++11--c++11

Source: Internet
Author: User

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&LT;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&LT;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.