C++ 10 关联容器

map 使用

#include <iostream>
#include <utility>
#include <map>
#include <string>
using std::pair; using std::map;
using std::string; using std::cin; using std::cout; using std::endl;

int main()
{
	pair<string, string> author("Jom", "Tom");
	author = std::make_pair("Tom", "Jerry");			// pair 对象使用
	cout << "author : " << author.first << "&" << author.second << endl;

	map<string, int> word_cout;
	word_cout["A"] = 1;		// map 对象使用
	map<string, int>::iterator map_it = word_cout.begin();
	cout << map_it->first << ":" << map_it->second << endl;
	++word_cout["A"];
	cout << "count for A: " << word_cout["A"] << endl;

	word_cout.insert(map<string, int>::value_type("B", 1));
	pair<map<string, int>::iterator, bool> ret = word_cout.insert(std::make_pair("B", 1)); // map插入 返回插入键迭代器和 是否插入
	if (!ret.second) ++ret.first->second;		// 若键已存在 , 则+1

	int occurs = 0;
	if (word_cout.count("B"))
		occurs = word_cout["B"];		// map 统计是否存在
	map<string, int>::iterator it = word_cout.find("A");	// map查找 若不存在 等于尾迭代器
	if (it != word_cout.end())	
		occurs = it->second;

	if (word_cout.erase("A"))		// map删除 0,1
		cout << "delete success : " << "A" << endl;
	else
		cout << "delete word not found" << endl;
			
	map<string, int>::const_iterator map_iter = word_cout.begin();  // map 遍历
	while (map_iter != word_cout.end()) {
		cout << map_iter->first << " : " << map_iter->second << endl;
		++map_iter;
	}

	return 0;
}

单词统计

#include <iostream>
#include<fstream>
#include <utility>
#include <map>
#include <string>
using std::pair; using std::map;
using std::string; using std::cin; using std::cout; using std::endl;

int main()
{
	std::ifstream in_file("out.txt");
	map<string, int> word_count;
	string word;
	while (in_file >> word) {
		pair<map<string, int>::iterator, bool> ret = word_count.insert(std::make_pair(word,1));
		if (!ret.second) ++ret.first->second;
	}
	for (map<string, int>::iterator it = word_count.begin(); it != word_count.end(); ++it)
	{
		cout << it->first << ":" << it->second << endl;
	}

	return 0;
}

multimap、multiset用法

#include <iostream>
#include<fstream>
#include <utility>
#include <set>
#include <map>
#include <string>
using std::pair; using std::map; using std::set; using std::multimap; using std::multiset;
using std::string; using std::cin; using std::cout; using std::endl;

int main()
{
	set<string> set1;
	set1.insert("A");
	set<string> set2;
	set2.insert(set1.begin(), set1.end());	// set 插入

	set<string>::iterator it = set2.find("A");
	set2.count("A"); // 0 , 1

	multimap<string, string> mapping;
	mapping.insert(std::make_pair("A", "B"));
	mapping.insert(std::make_pair("A", "C"));	// multimap 插入
	string search_item("A");
	multimap<string, string>::size_type enties = mapping.count(search_item); // multimap 统计
	multimap<string, string>::iterator it2 = mapping.find(search_item);	// multimap 查找
	for (multimap<string, string>::size_type i = 0; i != enties; ++i, ++it2)
		cout << it2->first << ":" << it2->second << endl;

	multimap<string, string>::iterator beg = mapping.lower_bound(search_item), end = mapping.upper_bound(search_item); //multimap 读取键对应范围
	while (beg != end) {
		cout << beg->first << ":" << beg->second << endl;
		++beg;
	}
	typedef multimap<string, string>::iterator map_it;
	pair<map_it, map_it> pos = mapping.equal_range(search_item); // multimap一次性获取键对应范围
	while (pos.first != pos.second)
	{
		cout << pos.first->first << ":" << pos.first->second << endl;
		++pos.first;
	}
	multimap<string, string>::size_type cnt = mapping.erase(search_item); // multimap 删除
	cout << "remove :" << cnt << endl;

	return 0;
}

posted @ 2017-06-11 22:22  张狂莫怪  Views(140)  Comments(0Edit  收藏  举报