\

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. C++链接ODBC数据源:VS2013,Access

    参考资料:1.http://wenku.baidu.com/view/a92d1a812cc58bd63186bd8d.html 2.http://blog.sina.com.cn/s/blog_68 ...

  2. androidStudio简便安装

    最近在公司由eclipse换为androidstudio,说句实话,androidstudio还是蛮好用的,但是自己刚刚安装的时候遇到很多的问题,问了度娘,各种说法都有,还是捣鼓不得,于是自己尝试,弄 ...

  3. JS学习笔记(2)--正则表达式获取指定字符串

    js 正则提取字串 这里就有:SA 怎么用正则提取sa出来 var str=“这里就有:SA ”怎么用正则提取sa出来 YDhcui | 浏览 2087 次 推荐于2016-05-30 18:25:4 ...

  4. 读取大csv文件数据插入到MySql或者Oracle数据库通用处理

    import java.io.BufferedInputStream; import java.io.BufferedReader;import java.io.BufferedWriter;impo ...

  5. 安装expect命令 两种方式

    yum安装 yum -y install expect 手动安装 expect以及tcl版本 #!/bin/bash oldpath=`pwd` tar -zxf tcl8.4.20-src.tar. ...

  6. 深入理解JVM--JVM垃圾回收机制(转)

    Java语言出来之前,大家都在拼命的写C或者C++的程序,而此时存在一个很大的矛盾,C++等语言创建对象要不断的去开辟空间,不用的时候有需要不断的去释放控件,既要写构造函数,又要写析构函数,很多时候都 ...

  7. list中存放map实例

    list中存放map实例 2016年08月08日 18:46:14 阅读数:22279 List中存放Map遍历输出的实例   import java.util.ArrayList;   import ...

  8. Java解析json(二):jackson

    Java解析json(二):jackson   官方参考 Jackson Home Page:https://github.com/FasterXML/jackson Jackson Wiki:htt ...

  9. PHP之PHP文件引用详解

    HP的文件引用涉及到四个函数: 文件引用 1.include()2.include_once()3.require()4.require_once() 这四个函数常常会给PHP初学者造成困扰,总的来说 ...

  10. datagrid columns

    columns: [[ { field: 'Source_Id', title: 'Source_Id', hidden: true }, //{ field: 'Current_Value', hi ...