更多 STL 数据结构请阅读 NOIp 数据结构专题总结(STL structure 章节)

std::map

Definition:

template < class Key,                                     // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;

In a map, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ, and are grouped together in member type value_type, which is a pair type combining both:

typedef pair<const Key, T> value_type;

[WARNING] 如果不检查,直接返回 map[key],可能会出现意想不到的行为。如果 map 包含 key,没有问题,如果 map 不包含 key,使用下标有一个危险的副作用,会在 map 中插入一个 key 的元素,value 取默认值,返回 value。也就是说,map[key] 不可能返回 null

Functions:

count(): Count elements with a specific key

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).

find(): Get iterator to element (Recommend)

Searches the container for an element with a key equivalent to k and returns an iterator to it if found, otherwise it returns an iterator to map::end.

iter = m.find(key);
if (iter != m.end()) {
return iter->second;
}
return null;

clear(): 清空 map

empty(): 判断是否为空 返回 0,1

std::set

Definition:

template < class T,                        // set::key_type/value_type
class Compare = less<T>, // set::key_compare/value_compare
class Alloc = allocator<T> // set::allocator_type
> class set;

In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.

Functions:

clear() , 删除set容器中的所有的元素

empty() , 判断set容器是否为空

max_size() , 返回set容器可能包含的元素最大个数

size() , 返回当前set容器中的元素个数

count() 用来查找set中某个某个键值出现的次数。这个函数在set并不是很实用,因为一个键值在set只可能出现0或1次,这样就变成了判断某一键值是否在set出现过了。

begin() , 返回set容器的第一个元素

end() , 返回set容器的最后一个元素

erase(iterator) , 删除定位器iterator指向的值

erase(first,second) , 删除定位器first和second之间的值

erase(key_value) , 删除键值key_value的值

find() ,返回给定值值得定位器,如果没找到则返回end()

lower_bound(key_value) ,返回第一个大于等于key_value的定位器

upper_bound(key_value),返回最后一个大于等于key_value的定位器


References

  1. http://www.cplusplus.com/reference/map/map/
  2. http://www.cnblogs.com/nzbbody/p/3409298.html
  3. internal (blog/86) by dph
  4. http://www.cplusplus.com/reference/set/set/

C++ Standard Template Library (STL) 高级容器的更多相关文章

  1. C++ Standard Template Library STL(undone)

    目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...

  2. [c++] STL = Standard Template Library

    How many people give up, because of YOU. Continue... 先实践,最后需要总结. 1. 数据流中的数据按照一定的格式<T>提取 ------ ...

  3. C++标准模板库Stand Template Library(STL)简介与STL string类

    参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...

  4. <Standard Template Library>标准模板库专项复习总结(二)

    4.队列 先进先出(FIFO)表 头文件:#include<queue> 变量的定义:queue<TYPE>queueName 成员函数: bool empty() 空队列返回 ...

  5. <Standard Template Library>标准模板库专项复习总结(一)

    看了看博客园的申请时间也一年多了...想想自己一年多以来一直处于各种划水状态,现在又要面临ACM的冲击... 还是要抓紧时间赶紧复习一下了- -毕竟校园新生赛还是有奖金的.. 1.栈 先进后出(LIF ...

  6. C++ STL vector容器学习

    STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...

  7. [C++ STL] 各容器简单介绍

    什么是STL? 1.STL(Standard Template Library),即标准模板库,是一个高效的C++程序库. 2.包含了诸多常用的基本数据结构和基本算法.为广大C++程序员们提供了一个可 ...

  8. STL List容器

    转载http://www.cnblogs.com/fangyukuan/archive/2010/09/21/1832364.html 各个容器有很多的相似性.先学好一个,其它的就好办了.先从基础开始 ...

  9. STL之容器适配器queue的实现框架

    说明:本文仅供学习交流,转载请标明出处,欢迎转载! 上篇文章STL之容器适配器stack的实现框架已经介绍了STL是怎样借助基础容器实现一种经常使用的数据结构stack (栈),本文介绍下第二种STL ...

随机推荐

  1. DirectX 安装报错: 不能信任一个安装所需的压缩文件,请检查加密服务是否启用并且cabinet文件证书是否有效

    DirectX 安装报错 不能信任一个安装所需的压缩文件,请检查加密服务是否启用并且cabinet文件证书是否有效 是直播软件open broadcaster software,这个软件安装的时候提示 ...

  2. Shell脚本中单引号(‘)和双引号(“)的使用区别

    在Linux操作系统上编写Shell脚本时候,我们是在变量的前面使用$符号来获取该变量的值,通常在脚本中使用”$param”这种带双引号的格式,但也有出现使用'$param'这种带引号的使用的场景,首 ...

  3. failed to create process ,pip报错问题

  4. [Usaco2014 Feb] Roadblock

    有一个无向图,共N个节点,编号1至N,共M条边.FJ在节点1,它想到达节点N.FJ总是会选择最短路径到达节点N .作为捣蛋的奶牛Bessie,它想尽量延迟FJ到达节点N的时间,于是Bessie决定从M ...

  5. [Web 前端] 020 css 定位之绑定定位

    绑定定位 少废话,上例子 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  6. jquery悬停和移出事件

    $('#hides').mouseover(function () { alert("sdfdsf")}).mouseout(function () { alert("啊 ...

  7. El 表达式和 Jstl 标签库

    El 表达式学习 1. 什么是 EL 表达式 全称:Expression Language,一种写法非常简介的表达式.语法简单易懂,便于使用.表达式语言的灵感来自于 ECMAScript 和XPath ...

  8. ajax传文件用express的multer接住

    html部分: //input type设为file <input type="file" name="file" id="fileInputE ...

  9. 什么是 Python 的命名空间?

    在 Python 中,所有的名字都存在于一个空间中,它们在该空间中存在和被操作——这就是命名空间.它就好像一个盒子,每一个变量名字都对应装着一个对象.当查询变量的时候,会从该盒子里面寻找相应的对象.

  10. 音频视频的播放的进度调整(以.net为例)

    Background:对于音视频在线播放,一些小应用是靠nginx处理访问视频.音频文件的请求,对外应用的一般会托管至各种云上使用相关的服务.前者存在巨大的安全隐患,后者会有一定的成本.有的时候还是需 ...