zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
  • 相关阅读:
    使用Apache搭建个人用户主页
    Linux搭建ftp服务器,并建立本地用户与虚拟用户
    Linux搭建FTP服务器,并建立匿名用户登录
    用虚拟机安装RHEL7
    基于OpenStack构建企业私有云(5)Neutron
    基于OpenStack构建企业私有云(3)Glance
    基于OpenStack构建企业私有云(1)实验环境准备
    基于OpenStack构建企业私有云(2)KeyStone
    python--003--百分号字符串拼接、format
    python--002--数据类型(set)
  • 原文地址:https://www.cnblogs.com/hiqianqian/p/6986663.html
Copyright © 2011-2022 走看看