首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

STL set multiset 地图 multi地图 unordered_set unordered_地图 example

2013-10-01 
STL set multiset map multimap unordered_set unordered_map example#includemap#includestdio.h#inc

STL set multiset map multimap unordered_set unordered_map example

#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;}


热点排行