STL set multiset map multimap unordered_set unordered_map example

来源:互联网 发布:溜冰鞋淘宝网 编辑:程序博客网 时间:2024/06/05 15:38


I decide to write to my blogs in English. When I meet something hard to depict, I'll add some Chinese necessarily. 


The differences between these containers are :

  1. The keys of set and map are unique, but they could be multiple for multiset and multimap. 
  2. Unordered_set and unordered_map are implemented by hash map, but the above 4 containers are implemented by Red-Black tree. They could have been named as hash_map, however, these names seem to have be  used by other standards. So this ugly name ‘unordered’ is adopted in contrast to the attribute 'ordered' of the above 4 containers. 
The following is the code: 


#include<map>#include<stdio.h>#include<unordered_map> #include<algorithm>//#include<unordered_multimap>  //there isn't such a library#include<string>#include<iostream>using namespace std;int main(){unordered_multimap<int, string> mapStudent1;mapStudent1.insert(pair<int, string>(1, "student_one"));mapStudent1.insert(pair<int, string>(2, "student_two"));mapStudent1.insert(unordered_multimap<int, string>::value_type (2, "student_two"));   //这样插入也可以,但是注意key是Unique的     mapStudent1.insert(make_pair<int, string>(3, "student_three"));mapStudent1.insert(make_pair<int, string>(3, "student_three"));//mapStudent.emplace(5,"student_five");  //Have to add the command '-std=c++11' for compilerunordered_multimap<int, string>::iterator  iter1;for(iter1 = mapStudent1.begin(); iter1 != mapStudent1.end(); iter1++){cout<<iter1->first<<"   "<<iter1->second<<endl;}printf("-------------------------------\n\n");map<int, string> mapStudent2;mapStudent2.insert(pair<int, string>(1, "student_one"));mapStudent2.insert(pair<int, string>(2, "student_two"));mapStudent2.insert(map<int, string>::value_type (2, "student_two"));   //这样插入也可以,但是注意key是Unique的     mapStudent2.insert(pair<int, string>(3, "student_three"));mapStudent2.insert(pair<int, string>(3, "student_three"));mapStudent2[4]="hello"; //利用数组插入同样可以,但是效率比较低 //mapStudent.emplace(5,"student_five");  //Have to add the command '-std=c++11' for compilermap<int, string>::iterator  iter2;for(iter2 = mapStudent2.begin(); iter2 != mapStudent2.end(); iter2++){cout<<iter2->first<<"   "<<iter2->second<<endl;}printf("-------------------------------\n\n");    std::unordered_multimap<std::string,std::string> myumm = {     {"orange","FL"},     {"strawberry","LA"},     {"strawberry","OK"},     {"pumpkin","NH"} };    for (auto& x: {"orange","lemon","strawberry"}) {      std::cout << x << ": " << myumm.count(x) << " entries.\n";    }        printf("-------------------------------\n\n");typedef std::unordered_multimap<std::string,std::string> stringmap;    stringmap myumm1 = {     {"orange","FL"},     {"strawberry","LA"},     {"pumpkin","NH"},     {"strawberry","OK"}    };cout<<"All entries are:"<<endl;stringmap::iterator  iter3;for(iter3 = myumm1.begin(); iter3 != myumm1.end(); iter3++){cout<<iter3->first<<"   "<<iter3->second<<endl;}    std::cout << "Entries with strawberry:";    auto range = myumm1.equal_range("strawberry");    for_each (      range.first,      range.second,      [](stringmap::value_type& x){std::cout << " " << x.second;}    );return 0;}


原创粉丝点击