C++ STC常用容器list vector map总结
一:list
#include <list>
list 是一个双向链表,可以高效的进行插入和删除操作;但是由于是链表,所以进行固定某一位置的访问时效率会比较低。
而且由于它的空间是不连续的,所以不支持用下标去随机访问。(特点和vector正好相反)
二:vector
#include <vector>
vector是一个动态数组,由于是数组,所以他的插入和删除操作都会导致其他数据的位置移动,所以插入和删除没有list高效。
但是他对固定位置的数据访问就比list高效很多。他比list多了vector::at()函数,可以让用户直接去访问某个位置的指,也支持直接像c语言一样用数组下标去访问。
关于list和vector的选择,如果是有对某个位置有固定访问的场景,比如房间号、电梯楼层,可以选择用vector;其他用不到固定位置访问的,可以用list,比如数据的缓存,需要快速的插入和取出。
函数 |
表述 |
c.assign(beg,end) c.assign(n,elem) |
将[beg; end)区间中的数据赋值给c。 将n个elem的拷贝赋值给c。 |
c.at(idx) |
传回索引idx所指的数据,如果idx越界,抛出out_of_range。 |
c.back() |
传回最后一个数据,不检查这个数据是否存在。 |
c.begin() |
传回迭代器重的可一个数据。 |
c.capacity() |
返回容器中数据个数。 |
c.clear() |
移除容器中所有数据。 |
c.empty() |
判断容器是否为空。 |
c.end() |
指向迭代器中的最后一个数据地址。 |
c.erase(pos) c.erase(beg,end) |
删除pos位置的数据,传回下一个数据的位置。 删除[beg,end)区间的数据,传回下一个数据的位置。 |
c.front() |
传回第一个数据。 |
get_allocator |
使用构造函数返回一个拷贝。 |
c.insert(pos,elem) c.insert(pos,n,elem) c.insert(pos,beg,end) |
在pos位置插入一个elem拷贝,传回新数据位置。 在pos位置插入n个elem数据。无返回值。 在pos位置插入在[beg,end)区间的数据。无返回值。 |
c.max_size() |
返回容器中最大数据的数量。 |
c.pop_back() |
删除最后一个数据。 |
c.push_back(elem) |
在尾部加入一个数据。 |
c.rbegin() |
传回一个逆向队列的第一个数据。 |
c.rend() |
传回一个逆向队列的最后一个数据的下一个位置。 |
c.resize(num) |
重新指定队列的长度。 |
c.reserve() |
保留适当的容量。 |
c.size() |
返回容器中实际数据的个数。 |
c1.swap(c2) swap(c1,c2) |
将c1和c2元素互换。 同上操作。 |
vector<Elem> c vector <Elem> c1(c2) vector <Elem> c(n) vector <Elem> c(n, elem) vector <Elem> c(beg,end) c.~ vector <Elem>() |
创建一个空的vector。 复制一个vector。 创建一个vector,含有n个数据,数据均已缺省构造产生。 创建一个含有n个elem拷贝的vector。 创建一个以[beg;end)区间的vector。 销毁所有数据,释放内存。 |
三:map
#include <map[>
std::map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下std map内部数据的组织,std map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这树具有对数据自动排序的功能,所以在std map内部所有的数据都是有序的。
Member classes
compares objects of type value_type (class) |
Member functions
constructs the map (public member function) |
|
destructs the map (public member function) |
|
assigns values to the container (public member function) |
|
returns the associated allocator (public member function) |
|
Element access |
|
(C++11) |
access specified element with bounds checking (public member function) |
access or insert specified element (public member function) |
|
Iterators |
|
returns an iterator to the beginning (public member function) |
|
returns an iterator to the end (public member function) |
|
returns a reverse iterator to the beginning (public member function) |
|
returns a reverse iterator to the end (public member function) |
|
Capacity |
|
checks whether the container is empty (public member function) |
|
returns the number of elements (public member function) |
|
returns the maximum possible number of elements (public member function) |
|
Modifiers |
|
clears the contents (public member function) |
|
inserts elements or nodes (since C++17) (public member function) |
|
(C++17) |
inserts an element or assigns to the current element if the key already exists (public member function) |
(C++11) |
constructs element in-place (public member function) |
(C++11) |
constructs elements in-place using a hint (public member function) |
(C++17) |
inserts in-place if the key does not exist, does nothing if the key exists (public member function) |
erases elements (public member function) |
|
swaps the contents (public member function) |
|
(C++17) |
extracts nodes from the container (public member function) |
(C++17) |
splices nodes from another container (public member function) |
Lookup |
|
returns the number of elements matching specific key (public member function) |
|
finds element with specific key (public member function) |
|
(C++20) |
checks if the container contains element with specific key (public member function) |
returns range of elements matching a specific key (public member function) |
|
returns an iterator to the first element not less than the given key (public member function) |
|
returns an iterator to the first element greater than the given key (public member function) |
|
Observers |
|
returns the function that compares keys (public member function) |
|
returns the function that compares keys in objects of type value_type (public member function) |
Non-member functions
lexicographically compares the values in the map (function template) |
|
specializes the std::swap algorithm (function template) |