\

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. 82. Single Number【easy】

    Given 2*n + 1 numbers, every numbers occurs twice except one, find it.   Example Given [1,2,2,1,3,4, ...

  2. 基于redis的分布式缓存disgear开源到github上了

    disgear是笔者参考solrcloud架构基于redis实现的分布式的缓存,支持数据切分到多台机器上,支持HA,支持读写分离和主节点失效自动选举,目前把它开放到github上,开放给大家 gith ...

  3. 1、Reactive Extensions for .NET(译)

    注:本文的工程是基于 vs2010 的,在 vs2012 中区别不大. 本文的意图是让读者熟悉 Reactive Extension for .net(Rx) 的使用.通过一系列的例子,让读者感受 基 ...

  4. 延时NSTimer

    import Foundationimport UIKit class YijfkController:UIViewController{ override func viewDidLoad() { ...

  5. iOS开发 支持https请求以及ssl证书配置(转)

    原文地址:http://blog.5ibc.net/p/100221.html 众所周知,苹果有言,从2017年开始,将屏蔽http的资源,强推https 楼主正好近日将http转为https,给还没 ...

  6. 转:Andriod studio技巧合集

    1. 书签(Bookmarks) 描述:这是一个很有用的功能,让你可以在某处做个标记(书签),方便后面再跳转到此处. 调用:Menu → Navigate → Bookmarks 快捷键: 添加/移除 ...

  7. span和img标签对齐

    html代码 <li> <span class="left_item">在线</span> <img class="right_ ...

  8. jQuery实现淡入淡出二级下拉导航菜单的方法

    本文实例讲述了jQuery实现淡入淡出二级下拉导航菜单的方法.分享给大家供大家参考.具体如下: 这是一款基于jQuery实现的导航菜单,淡入淡出二级的菜单导航,很经常见到的效果,这里使用的是jquer ...

  9. img与特殊布局下对浏览器渲染的剖析

    补白 在内联元素中,分为替换元素和非替换元素(不了解的同学可以百度一下),非替换元素是不可以设置尺寸的,而替换元素作为特殊的内联元素,由于其自身拥有尺寸属性,所以其的尺寸是可以进行再次设置的. 此文适 ...

  10. iOS开发之--使用storyboard进行跳转

    iOS开发中使用故事板进行开发是非常高效的一种方式,虽然有这样那样的问题,但是不得不承认,使用sb可以在最短的时间内完成整个项目的布局,节约开发者大量的时间,而且便于修改,非常直观,虽然可能不太灵活, ...