• 【C++】泛型算法(六)Map和Set的使用


    Map

    map
    key起到索引的作用。

    //常见使用:字数统计程序
    #include 
    #include 
    map<string, int> words; //string是key, int是value
    
    string tword;
    while(cin >> tword)
    {
    	words[tword]++; //字数统计
    	//其中words[tword]取出与tword相应的value
    	//如果tword不在map里,则它会因此被放入map中,并获得默认值0
    }
    
    map<string,int>::iterator it=words.begin();
    for (; it!=word.end(); ++it)
    	cout<<"key:"<<it->first 		  //first对应key
    		<<"value:"<<it->second<<endl; //second对应value
    									  //cout输出字眼在map里出现的次数
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    查询map内是否存在某个key的三种方法

    方法一:key索引

    int count = 0;
    if(!(count = words["vermeer"]))
    
    • 1
    • 2

    缺点:如果key不存在于map内,这个key会被自动加入map中,对应的value被设置为所属类型的默认值

    方法二:利用map自带的find()函数

    int count = 0;
    map<string,int>::iterator it;
    
    it = words.find("vermeer");
    if (it != words.end())
    	count = it->second;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    如果key已放在其中,find()会返回一个iterator,指向key/value形成pair(pair class是标准库的一员),反之则返回words.end()。

    方法三:利用map自带的count()函数

    int count = 0;
    
    if(words.count("vermeer"))  //只要存在
        value = words["vermeer"];
    
    • 1
    • 2
    • 3
    • 4

    count()会返回某特定项在map内的个数

    任何一个key在map内最多只会有一份,如需储存多份相同的key,就必须使用multimap。(略)

    Set

    Set由一群key组合而成;
    如果想知道某值是否存在于某个集合内,可以用set。

    //常见应用:统计排除的字集
    #include
    #include
    #include
    
    map<string,int>words;
    stirng tword;
    set<string>word_exclusion;//set只有key值
    
    while(cin >> tword)
    {
    	if(word_exclusion.count(tword));
    	//如果tword在“排除字集”内,则此次迭代被跳过
    		continue;
    	
    	words[ tword ]++;//到达此处说明tword不属于需要排除的字集,纳入map
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    set:升序排列

    默认情况下,set里的元素默认升序排列(从小到大排列)

    int ia[10]={1,3,5,8,5,3,1,5,8,1};
    vector<int>vec(ia,ia+10);
    set<int>iset(vec.begin(),vec.end());
    //iset的元素为{1,3,5,8}
    //(已经排除了和1,3,5,8相同的元素)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    set:加入元素

    1. 为set加入单一元素,可使用单一参数的insert():
    iset.insert(ival);//ival为单一元素
    
    • 1
    1. 为set加入多个元素,可使用双参数的insert():
    iset.insert(vec.begin(),vec.end());
    
    • 1

    set:迭代

    使用泛型指针iterator

    set<int>::iterator it=iset.begin();
    for(; it!=iset.end(); ++it)
    {
    	cout << *it << ' ';
    }
    cout << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    泛型算法中还有其他和set相关的泛型算法:
    set_intersection(),
    set_union(),
    set_difference(),
    set_symmetric_difference()

    对于任何key值,set只能储存一份,要储存多份相同key值,必须使用multiset。(略)

  • 相关阅读:
    关于:在企业局域网中启用 Delivery Optimization
    Spring多事务管理器报错处理
    1.java环境搭建与eclipse安装和配置
    用数字隔离器取代传统的光耦合器
    特征工程建模可解释包(note)
    前端面试前端性能优化篇
    笔记本电脑没有声音?几招恢复声音流畅!
    自动化测试----unittest框架
    Golang第一章:入门
    【数模系列】02_三大相关系数+Python代码
  • 原文地址:https://blog.csdn.net/weixin_49347928/article/details/133122772