码迷,mamicode.com
首页 > 编程语言 > 详细

C++ multimap 的插入,遍历,删除

时间:2014-10-07 12:28:53      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:multimap   插入   遍历   删除   stl   

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
	multimap<string, string> authors;

	// 插入元素, 引入的头文件是 map
	authors.insert(make_pair(
		string("Evin"),   // 这个是 Key
		string("Hi")));

	authors.insert(make_pair(
		string("Evin"),   // 跟上面的一样,是同一个key
		string("Hello")));

	
	typedef multimap<string, string>::const_iterator mmap_cit;

	// 第一种遍历方式
	mmap_cit beg = authors.lower_bound("Evin");
	mmap_cit end = authors.upper_bound("Evin");

	while(beg != end)
	{
		cout<<beg->second<<endl;
		++beg;
	}

	// 第二种遍历方式
	pair<mmap_cit, mmap_cit> pos = authors.equal_range("Evin");

	while(pos.first != pos.second)
	{
		cout<<pos.first->second<<endl;
		++pos.first;
	}

	cout<<authors.erase("Evin")<<endl;    // erase 返回删除的元素个数

	return 0;
}


利用 multimap 可以做到一个key, 同时对应多个value


C++ multimap 的插入,遍历,删除

标签:multimap   插入   遍历   删除   stl   

原文地址:http://blog.csdn.net/core__code/article/details/39852601

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!