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

简介: mapmap又称为哈希表,是一个由标记值(key value)和映射(mapped value)组成的关系列表,其中标记值将映射值进行排序和整理,每一个标记值对应着一个映射值,map在通过标记值找到映射值的过程比unordered_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

目录
相关文章
|
5天前
|
存储 算法 C++
C++提高篇:泛型编程和STL技术详解,探讨C++更深层的使用
文章详细探讨了C++中的泛型编程与STL技术,重点讲解了如何使用模板来创建通用的函数和类,以及模板在提高代码复用性和灵活性方面的作用。
17 2
C++提高篇:泛型编程和STL技术详解,探讨C++更深层的使用
|
2月前
|
存储 算法 编译器
[C++] STL简介
[C++] STL简介
23 1
|
2月前
|
存储 算法 C++
C++ STL应用宝典:高效处理数据的艺术与实战技巧大揭秘!
【8月更文挑战第22天】C++ STL(标准模板库)是一组高效的数据结构与算法集合,极大提升编程效率与代码可读性。它包括容器、迭代器、算法等组件。例如,统计文本中单词频率可用`std::map`和`std::ifstream`实现;对数据排序及找极值则可通过`std::vector`结合`std::sort`、`std::min/max_element`完成;而快速查找字符串则适合使用`std::set`配合其内置的`find`方法。这些示例展示了STL的强大功能,有助于编写简洁高效的代码。
34 2
|
3月前
|
NoSQL API Redis
c++开发redis module问题之为什么在使用RedisModule_GetApi之前要通过((void**)ctx)[0]这种方式获取其地址
c++开发redis module问题之为什么在使用RedisModule_GetApi之前要通过((void**)ctx)[0]这种方式获取其地址
|
3月前
|
NoSQL 编译器 Redis
c++开发redis module问题之如果Redis加载了多个C++编写的模块,并且它们都重载了operator new,会有什么影响
c++开发redis module问题之如果Redis加载了多个C++编写的模块,并且它们都重载了operator new,会有什么影响
|
3月前
|
NoSQL Redis C++
c++开发redis module问题之在复杂的Redis模块中,特别是使用第三方库或C++开发时,接管内存统计有哪些困难
c++开发redis module问题之在复杂的Redis模块中,特别是使用第三方库或C++开发时,接管内存统计有哪些困难
|
2月前
|
安全 编译器 容器
C++STL容器和智能指针
C++STL容器和智能指针
|
2月前
|
算法 安全 Linux
|
3月前
|
NoSQL Redis C++
c++开发redis module问题之避免多个C++模块之间因重载operator new而产生的冲突,如何解决
c++开发redis module问题之避免多个C++模块之间因重载operator new而产生的冲突,如何解决
|
3月前
|
存储 C++ 容器
【C++】开散列实现unordered_map与unordered_set的封装
【C++】开散列实现unordered_map与unordered_set的封装
40 0
下一篇
无影云桌面