C++ Standard Template Library (STL) 高级容器
更多 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
- http://www.cplusplus.com/reference/map/map/
- http://www.cnblogs.com/nzbbody/p/3409298.html
- internal (blog/86) by dph
- http://www.cplusplus.com/reference/set/set/
C++ Standard Template Library (STL) 高级容器的更多相关文章
- C++ Standard Template Library STL(undone)
目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...
- [c++] STL = Standard Template Library
How many people give up, because of YOU. Continue... 先实践,最后需要总结. 1. 数据流中的数据按照一定的格式<T>提取 ------ ...
- C++标准模板库Stand Template Library(STL)简介与STL string类
参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...
- <Standard Template Library>标准模板库专项复习总结(二)
4.队列 先进先出(FIFO)表 头文件:#include<queue> 变量的定义:queue<TYPE>queueName 成员函数: bool empty() 空队列返回 ...
- <Standard Template Library>标准模板库专项复习总结(一)
看了看博客园的申请时间也一年多了...想想自己一年多以来一直处于各种划水状态,现在又要面临ACM的冲击... 还是要抓紧时间赶紧复习一下了- -毕竟校园新生赛还是有奖金的.. 1.栈 先进后出(LIF ...
- C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...
- [C++ STL] 各容器简单介绍
什么是STL? 1.STL(Standard Template Library),即标准模板库,是一个高效的C++程序库. 2.包含了诸多常用的基本数据结构和基本算法.为广大C++程序员们提供了一个可 ...
- STL List容器
转载http://www.cnblogs.com/fangyukuan/archive/2010/09/21/1832364.html 各个容器有很多的相似性.先学好一个,其它的就好办了.先从基础开始 ...
- STL之容器适配器queue的实现框架
说明:本文仅供学习交流,转载请标明出处,欢迎转载! 上篇文章STL之容器适配器stack的实现框架已经介绍了STL是怎样借助基础容器实现一种经常使用的数据结构stack (栈),本文介绍下第二种STL ...
随机推荐
- 《Python Data Structures》Week5 Dictionary 课堂笔记
Coursera课程<Python Data Structures> 密歇根大学 Charles Severance Week5 Dictionary 9.1 Dictionaries 字 ...
- 16/7/7_PHP-构造\解析函数
昨天又问老师问题,老师还是强调要用博客之类记录下每天的学习的习惯. 我个人的想法是一些知识点不用笔记一下 在脑海的理解不是太深.但是老师都这么说了我老师乖乖的记录下今天的学习的一些知识.心得.技巧等等 ...
- 应用安全-Web安全-SSRF攻防
原理 服务器: IP:.XX.191.14 nc -l -p 客户端: http://xx.map.xx.com/maps/services/thumbnails?width=215&heig ...
- 第一个spring boot应用
前提 首先要确保已经安装了java和maven: $ java -version java version "1.8.0_102" Java(TM) SE Runtime Envi ...
- Linux内核模块(Module)初解
#include <linux/init.h> // __init __exit #include <linux/module.h> // module_init module ...
- python学习第四十九天XML模块的用法
xml是实现不通语言或程序之间进行数据交换的协议,跟json差不多,但是json用起来简单,还没诞生json,以前都是用xml,下面讲述XML模块的用法. 1,导入xml模块 import xml 2 ...
- python字符串中的转义符
python字符串中的转义符 1,单引号,双引号,三引号 a='www.96net.com.cn',b="96net.com.cn",c="""96n ...
- c#用log4Net将日志写入到Oracle数据库,并写入到文件中
原文:c#用log4Net将日志写入到Oracle数据库,并写入到文件中 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https:/ ...
- OPECV的配置
因为要做一点道路是别的东西,所以想用到OPENCV的一些东西.在网上找了一些OPENCCSHARP的代码,但是这方面的书籍或者资料还是不是特别的多,所以我就觉得可能还不是很好.主要的原因还是因为自己的 ...
- Spark2.0集成Hive操作的相关配置与注意事项
前言 已完成安装Apache Hive,具体安装步骤请参照,Linux基于Hadoop2.8.0集群安装配置Hive2.1.1及基础操作 补充说明 Hive中metastore(元数据存储)的三种方式 ...