C++:STL常用模块总结(map)

map

map又称为哈希表,是一个由标记值(key value)和映射(mapped value)组成的关系列表,其中标记值将映射值进行排序和整理,每一个标记值对应着一个映射值,map在通过标记值找到映射值的过程比unordered_map慢,但是可以通过指针依照排放顺序来进行操作。

使用之前引用
#include <map>
定义方法重载函数汇总
empty (1):  
explicit map (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());
explicit map (const allocator_type& alloc);
range (2):  
template <class InputIterator>
  map (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());
copy (3):   
map (const map& x);
map (const map& x, const allocator_type& alloc);
move (4):   
map (map&& x);
map (map&& x, const allocator_type& alloc);
initializer list (5):   
map (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());
定义程序示例
#include <iostream>
#include <map>

bool fncomp (char lhs, char rhs) {return lhs<rhs;}

struct classcomp {
  bool operator() (const char& lhs, const char& rhs) const
  {return lhs<rhs;}
};

int main ()
{
  std::map<char,int> first;

  first['a']=10;
  first['b']=30;
  first['c']=50;
  first['d']=70;

  std::map<char,int> second (first.begin(),first.end());

  std::map<char,int> third (second);

  std::map<char,int,classcomp> fourth;                 // class as Compare

  bool(*fn_pt)(char,char) = fncomp;
  std::map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare

  return 0;
}
基本操作
1、map::operator[]:
mapped_type& operator[] (const key_type& k);
mapped_type& operator[] (key_type&& k);
//如果k在map里面存在,则返回映射值,如果不存在,就会以k值进本身行插入并返回一个相对映射值的引用(map会自动开辟一个空间以放置多余的元素)。相同的类似的操作符map::at,除了k在不存在时会抛出异常,其他与[]操作符功能相同。
示例程序
#include <iostream>
#include <map>
#include <string>

int main ()
{
  std::map<char,std::string> mymap;

  mymap['a']="an element";
  mymap['b']="another element";
  mymap['c']=mymap['b'];

  std::cout << "mymap['a'] is " << mymap['a'] << '\n';
  std::cout << "mymap['b'] is " << mymap['b'] << '\n';
  std::cout << "mymap['c'] is " << mymap['c'] << '\n';
  std::cout << "mymap['d'] is " << mymap['d'] << '\n';
//注意最后一个元素‘d’的访问操作时,操作器在map容器中插入了一个元素(初始化),一个空的字符串,仅仅可以通过检索找到,通过map::find是找不到的

  std::cout << "mymap now contains " << mymap.size() << " elements.\n";

  return 0;
}

output:

mymap['a'] is an element
mymap['b'] is another element
mymap['c'] is another element
mymap['d'] is
mymap now contains 4 elements.
2、map::find
      iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
//在map容器中寻找是否有对应的标记的映射值,如果有的话就返回只想这个元素的指针,如果没有就返回指向map::end的指针
示例程序
#include <iostream>
#include <map>

int main ()
{
  std::map<char,int> mymap;
  std::map<char,int>::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;
}
3、map::begin:返回指向第一个元素的指针,如果不存在,返回的指针将不能被访问。
4、map::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;
}
5、map::size:返回map容器中的元素数量
6、map::empty:如果map容器为空,则返回1,反之返回0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老潘的博客

请老潘吃块饼干!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值