\

http://morningspace.51.net/resource/stlintro/stlintro.html

标准容器

C++标准容器分为序列容器和关联容器,对于序列容器,C++提供的基本序列有

vector 支持随机访问,不适合做插入和删除操作频繁的场景

list 双向链表,适合做元素的插入和删除,不是随机访问

deque 也是一个双端序列,但是经过优化,其双端操作效率类似list,随即访问效率接近vector。

从它们出发,通过定义适当的借口,生成了

stack 默认用deque实现

queue 默认是deque实现

priority_queue 默认是vector保存元素,实现最可能是heap

对于关联容器,C++提供的有:

map 映射

mulitimap 多重映射,相比map,允许重复的key

set 被看做是一个map,其中的值是无关紧要的

mulitiset 相比set,允许重复的key

bitset 位集合

hash_map 散列映射,通过实现一个散列函数,将容器实现为一个散列表,以减少查找元素所需要的时间

标准容器具体用法可以参考C++在线手册STL容器:http://www.cplusplus.com/reference/stl/

Standard library[edit]

The C++ standard consists of two parts: the core language and the C++ Standard Library. C++ programmers expect the latter on every major implementation of C++; it includes vectors, lists, maps, algorithms (find, for_each, binary_search, random_shuffle, etc.), sets, queues, stacks, arrays, tuples, input/output facilities (iostream, for reading from and writing to the console and files), smart pointers for automatic memory management, regular expression support, multi-threading library, atomics support (allowing a variable to be read or written to by at most one thread at a time without any external synchronisation), time utilities (measurement, getting current time, etc.), a system for converting error reporting that doesn't use C++ exceptions into C++ exceptions, a random number generator and a slightly modified version of the C standard library (to make it comply with the C++ type system).

A large part of the C++ library is based on the Standard Template Library (STL). Useful tools provided by the STL include containers as the collections of objects (such as vectors and lists), iterators that provide array-like access to containers, and algorithms that perform operations such as searching and sorting.

Furthermore, (multi)maps (associative arrays) and (multi)sets are provided, all of which export compatible interfaces. Therefore, using templates it is possible to write generic algorithms that work with any container or on any sequence defined by iterators. As in C, the features of the libraryare accessed by using the #include directive to include a standard header. C++ provides 105 standard headers, of which 27 are deprecated.

The standard incorporates the STL that was originally designed by Alexander Stepanov, who experimented with generic algorithms and containers for many years. When he started with C++, he finally found a language where it was possible to create generic algorithms (e.g., STL sort) that perform even better than, for example, the C standard library qsort, thanks to C++ features like using inlining and compile-time binding instead of function pointers. The standard does not refer to it as "STL", as it is merely a part of the standard library, but the term is still widely used to distinguish it from the rest of the standard library (input/output streams, internationalization, diagnostics, the C library subset, etc.).[50]

Most C++ compilers, and all major ones, provide a standards conforming implementation of the C++ standard library.

算法和函数对象

容器本身之所以有用,是因为容器提供了一些基本操作,如确定大小,迭代,复制,排序,查找等。标准库提供了许多算法,服务于容器用户的最普遍和最基本的需要。

标准库算法不过就是60个,每个算法都描述为一个模板函数或一组模板函数,例如最常用的排序算法:sort,能以很好的平均效率排序,建议排序都用sort替换C语言的qsort,sort可以更好结合容器。

标准库算法和函数对象的教程可以参考C++之父的C++程序设计语言的算法和函数对象,这一章内容可以教你如何使用标准库算法和函数对象。

标准库算法的声明在<algorithm>,函数对象在<functional>里。

标准库算法具体用法参考C++在线手册STL算法:http://www.cplusplus.com/reference/algorithm/

迭代器和分配器

迭代器是连接容器和算法的纽带,让写算法的人不必关心各种数据结构的具体细节,而分配器提供了一个映射,将低级的字节形式的数据模型映射到高级的对象模型。

迭代器是每个程序员都需要关心的概念之一,但是分配器仅仅是一个支持机制,标准库已经提供了默认的分配器,很少有程序员需要去写新的分配器。

迭代器在接触STL容器就会了解了,一般迭代器分为正向迭代器,反向迭代器,插入迭代器,带检查的迭代器。

