zoukankan      html  css  js  c++  java
  • STL set multiset map multimap unordered_set unordered_map example

    
    
    
    


    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 compiler
    	
    	unordered_multimap<int, string>::iterator  iter1;
    	for(iter1 = mapStudent1.begin(); iter1 != mapStudent1.end(); iter1++)
    	{
    		cout<<iter1->first<<"   "<<iter1->second<<endl;
    	}
    	printf("-------------------------------
    
    ");	
    
    	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 compiler
    	
    	map<int, string>::iterator  iter2;
    	for(iter2 = mapStudent2.begin(); iter2 != mapStudent2.end(); iter2++)
    	{
    		cout<<iter2->first<<"   "<<iter2->second<<endl;
    	}
    
    	printf("-------------------------------
    
    ");	
        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.
    ";
        }
        
        
    	printf("-------------------------------
    
    ");	
    	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;
    }


  • 相关阅读:
    codeforces 814B An express train to reveries
    codeforces 814A An abandoned sentiment from past
    codeforces 785D D. Anton and School
    codeforces 785C Anton and Fairy Tale
    codeforces 791C Bear and Different Names
    AOP详解
    Spring集成JUnit测试
    Spring整合web开发
    IOC装配Bean(注解方式)
    IOC装配Bean(XML方式)
  • 原文地址:https://www.cnblogs.com/james1207/p/3347864.html
Copyright © 2011-2022 走看看