C++ 字典 map详解

参考:map

文章目录

  • 容器重要属性
  • 操作
    • 迭代器
      • 1. begin 指向起始
      • 2. end 指向末尾
      • 3. rbegin 指向倒序起始(即末尾)
      • 4. rend 指向倒序末尾(即起始)
    • 大小
      • 1. empty 判断容器是否为空
      • 2. size 返回容器大小
      • 3. max_size 返回容器最大尺寸
    • 元素访问
      • 1. operater[ ]
      • 2. at
    • 修改元素
      • 1. insert 插入元素
      • 2. erase 删除元素
      • 3. swap 交换两个map容器内容
      • 4. clear 清除容器
      • 5. emplace 构造并插入元素
    • 操作
      • 1. 赋值
      • 2. find 获得指向元素的迭代器
      • 3. count 对某个键的元素计数
      • 4. lower_bound 返回下边界的迭代器
      • 5. upper_bound 返回上边界的迭代器
      • 6. equal_range 获得相同元素的范围


容器重要属性

  • 通过键值访问,而不是位置
  • 按Key有序排列
  • 键与值一 一对应
  • 键唯一,不存在相同的键对应不同的值

操作

  • 迭代器

    1. begin 指向起始

    2. end 指向末尾

    3. rbegin 指向倒序起始(即末尾)

    4. rend 指向倒序末尾(即起始)

    // map::begin/end
    #include 
    #include 
    
    int main ()
    {
      std::map mymap;
    
      mymap['b'] = 100;
      mymap['a'] = 200;
      mymap['c'] = 300;
    
      // show content:
      for (std::map::iterator it=mymap.begin(); it!=mymap.end(); ++it)
        std::cout << it->first << " => " << it->second << '\n';
    
      return 0;
    }
    // Output:
    //	a => 200
    //	b => 100
    //	c => 300
    
  • 大小

    1. empty 判断容器是否为空

    // map::empty
    #include 
    #include 
    
    int main ()
    {
      std::map mymap;
    
      mymap['a']=10;
      mymap['b']=20;
      mymap['c']=30;
    
      while (!mymap.empty())
      {
        std::cout << mymap.begin()->first << " => " << mymap.begin()->second << '\n';
        mymap.erase(mymap.begin());
      }
    
      return 0;
    }
    
    //Output:
    //a => 10
    //b => 20
    //c => 30
    

    2. size 返回容器大小

    // map::size
    #include 
    #include 
    
    int main ()
    {
      std::map mymap;
      mymap['a']=101;
      mymap['b']=202;
      mymap['c']=302;
    
      std::cout << "mymap.size() is " << mymap.size() << '\n';
    
      return 0;
    }
    //Output:
    //mymap.size() is 3
    

    3. max_size 返回容器最大尺寸

  • 元素访问

    1. operater[ ]

    2. at

    // accessing mapped values: at / operator[]
    #include 
    #include 
    #include 
    
    int main ()
    {
     std::map mymap = {
                   { "alpha", 0 },
                   { "beta", 0 },
                   { "gamma", 0 } };
    
     mymap.at("alpha") = 10;
     mymap["beta"] = 20;
     mymap.at("gamma") = 30;
    
     for (auto& x: mymap) {
       std::cout << x.first << ": " << x.second << '\n';
     }
    
     return 0;
    }
    //Output:
    //alpha: 10
    //beta: 20
    //gamma: 30
    
  • 修改元素

    1. insert 插入元素

    方法一:直接插入键值对mymap.insert ( std::pair('a',100) );
    方法二:通过迭代器指定位置并插入std::map::iterator it = mymap.begin(); mymap.insert (it, std::pair('b',300)); // max efficiency inserting
    方法三:通过迭代器范围插入,区间半包含([ , ))
    std::map anothermap; anothermap.insert(mymap.begin(),mymap.find('c'));

    // map::insert (C++98)
    #include 
    #include 
    
    int main ()
    {
     std::map mymap;
    
     // first insert function version (single parameter):
     mymap.insert ( std::pair('a',100) );
     mymap.insert ( std::pair('z',200) );
    
     std::pair::iterator,bool> ret;
     ret = mymap.insert ( std::pair('z',500) );
     if (ret.second==false) {
       std::cout << "element 'z' already existed";
       std::cout << " with a value of " << ret.first->second << '\n';
     }
    
     // second insert function version (with hint position):
     std::map::iterator it = mymap.begin();
     mymap.insert (it, std::pair('b',300));  // max efficiency inserting
     mymap.insert (it, std::pair('c',400));  // no max efficiency inserting
    
     // third insert function version (range insertion):
     std::map anothermap;
     anothermap.insert(mymap.begin(),mymap.find('c'));
    
     // showing contents:
     std::cout << "mymap contains:\n";
     for (it=mymap.begin(); it!=mymap.end(); ++it)
       std::cout << it->first << " => " << it->second << '\n';
    
     std::cout << "anothermap contains:\n";
     for (it=anothermap.begin(); it!=anothermap.end(); ++it)
       std::cout << it->first << " => " << it->second << '\n';
    
     return 0;
    }
    //Output:
    //element 'z' already existed with a value of 200
    //mymap contains:
    //a => 100
    //b => 300
    //c => 400
    //z => 200
    //anothermap contains:
    //a => 100
    //b => 300
    

    2. erase 删除元素

    // erasing from map
    #include 
    #include 
    
    int main ()
    {
     std::map mymap;
     std::map::iterator it;
    
     // insert some values:
     mymap['a']=10;
     mymap['b']=20;
     mymap['c']=30;
     mymap['d']=40;
     mymap['e']=50;
     mymap['f']=60;
    
     it=mymap.find('b');
     mymap.erase (it);                   // erasing by iterator
    
     mymap.erase ('c');                  // erasing by key
    
     it=mymap.find ('e');
     mymap.erase ( it, mymap.end() );    // erasing by range
    
     // show content:
     for (it=mymap.begin(); it!=mymap.end(); ++it)
       std::cout << it->first << " => " << it->second << '\n';
    
     return 0;
    }
    //Output:
    //a => 10
    //d => 40
    

    3. swap 交换两个map容器内容

    // swap maps
    #include 
    #include 
    
    int main ()
    {
      std::map foo,bar;
    
      foo['x']=100;
      foo['y']=200;
    
      bar['a']=11;
      bar['b']=22;
      bar['c']=33;
    
      foo.swap(bar);
    
      std::cout << "foo contains:\n";
      for (std::map::iterator it=foo.begin(); it!=foo.end(); ++it)
        std::cout << it->first << " => " << it->second << '\n';
    
      std::cout << "bar contains:\n";
      for (std::map::iterator it=bar.begin(); it!=bar.end(); ++it)
        std::cout << it->first << " => " << it->second << '\n';
    
      return 0;
    }
    //Output:
    //foo contains:
    //a => 11
    //b => 22
    //c => 33
    //bar contains:
    //x => 100
    //y => 200
    

    4. clear 清除容器

    5. emplace 构造并插入元素

    // map::emplace
    #include 
    #include 
    
    int main ()
    {
      std::map mymap;
    
      mymap.emplace('x',100);
      mymap.emplace('y',200);
      mymap.emplace('z',100);
    
      std::cout << "mymap contains:";
      for (auto& x: mymap)
        std::cout << " [" << x.first << ':' << x.second << ']';
      std::cout << '\n';
    
      return 0;
    }
    //Output:
    //mymap contains: [x:100] [y:200] [z:100]
    
  • 操作

    1. 赋值

    // assignment operator with maps
    #include 
    #include 
    
    int main ()
    {
     std::map first;
     std::map second;
    
     first['x']=8;
     first['y']=16;
     first['z']=32;
    
     second=first;                // second now contains 3 ints
     first=std::map();  // and first is now empty
    
     std::cout << "Size of first: " << first.size() << '\n';
     std::cout << "Size of second: " << second.size() << '\n';
     return 0;
    }
    //Output:
    //Size of first: 0
    //Size of second: 3
    

    2. find 获得指向元素的迭代器

    // map::find
    #include 
    #include 
    
    int main ()
    {
     std::map mymap;
     std::map::iterator it;
    
     mymap['a']=50;
     mymap['b']=100;
     mymap['c']=150;
     mymap['d']=200;
    
     it = mymap.find('b');
     if (it != mymap.end())
       mymap.erase (it);
    
     // print content:
     std::cout << "elements in mymap:" << '\n';
     std::cout << "a => " << mymap.find('a')->second << '\n';
     std::cout << "c => " << mymap.find('c')->second << '\n';
     std::cout << "d => " << mymap.find('d')->second << '\n';
    
     return 0;
    }
    //Output:
    //elements in mymap:
    //a => 50
    //c => 150
    //d => 200
    

    3. count 对某个键的元素计数

    // map::count
    #include 
    #include 
    
    int main ()
    {
      std::map mymap;
      char c;
    
      mymap ['a']=101;
      mymap ['c']=202;
      mymap ['f']=303;
    
      for (c='a'; c<'h'; c++)
      {
        std::cout << c;
        if (mymap.count(c)>0)
          std::cout << " is an element of mymap.\n";
        else 
          std::cout << " is not an element of mymap.\n";
      }
    
      return 0;
    }
    //Output:
    //a is an element of mymap.
    //b is not an element of mymap.
    //c is an element of mymap.
    //d is not an element of mymap.
    //e is not an element of mymap.
    //f is an element of mymap.
    //g is not an element of mymap.
    

    4. lower_bound 返回下边界的迭代器

    返回指向该元素的迭代器

    5. upper_bound 返回上边界的迭代器

    返回指向该元素的下一个元素的迭代器

    // map::lower_bound/upper_bound
    #include 
    #include 
    
    int main ()
    {
     std::map mymap;
     std::map::iterator itlow,itup;
    
     mymap['a']=20;
     mymap['b']=40;
     mymap['c']=60;
     mymap['d']=80;
     mymap['e']=100;
    
     itlow=mymap.lower_bound ('b');  // itlow points to b
     itup=mymap.upper_bound ('d');   // itup points to e (not d!)
    
     mymap.erase(itlow,itup);        // erases [itlow,itup)
    
     // print content:
     for (std::map::iterator it=mymap.begin(); it!=mymap.end(); ++it)
       std::cout << it->first << " => " << it->second << '\n';
    
     return 0;
    }
    //Output:
    //a => 20
    //e => 100
    

    6. equal_range 获得相同元素的范围

你可能感兴趣的:(C++,c++,map,字典)