map member functions

Posted h404nofound

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了map member functions相关的知识,希望对你有一定的参考价值。

http://www.cplusplus.com

 

搜了才发现map的成员函数这么多orz,跟着cplusplus按字典序走一遍叭(顺序有微调orz

<1>  map::at (c++11)

// map::at 
//Returns a reference to the mapped value of the element identified with key k.
//If k does not match the key of any element in the container, the function throws an out_of_range exception.
#include <iostream>
#include <string>
#include <map>

int main ()

  std::map<std::string,int> mymap = 
                 "alpha", 0 ,
                 "beta", 0 ,
                 "gamma", 0  ;

  mymap.at("alpha") = 10;
  mymap.at("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*/

<2> map::begin/end

// map::begin/end
//Returns an iterator referring to the first element in the map container.
//end同理
#include <iostream>
#include <map>

int main ()

  std::map<char,int> mymap;

  mymap[b] = 100;
  mymap[a] = 200;
  mymap[c] = 300;

  // show content:
  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << \n;

  return 0;

/*output
a => 200
b => 100
c => 300*/

 

<3>map::cbegin(c++11)

// map::cbegin/cend
#include <iostream>
#include <map>

int main ()

  std::map<char,int> mymap;

  mymap[b] = 100;
  mymap[a] = 200;
  mymap[c] = 300;

  // print content:
  std::cout << "mymap contains:";
  for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
    std::cout << " [" << (*it).first << : << (*it).second << ];
  std::cout << \n;

  return 0;

/*output
mymap contains: [a:200] [b:100] [c:300]
*/

<4> map::clear

Removes all elements from the map container (which are destroyed), leaving the container with a size of 0.

//很简单所以就不转例子了orz

<5>map::count

Searches the container for elements with a key equivalent to k and returns the number of matches.
Because all elements in a map container are unique, the function can only return 1 (if the element is found) or zero (otherwise).
Two keys are considered equivalent if the container‘s comparison object returns false reflexively (i.e., no matter the order in which the keys are passed as arguments).

// map::count
#include <iostream>
#include <map>
int main ()

  std::map<char,int> 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;

<5>map::crbegin

Returns a const_reverse_iterator pointing to the last element in the container (i.e., its reverse beginning).

<6>map::crend

Returns a const_reverse_iterator pointing to the theoretical element preceding the first element in the container (which is considered its reverse end).

 

// map::crbegin/crend
#include <iostream>
#include <map>

int main ()

  std::map<char,int> mymap;

  mymap[b] = 100;
  mymap[a] = 200;
  mymap[c] = 300;

  std::cout << "mymap backwards:";
  for (auto rit = mymap.crbegin(); rit != mymap.crend(); ++rit)
    std::cout << " [" << rit->first << : << rit->second << ];
  std::cout << \n;

  return 0;

//output:mymap backwards: [c:300] [b:100] [a:200]

 

<7>map::emplace(c++11)

// map::emplace
#include <iostream>
#include <map>

int main ()

  std::map<char,int> 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]

<8>map::emplace_hint

带有提示的位置插入?

// map::emplace_hint
#include <iostream>
#include <map>

int main ()

  std::map<char,int> mymap;
  auto it = mymap.end();

  it = mymap.emplace_hint(it,b,10);
  mymap.emplace_hint(it,a,12);
  mymap.emplace_hint(mymap.end(),c,14);

  std::cout << "mymap contains:";
  for (auto& x: mymap)
    std::cout << " [" << x.first << : << x.second << ];
  std::cout << \n;

  return 0;

//output:mymap contains: [a:12] [b:10] [c:14]

 

<9>map::empty

Returns whether the map container is empty (i.e. whether its size is 0).

<10>map::equal_range

<11>map::erase

以上是关于map member functions的主要内容,如果未能解决你的问题,请参考以下文章

variadic templates & pass by const reference & member operator [] in const map & gcc suc

thinkPHP分页的制作

thinkPHP分页的制作

在Sonar中删除此未使用的方法参数

java 通过map实现循环控制

mybatis 返回值类型是Map