更多 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. Caffe深度学习计算框架

    Caffe | Deep Learning Framework是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的 Yangqing Jia,目前在Google工作.Caffe是 ...

  2. 练习2:python-把excel表格中某张表的内容导入sqlite

    前言:最新需要用到大批量的数据,在excel造好数据之后,存储在数据库库中,方便调用数据,于是就想着用python语言写一下这个过程 python有个openpyxl的模块,可以直接用来对于excel ...

  3. [9期]软WAF上传绕过+webshell免杀

    安全狗上传绕过 思路: 1.扰乱编码 form-data 替换成 ~form-data           form-data    改成 f+orm-data form-data    改成 for ...

  4. 使用SqlBulkCopy 批量操作大量数据

    private void button1_Click(object sender, EventArgs e) { //1.0 构建一个内存表一定要和Users表的结构保持一致,除了自增的主键外 Dat ...

  5. [Web 前端] 004 html 小练习

    1. 锚点 用法 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  6. Eclipse查看jdk源码(Ctrl+左键)

    Window ->Preferences ->Java ->Installed JREs ->选中jdk ->Edit ->选中rt.jar ->source ...

  7. WPF ControlTemplate

    ControlTemplate:控件模板,顾名思义也就是定制特定的控件供公共调用,有点类似WinForm中对一些通用控件进行重写使用. ControlTemplate:控件模板主要有两个重要属性:Vi ...

  8. 如何使用IDEA将项目上传到GitHub中

    上传之前先规定上传的格式: 1 . 以后所有上传的项目,都只上传 src文件集以及pom.xml文件,不要带有自己的 .idea配置文件或者target运行文件之类的(就算是测试文件也一样,从开始就养 ...

  9. BZOJ 1875(DP+矩阵快速幂)

    题面 传送门 分析 容易想到根据点来dp,设dp[i][j]表示到i点路径长度为j的方案数 状态转移方程为dp[i][k]=∑(i,j)∈Edp[j][k−1]" role="pr ...

  10. SQLServer查看及设置最大连接数

    很多时候自己本地开发会遇到 ,打开几个连接正常访问 之后就报错误,这时候需要调整sqlserver 最大连接数. 1. 查询最大连接数 SELECT value_in_useFROM sys.conf ...