STL set multiset map multimap unordered_set unordered_map example

265 篇文章 1 订阅


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("-------------------------------\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 compiler
	
	map<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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值