c++ primer multimap

	std::multimap<std::string, std::string> authors;

	// 插入
	authors.insert(std::make_pair(std::string("test1"), std::string("aaa")));
	authors.insert(std::make_pair(std::string("test1"), std::string("bbb")));


	// 查找
	string search_item("test1");

	typedef multimap<string, string>::size_type sz_type;
	sz_type entries = authors.count(search_item);

	multimap<string, string>::iterator iter = authors.find(search_item);

	for(sz_type cnt = 0; cnt != entries; ++cnt, ++iter)
	{
		std::cout << iter->second << std::endl;
	}
	

	// 查找方式二
	typedef multimap<string, string>::iterator authors_iter;
	authors_iter beg = authors.lower_bound(search_item);
	authors_iter end = authors.upper_bound(search_item);

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

	// 查找方式三
	std::pair<authors_iter, authors_iter> pos = authors.equal_range(search_item);

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

		++pos.first;
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章