C++ Primer 读书笔记 Charpter 10 关联容器

  1. 关联容器:Map(<key,value>), Set(<key>), MultiMap(key可以重复), MultiSet(key可以重复)
  2. pair类型
    1. 初始化:pair<T1,T2> p1; pair<T1,T2> p1(v1,v2); make_pair(v1,v2)
    2. pair比较,先比较first,再比较second
    3. pair相等,first相等且second相等
    4. p.first, p.second
  3. Map类型
    1. 定义: map<k,v> m; map<k,v> m(m2); map<k,v> m(b,e);
    2. 键类型:必须严格弱排序,即必须定义<,其他关系不作要求
    3. map定义的类型:
      1. key_value:键值
      2. mapped_type:键关联的值
      3. value_type:pair类型,first为key_type, second为mapped_value
    4. 给map添加元素
      1. 用下标访问不存在的元素,会导致map容器添加一个元素,键即为下标值
      2. map下标返回的类型与对map迭代器进行解引用获得类型不同:下标操作返回mapped_type类型的值,迭代器返回value_type类型的值
      3. insert(e): 如果e.first不在,则插入,如果在,则保持不变。返回值为pair<指向e.first的map迭代器,bool表示是否插入了>
      4. insert(beg, end):将beg和end之间的first不在m中的元素插入到m中,返回void
      5. insert(iter,e):如果e.first不在m中,则创建新元素,并以iter为起点搜索新元素的位置,返回一个迭代器指向给定键的元素
    5. 查找并读取map中的元素
      1. m.count(k) m中k出现的次数,k为键类型
      2. m.find(k) m中存在k索引的元素,则返回该元素的迭代器,否则返回超出末端迭代器,即end
    6. 删除元素
      1. m.erase(k):删除键为k的元素,返回size_type,表示删除个数
      2. m.erase(p):删除迭代器p指向的元素,返回void
      3. m.erase(b,e):删除b到e之间的元素,返回void
  4. Set类型
    1. set支持大部分map操作,包括构造函数,insert,count和find,erase
    2. 添加元素:insert
    3. 获取元素:find和count,不支持下标操作
    4. Map和Set的key是只读类型
        set<int>::iterator set_it = iset.find(1); 
        
        *set_it = 11; //error, key is read-only
  5. MultiSet和MultiMap:键值不唯一
    1. 元素的添加和删除:
      1. 每次insert都会添加一个元素
      2. 带有一个键参数的erase会删除该键的所有元素:m.erase(k)
      3. 带有一个或一对迭代器参数的版本只删除指定元素
    2. 查找:在multimap中,同一个键所关联的元素必然相邻存放
      1. 使用find和count查找
          // author we'll look for
          
          string search_item("Alain de Botton");
          
          
          
          // how many entries are there for this author
          
          typedef multimap<string, string>::size_type sz_type;
          
          sz_type entries = authors.count(search_item);
          
          
          
          // get iterator to the first entry for this author
          
          multimap<string,string>::iterator iter = authors.find(search_item);
          
          
          
          // loop through the number of entries there are for this author
          
          for (sz_type cnt = 0; cnt != entries; ++cnt, ++iter) 
          
          	cout << iter->second << endl; // print each title
      2. 使用lower_bound和upper_bound
        1. lower_bound(k):指向键不小于k的第一个元素,返回迭代器
        2. upper_bound(k):指向键大于k的第一个元素,返回迭代器
        3. equal_range(k):返回pair,first等于lower_bound,second等于upper_bound
        4. // beg and end denote range of elements for this author
          
          typedef multimap<string, string>::iterator authors_it;
          
          
          
          authors_it beg = authors.lower_bound(search_item), end = authors.upper_bound(search_item);
          
          
          
          // loop through the number of entries there are for this author
          
          while (beg != end) {
          
          	cout << beg->second << endl; // print each title
          
          	++beg;
          
          }

你可能感兴趣的:(读书笔记)