zoukankan      html  css  js  c++  java
  • container,algorith,iterate

    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()的效率高一些。

  • 相关阅读:
    路由器竟装有隐藏摄像头!游客青岛民宿上演教科书式反偷拍
    21天单词速记训练营 第1天~ 21天 加油! (雅途右脑单词速记)
    词汇速记训练营
    词汇速记训练营
    词汇速记训练营
    Windows下解决github push failed (remote: Permission to userA/XXXX.git denied to userB.) 上传gitHub失败报错
    WMITools修复wmi劫持--hao643.com/jtsh123劫持(修改快捷方式跳转至hao123.com)
    Ubuntu下载gitea
    go+cookie+angular踩过的坑
    golang中创建logger时候踩过的坑
  • 原文地址:https://www.cnblogs.com/SZLLQ2000/p/5275934.html
Copyright © 2011-2022 走看看