至于分配器提供了一套分配和释放存储的标准方式,标准库提供了一个标准分配器,在<memory>里的标准allocator模板用operator new()分配存储,所有的标准容器在默认情况下使用它,当然你也可以自己实现一个分配器,容器的实现需要一次次的allocate()或者deallocate()对象,意味着new的大量调用,你可以采用一个固定大小存储块的存储池,可以比常规的更通用的operator new()的效率高一些。

container,algorith,iterate的更多相关文章

  1. Hiero中versionscanner模块结构图

    花了两周读这个模块,终于把结构理清楚了,当然新功能也搞定了,搜索条件更宽松,可以找到binitem对象中更多的版本,截图如下: 当然功能也做出来啦: 代码如下: ################### ...

  2. golang中container/list包源码分析

    golang源码包中container/list实际上是一个双向链表 提供链表的一些基本操作,下面就结合定义和接口进行下说明 1. 定义 // Element is an element of a l ...

  3. 在docker中运行ASP.NET Core Web API应用程序(附AWS Windows Server 2016 widt Container实战案例)

    环境准备 1.亚马逊EC2 Windows Server 2016 with Container 2.Visual Studio 2015 Enterprise(Profresianal要装Updat ...

  4. .Container与.container_fluid区别

    .Container与.container_fluid是bootstrap中的两种不同类型的外层容器,两者的区别是:.container 类用于固定宽度并支持响应式布局的容器..container-f ...

  5. View and Data API Tips: Constrain Viewer Within a div Container

    By Daniel Du When working with View and Data API, you probably want to contain viewer into a <div ...

  6. [LeetCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  7. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  8. iBatis.net 循环iterate,没有foreach

    3.9.4. Iterate Element This tag will iterate over a collection and repeat the body content for each ...

  9. Docker - command in docker container

    1.查看Container 里面运行的进程 在运行容器以后,可以查看里面的进程: docker top <container_id> or <container_name> 2 ...

随机推荐

  1. JAX-WS使用Handler实现简单的WebService权限验证

    WebService如果涉及到安全保密或者使用权限的时候,WS-Security通常是最优选择.WS-Security (Web服务安全) 包含了关于如何在WebService消息上保证完整性和机密性 ...

  2. 关于HashMap初始化容量问题

    使用阿里云代码规范插件扫描后出现以下提示: hashmap should set a size when initalizing,即hashmap应该在初始化时设置一个大小 在网上搜到一篇讲解(htt ...

  3. Java的==与equals之辨,简单解释,很清楚

    "=="和equals方法究竟有什么区别? (单独把一个东西说清楚,然后再说清楚另一个,这样,它们的区别自然就出来了,混在一起说,则很难说清楚) ==操作符专门用来比较两个变量的值 ...

  4. Hbase分布式安装部署过程

    系统 Red hat linux 6.4 Hadoop版本 1.2.1 Hbase版本 0.94.16 Hbase的完全分布式安装概述: 1. 配置hosts,确保涉及的主机名均可解析为ip 2. 编 ...

  5. log4cplus基本用法

    说起日志系统,不得不提大名鼎鼎的Log4j.特别是使用Java的人们,能够说是无人不知无人不晓无人不用. Log4j以其简单的使用方式(引入一个jar包.一行代码就可以调用).灵活(可通过配置文件任意 ...

  6. TensorFlow基础笔记(4) Tensor Transformation

    https://segmentfault.com/a/1190000008793389 抽取 tf.slice(input_, begin, size, name=None):按照指定的下标范围抽取连 ...

  7. qt 使用多个ui文件

    一般的QT工程只有一个ui,本文记录如何在一个工程中使用多个ui文件. 参考链接: http://www.cnblogs.com/lc-cnblong/p/3182903.html 创建方法,鼠标在工 ...

  8. JAVA在语言级支持多线程

    进程:任务 任务并发执行是一个宏观概念,微观上是串行的. 进程的调度是有OS负责的(有的系统为独占式,有的系统为共享式,根据重要性,进程有优先级). 由OS将时间分为若干个时间片. JAVA在语言级支 ...

  9. web 前端 转盘界面

    http://www.cnblogs.com/arfeizhang/p/turntable.html "如果有个做转盘的需求,你准备怎么做?设计师只会提供一个转盘的图片,其余都需要你完成,不 ...

  10. SSH开发环境整合搭建

    1.建立动态web工程,加入必要的jar包. antlr-2.7.7.jar asm-3.3.jar asm-commons-3.3.jar asm-tree-3.3.jar c3p0-0.9.1.2 ...