mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
设置md代码块为cpp格式
This commit is contained in:
parent
c7812d2e41
commit
7f88f8bac8
254
STL/STL.md
254
STL/STL.md
@ -35,7 +35,7 @@ array是固定大小的顺序容器,它们保存了一个以严格的线性顺
|
||||
|
||||
数组容器的另一个独特特性是它们可以被当作元组对象来处理:array头部重载get函数来访问数组元素,就像它是一个元组,以及专门的tuple_size和tuple_element类型。
|
||||
|
||||
```
|
||||
```cpp
|
||||
template < class T, size_t N > class array;
|
||||
```
|
||||
|
||||
@ -47,14 +47,14 @@ template < class T, size_t N > class array;
|
||||
|
||||

|
||||
|
||||
```
|
||||
```cpp
|
||||
iterator begin() noexcept;
|
||||
const_iterator begin() const noexcept;
|
||||
```
|
||||
|
||||
Example
|
||||
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -78,14 +78,14 @@ myarray contains: 2 16 77 34 50
|
||||
|
||||
返回指向数组容器中最后一个元素之后的理论元素的迭代器。
|
||||
|
||||
```
|
||||
```cpp
|
||||
iterator end() noexcept;
|
||||
const_iterator end() const noexcept;
|
||||
```
|
||||
|
||||
Example
|
||||
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -111,12 +111,12 @@ myarray contains: 5 19 77 34 99
|
||||
|
||||
返回指向数组容器中最后一个元素的反向迭代器。
|
||||
|
||||
```
|
||||
```cpp
|
||||
reverse_iterator rbegin()noexcept;
|
||||
const_reverse_iterator rbegin()const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -140,13 +140,13 @@ myarray contains: 14 80 26 4
|
||||
|
||||
返回一个反向迭代器,指向数组中第一个元素之前的理论元素(这被认为是它的反向结束)。
|
||||
|
||||
```
|
||||
```cpp
|
||||
reverse_iterator rend() noexcept;
|
||||
const_reverse_iterator rend() const noexcept;
|
||||
```
|
||||
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -170,11 +170,11 @@ myarray contains: 14 80 26 4
|
||||
#### array::cbegin
|
||||
|
||||
返回指向数组容器中第一个元素的常量迭代器(const_iterator);这个迭代器可以增加和减少,但是不能用来修改它指向的内容。
|
||||
```
|
||||
```cpp
|
||||
const_iterator cbegin()const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -199,11 +199,11 @@ myarray contains: 2 16 77 34 50
|
||||
|
||||
#### array::cend
|
||||
返回指向数组容器中最后一个元素之后的理论元素的常量迭代器(const_iterator)。这个迭代器可以增加和减少,但是不能用来修改它指向的内容。
|
||||
```
|
||||
```cpp
|
||||
const_iterator cend() const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -227,11 +227,11 @@ myarray contains: 2 16 77 34 50
|
||||
|
||||
#### array::crbegin
|
||||
返回指向数组容器中最后一个元素的常量反向迭代器(const_reverse_iterator)
|
||||
```
|
||||
```cpp
|
||||
const_reverse_iterator crbegin()const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -256,11 +256,11 @@ myarray backwards: 60 50 40 30 20 10
|
||||
|
||||
返回指向数组中第一个元素之前的理论元素的常量反向迭代器(const_reverse_iterator),它被认为是其反向结束。
|
||||
|
||||
```
|
||||
```cpp
|
||||
const_reverse_iterator crend() const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -286,11 +286,11 @@ myarray backwards: 60 50 40 30 20 10
|
||||
|
||||
返回数组容器中元素的数量。
|
||||
|
||||
```
|
||||
```cpp
|
||||
constexpr size_type size()noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -310,11 +310,11 @@ sizeof(myints): 20
|
||||
```
|
||||
#### array::max_size
|
||||
返回数组容器可容纳的最大元素数。数组对象的max_size与其size一样,始终等于用于实例化数组模板类的第二个模板参数。
|
||||
```
|
||||
```cpp
|
||||
constexpr size_type max_size() noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -335,11 +335,11 @@ max_size of myints: 10
|
||||
|
||||
#### array::empty
|
||||
返回一个布尔值,指示数组容器是否为空,即它的size()是否为0。
|
||||
```
|
||||
```cpp
|
||||
constexpr bool empty() noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -359,12 +359,12 @@ second is not empt
|
||||
```
|
||||
#### array::operator[]
|
||||
返回数组中第n个位置的元素的引用。与array::at相似,但array::at会检查数组边界并通过抛出一个out_of_range异常来判断n是否超出范围,而array::operator[]不检查边界。
|
||||
```
|
||||
```cpp
|
||||
reference operator[] (size_type n);
|
||||
const_reference operator[] (size_type n) const;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -392,12 +392,12 @@ myarray contains: 0 1 2 3 4 5 6 7 8 9
|
||||
```
|
||||
#### array::at
|
||||
返回数组中第n个位置的元素的引用。与array::operator[]相似,但array::at会检查数组边界并通过抛出一个out_of_range异常来判断n是否超出范围,而array::operator[]不检查边界。
|
||||
```
|
||||
```cpp
|
||||
reference at ( size_type n );
|
||||
const_reference at ( size_type n ) const;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -426,12 +426,12 @@ myarray contains: 0 1 2 3 4 5 6 7 8 9
|
||||
#### array::front
|
||||
返回对数组容器中第一个元素的引用。array::begin返回的是迭代器,array::front返回的是直接引用。
|
||||
在空容器上调用此函数会导致未定义的行为。
|
||||
```
|
||||
```cpp
|
||||
reference front();
|
||||
const_reference front() const;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -461,12 +461,12 @@ myarray now contains: 100 16 77
|
||||
#### array::back
|
||||
返回对数组容器中最后一个元素的引用。array::end返回的是迭代器,array::back返回的是直接引用。
|
||||
在空容器上调用此函数会导致未定义的行为。
|
||||
```
|
||||
```cpp
|
||||
reference back();
|
||||
const_reference back() const;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -496,12 +496,12 @@ myarray now contains: 5 19 50
|
||||
返回指向数组对象中第一个元素的指针。
|
||||
|
||||
由于数组中的元素存储在连续的存储位置,所以检索到的指针可以偏移以访问数组中的任何元素。
|
||||
```
|
||||
```cpp
|
||||
value_type* data() noexcept;
|
||||
const value_type* data() const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <array>
|
||||
@ -524,11 +524,11 @@ Test string
|
||||
```
|
||||
#### array::fill
|
||||
用val填充数组所有元素,将val设置为数组对象中所有元素的值。
|
||||
```
|
||||
```cpp
|
||||
void fill (const value_type& val);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -553,11 +553,11 @@ myarray contains: 5 5 5 5 5 5
|
||||
通过x的内容交换数组的内容,这是另一个相同类型的数组对象(包括相同的大小)。
|
||||
|
||||
与其他容器的交换成员函数不同,此成员函数通过在各个元素之间执行与其大小相同的单独交换操作,以线性时间运行。
|
||||
```
|
||||
```cpp
|
||||
void swap (array& x) noexcept(noexcept(swap(declval<value_type&>(),declval<value_type&>())));
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -586,13 +586,13 @@ second: 10 20 30 40 50
|
||||
```
|
||||
#### get(array)
|
||||
形如:std::get<0>(myarray);传入一个数组容器,返回指定位置元素的引用。
|
||||
```
|
||||
```cpp
|
||||
template <size_t I,class T,size_t N> T&get(array <T,N>&arr)noexcept;
|
||||
template <size_t I,class T,size_t N> T && get(array <T,N> && arr)noexcept;
|
||||
template <size_t I,class T,size_t N> const T&get(const array <T,N>&arr)noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <tuple>
|
||||
@ -621,7 +621,7 @@ first element in mytuple: 10
|
||||
```
|
||||
#### relational operators (array)
|
||||
形如:arrayA != arrayB、arrayA > arrayB;依此比较数组每个元素的大小关系。
|
||||
```
|
||||
```cpp
|
||||
(1)
|
||||
template <class T,size_T N>
|
||||
bool operator ==(const array <T,N>&lhs,const array <T,N>&rhs);
|
||||
@ -642,7 +642,7 @@ template <class T,size_T N>
|
||||
bool operator> =(const array <T,N>&lhs,const array <T,N>&rhs);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
|
||||
@ -689,7 +689,7 @@ vector是表示可以改变大小的数组的序列容器。
|
||||
* 在尾部增删元素 - 平摊(amortized)常数 O(1)}}
|
||||
* 增删元素 - 至 vector 尾部的线性距离 O(n)}}
|
||||
|
||||
```
|
||||
```cpp
|
||||
template < class T, class Alloc = allocator<T> > class vector;
|
||||
```
|
||||

|
||||
@ -710,7 +710,7 @@ x保持未指定但有效的状态。
|
||||
(6)初始化列表构造函数
|
||||
构造一个容器中的每个元件中的一个拷贝的IL,以相同的顺序。
|
||||
|
||||
```
|
||||
```cpp
|
||||
default (1)
|
||||
explicit vector (const allocator_type& alloc = allocator_type());
|
||||
fill (2)
|
||||
@ -732,7 +732,7 @@ vector (initializer_list<value_type> il,
|
||||
const allocator_type& alloc = allocator_type());
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -762,12 +762,12 @@ The contents of fifth are: 16 2 77 29
|
||||
```
|
||||
#### vector::~vector
|
||||
销毁容器对象。这将在每个包含的元素上调用allocator_traits::destroy,并使用其分配器释放由矢量分配的所有存储容量。
|
||||
```
|
||||
```cpp
|
||||
~vector();
|
||||
```
|
||||
#### vector::operator=
|
||||
将新内容分配给容器,替换其当前内容,并相应地修改其大小。
|
||||
```
|
||||
```cpp
|
||||
copy (1)
|
||||
vector& operator= (const vector& x);
|
||||
move (2)
|
||||
@ -812,11 +812,11 @@ Size of bar: 3
|
||||
|
||||
这是vector中保存的实际对象的数量,不一定等于其存储容量。
|
||||
|
||||
```
|
||||
```cpp
|
||||
size_type size() const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -848,11 +848,11 @@ Output
|
||||
返回该vector可容纳的元素的最大数量。由于已知的系统或库实现限制,
|
||||
|
||||
这是容器可以达到的最大潜在大小,但容器无法保证能够达到该大小:在达到该大小之前的任何时间,仍然无法分配存储。
|
||||
```
|
||||
```cpp
|
||||
size_type max_size() const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -885,12 +885,12 @@ max_size: 1073741823
|
||||
如果n也大于当前的容器的capacity(容量),分配的存储空间将自动重新分配。
|
||||
|
||||
注意这个函数通过插入或者删除元素的内容来改变容器的实际内容。
|
||||
```
|
||||
```cpp
|
||||
void resize (size_type n);
|
||||
void resize (size_type n, const value_type& val);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -919,11 +919,11 @@ myvector contains: 1 2 3 4 5 100 100 100 0 0 0 0
|
||||
```
|
||||
#### vector::capacity
|
||||
返回当前为vector分配的存储空间的大小,用元素表示。这个capacity(容量)不一定等于vector的size。它可以相等或更大,额外的空间允许适应增长,而不需要重新分配每个插入。
|
||||
```
|
||||
```cpp
|
||||
size_type capacity() const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -948,11 +948,11 @@ max_size: 1073741823
|
||||
```
|
||||
#### vector::empty
|
||||
返回vector是否为空(即,它的size是否为0)
|
||||
```
|
||||
```cpp
|
||||
bool empty() const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -986,11 +986,11 @@ total: 55
|
||||
在所有其他情况下,函数调用不会导致重新分配,并且vector容量不受影响。
|
||||
|
||||
这个函数对vector大小没有影响,也不能改变它的元素。
|
||||
```
|
||||
```cpp
|
||||
void reserve (size_type n);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -1041,11 +1041,11 @@ capacity changed: 100
|
||||
要求容器减小其capacity(容量)以适应其尺寸。
|
||||
|
||||
该请求是非绑定的,并且容器实现可以自由地进行优化,并且保持capacity大于其size的vector。 这可能导致重新分配,但对矢量大小没有影响,并且不能改变其元素。
|
||||
```
|
||||
```cpp
|
||||
void shrink_to_fit();
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -1085,7 +1085,7 @@ Possible output
|
||||
在初始化列表版本(3)中,新内容是以相同顺序作为初始化列表传递的值的副本。
|
||||
|
||||
所述内部分配器被用于(通过其性状),以分配和解除分配存储器如果重新分配发生。它也习惯于摧毁所有现有的元素,并构建新的元素。
|
||||
```
|
||||
```cpp
|
||||
range (1)
|
||||
template <class InputIterator>
|
||||
void assign (InputIterator first, InputIterator last);
|
||||
@ -1095,7 +1095,7 @@ initializer list (3)
|
||||
void assign (initializer_list<value_type> il);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -1185,12 +1185,12 @@ vector<_Tp,_Alloc>::operator=(const vector<_Tp, _Alloc>& __x)
|
||||
在vector的最后一个元素之后添加一个新元素。val的内容被复制(或移动)到新的元素。
|
||||
|
||||
这有效地将容器size增加了一个,如果新的矢量size超过了当前vector的capacity,则导致所分配的存储空间自动重新分配。
|
||||
```
|
||||
```cpp
|
||||
void push_back (const value_type& val);
|
||||
void push_back (value_type&& val);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -1215,11 +1215,11 @@ int main ()
|
||||
删除vector中的最后一个元素,有效地将容器size减少一个。
|
||||
|
||||
这破坏了被删除的元素。
|
||||
```
|
||||
```cpp
|
||||
void pop_back();
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -1250,7 +1250,7 @@ The elements of myvector add up to 600
|
||||
通过在指定位置的元素之前插入新元素来扩展该vector,通过插入元素的数量有效地增加容器大小。 这会导致分配的存储空间自动重新分配,只有在新的vector的size超过当前的vector的capacity的情况下。
|
||||
|
||||
由于vector使用数组作为其基础存储,因此除了将元素插入到vector末尾之后,或vector的begin之前,其他位置会导致容器重新定位位置之后的所有元素到他们的新位置。与其他种类的序列容器(例如list或forward_list)执行相同操作的操作相比,这通常是低效的操作。
|
||||
```
|
||||
```cpp
|
||||
single element (1)
|
||||
iterator insert (const_iterator position, const value_type& val);
|
||||
fill (2)
|
||||
@ -1264,7 +1264,7 @@ initializer list (5)
|
||||
iterator insert (const_iterator position, initializer_list<value_type> il);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -1326,12 +1326,12 @@ int main()
|
||||
这有效地减少了被去除的元素的数量,从而破坏了容器的大小。
|
||||
|
||||
由于vector使用一个数组作为其底层存储,所以删除除vector结束位置之后,或vector的begin之前的元素外,将导致容器将段被擦除后的所有元素重新定位到新的位置。与其他种类的序列容器(例如list或forward_list)执行相同操作的操作相比,这通常是低效的操作。
|
||||
```
|
||||
```cpp
|
||||
iterator erase (const_iterator position);
|
||||
iterator erase (const_iterator first, const_iterator last);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -1366,11 +1366,11 @@ myvector contains: 4 5 7 8 9 10
|
||||
在调用这个成员函数之后,这个容器中的元素是那些在调用之前在x中的元素,而x的元素是在这个元素中的元素。所有迭代器,引用和指针对交换对象保持有效。
|
||||
|
||||
请注意,非成员函数存在具有相同名称的交换,并使用与此成员函数相似的优化来重载该算法。
|
||||
```
|
||||
```cpp
|
||||
void swap (vector& x);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -1403,11 +1403,11 @@ bar contains: 100 100 100
|
||||
从vector中删除所有的元素(被销毁),留下size为0的容器。
|
||||
|
||||
不保证重新分配,并且由于调用此函数, vector的capacity不保证发生变化。强制重新分配的典型替代方法是使用swap:`vector<T>().swap(x); // clear x reallocating `
|
||||
```
|
||||
```cpp
|
||||
void clear() noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -1467,12 +1467,12 @@ v1 capacity = 5
|
||||
|
||||
该元素是通过调用allocator_traits::construct来转换args来创建的。插入一个类似的成员函数,将现有对象复制或移动到容器中。
|
||||
|
||||
```
|
||||
```cpp
|
||||
template <class... Args>
|
||||
iterator emplace (const_iterator position, Args&&... args);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -1504,7 +1504,7 @@ myvector contains: 10 200 100 20 30 300
|
||||
该元素是通过调用allocator_traits :: construct来转换args来创建的。
|
||||
|
||||
与push\_back相比,emplace\_back可以避免额外的复制和移动操作。
|
||||
```
|
||||
```cpp
|
||||
template <class... Args>
|
||||
void emplace_back (Args&&... args);
|
||||
```
|
||||
@ -1573,11 +1573,11 @@ Franklin Delano Roosevelt was re-elected president of the USA in 1936.
|
||||
```
|
||||
#### vector::get_allocator
|
||||
返回与vector关联的构造器对象的副本。
|
||||
```
|
||||
```cpp
|
||||
allocator_type get_allocator() const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
@ -1628,7 +1628,7 @@ deque上常见操作的复杂性(效率)如下:
|
||||
* 随机访问 - 常数O(1)
|
||||
* 在结尾或开头插入或移除元素 - 摊销不变O(1)
|
||||
* 插入或移除元素 - 线性O(n)
|
||||
```
|
||||
```cpp
|
||||
template < class T, class Alloc = allocator<T> > class deque;
|
||||
```
|
||||

|
||||
@ -1639,7 +1639,7 @@ template < class T, class Alloc = allocator<T> > class deque;
|
||||
构造一个deque容器对象,根据所使用的构造函数版本初始化它的内容:
|
||||
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
|
||||
@ -1674,12 +1674,12 @@ The contents of fifth are: 16 2 77 29
|
||||
在当前的最后一个元素之后 ,在deque容器的末尾添加一个新元素。val的内容被复制(或移动)到新的元素。
|
||||
|
||||
这有效地增加了一个容器的大小。
|
||||
```
|
||||
```cpp
|
||||
void push_back (const value_type& val);
|
||||
void push_back (value_type&& val);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
|
||||
@ -1704,12 +1704,12 @@ int main ()
|
||||
在deque容器的开始位置插入一个新的元素,位于当前的第一个元素之前。val的内容被复制(或移动)到插入的元素。
|
||||
|
||||
这有效地增加了一个容器的大小。
|
||||
```
|
||||
```cpp
|
||||
void push_front (const value_type& val);
|
||||
void push_front (value_type&& val);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
|
||||
@ -1736,11 +1736,11 @@ Output
|
||||
|
||||
这破坏了被删除的元素。
|
||||
|
||||
```
|
||||
```cpp
|
||||
void pop_back();
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
|
||||
@ -1771,11 +1771,11 @@ The elements of mydeque add up to 60
|
||||
删除deque容器中的第一个元素,有效地减小其大小。
|
||||
|
||||
这破坏了被删除的元素。
|
||||
```
|
||||
```cpp
|
||||
void pop_front();
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
|
||||
@ -1812,12 +1812,12 @@ The final size of mydeque is 0
|
||||
该元素是通过调用allocator_traits::construct来转换args来创建的。
|
||||
|
||||
存在一个类似的成员函数push_front,它可以将现有对象复制或移动到容器中。
|
||||
```
|
||||
```cpp
|
||||
template <class... Args>
|
||||
void emplace_front (Args&&... args);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
|
||||
@ -1848,12 +1848,12 @@ mydeque contains: 222 111 10 20 30
|
||||
该元素是通过调用allocator_traits::construct来转换args来创建的。
|
||||
|
||||
存在一个类似的成员函数push_back,它可以将现有对象复制或移动到容器中
|
||||
```
|
||||
```cpp
|
||||
template <class... Args>
|
||||
void emplace_back (Args&&... args);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
|
||||
@ -1894,7 +1894,7 @@ forward\_list(单向链表)被实现为单链表; 单链表可以将它们
|
||||
|
||||
#### forward\_list::forward\_list
|
||||
|
||||
```
|
||||
```cpp
|
||||
default (1)
|
||||
explicit forward_list (const allocator_type& alloc = allocator_type());
|
||||
fill (2)
|
||||
@ -1916,7 +1916,7 @@ forward_list (initializer_list<value_type> il,
|
||||
const allocator_type& alloc = allocator_type());
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <forward_list>
|
||||
|
||||
@ -1959,12 +1959,12 @@ sixth: 3 52 25 90
|
||||
|
||||
返回的迭代器不应被解除引用:它是为了用作成员函数的参数emplace\_after,insert\_after,erase\_after或splice\_after,指定序列,其中执行该动作的位置的开始位置。
|
||||
|
||||
```
|
||||
```cpp
|
||||
iterator before_begin() noexcept;
|
||||
const_iterator before_begin() const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <forward_list>
|
||||
|
||||
@ -1991,11 +1991,11 @@ mylist contains: 11 20 30 40 50
|
||||
一个常量性是指向常量内容的迭代器。这个迭代器可以增加和减少(除非它本身也是const),就像forward\_list::before\_begin返回的迭代器一样,但不能用来修改它指向的内容。
|
||||
|
||||
返回的价值不得解除引用。
|
||||
```
|
||||
```cpp
|
||||
const_iterator cbefore_begin() const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <forward_list>
|
||||
|
||||
@ -2035,11 +2035,13 @@ map 是关联容器,按照特定顺序存储由 key value (键值) 和 mapped
|
||||
|
||||
在映射中,键值通常用于对元素进行排序和唯一标识,而映射的值存储与此键关联的内容。该类型的键和映射的值可能不同,并且在部件类型被分组在一起VALUE_TYPE,这是一种对类型结合两种:
|
||||
|
||||
`typedef pair<const Key, T> value_type;`
|
||||
```cpp
|
||||
typedef pair<const Key, T> value_type;
|
||||
```
|
||||
|
||||
在内部,映射中的元素总是按照由其内部比较对象(比较类型)指示的特定的严格弱排序标准按键排序。映射容器通常比unordered_map容器慢,以通过它们的键来访问各个元素,但是它们允许基于它们的顺序对子集进行直接迭代。 在该映射值地图可以直接通过使用其相应的键来访问括号运算符((操作符[] )。 映射通常如实施
|
||||
|
||||
```
|
||||
```cpp
|
||||
template < class Key, // map::key_type
|
||||
class T, // map::mapped_type
|
||||
class Compare = less<Key>, // map::key_compare
|
||||
@ -2072,7 +2074,7 @@ x保持未指定但有效的状态。
|
||||
|
||||
用il中的每个元素的副本构造一个容器。
|
||||
|
||||
```
|
||||
```cpp
|
||||
empty (1)
|
||||
explicit map (const key_compare& comp = key_compare(),
|
||||
const allocator_type& alloc = allocator_type());
|
||||
@ -2094,7 +2096,7 @@ map (initializer_list<value_type> il,
|
||||
const allocator_type& alloc = allocator_type());
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
@ -2132,12 +2134,12 @@ int main ()
|
||||
由于map容器始终保持其元素的顺序,所以开始指向遵循容器排序标准的元素。
|
||||
|
||||
如果容器是空的,则返回的迭代器值不应被解除引用。
|
||||
```
|
||||
```cpp
|
||||
iterator begin() noexcept;
|
||||
const_iterator begin() const noexcept;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
@ -2166,11 +2168,11 @@ c => 300
|
||||
#### map::key_comp
|
||||
返回容器用于比较键的比较对象的副本。
|
||||
|
||||
```
|
||||
```cpp
|
||||
key_compare key_comp() const;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
@ -2208,11 +2210,11 @@ c => 300
|
||||
#### map::value_comp
|
||||
返回可用于比较两个元素的比较对象,以获取第一个元素的键是否在第二个元素之前。
|
||||
|
||||
```
|
||||
```cpp
|
||||
value_compare value_comp() const;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
@ -2249,12 +2251,12 @@ z => 3003
|
||||
如果容器的比较对象自反地返回假(即,不管元素作为参数传递的顺序),则两个key被认为是等同的。
|
||||
|
||||
另一个成员函数map::count可以用来检查一个特定的键是否存在。
|
||||
```
|
||||
```cpp
|
||||
iterator find (const key_type& k);
|
||||
const_iterator find (const key_type& k) const;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
@ -2294,11 +2296,11 @@ d => 200
|
||||
由于地图容器中的所有元素都是唯一的,因此该函数只能返回1(如果找到该元素)或返回零(否则)。
|
||||
|
||||
如果容器的比较对象自反地返回错误(即,不管按键作为参数传递的顺序),则两个键被认为是等同的。
|
||||
```
|
||||
```cpp
|
||||
size_type count (const key_type& k) const;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
@ -2344,12 +2346,12 @@ g is not an element of mymap.
|
||||
如果map类用默认的比较类型(less)实例化,则函数返回一个迭代器到第一个元素,其键不小于k。
|
||||
|
||||
一个类似的成员函数upper\_bound具有相同的行为lower\_bound,除非映射包含一个key值等于k的元素:在这种情况下,lower\_bound返回指向该元素的迭代器,而upper\_bound返回指向下一个元素的迭代器。
|
||||
```
|
||||
```cpp
|
||||
iterator lower_bound (const key_type& k);
|
||||
const_iterator lower_bound (const key_type& k) const;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
@ -2393,12 +2395,12 @@ e => 100
|
||||
|
||||
类似的成员函数lower\_bound具有与upper\_bound相同的行为,除了map包含一个元素,其键值等于k:在这种情况下,lower\_bound返回指向该元素的迭代器,而upper\_bound返回指向下一个元素的迭代器。
|
||||
|
||||
```
|
||||
```cpp
|
||||
iterator upper_bound (const key_type& k);
|
||||
const_iterator upper_bound (const key_type& k) const;
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
@ -2440,12 +2442,12 @@ e => 100
|
||||
如果没有找到匹配,则返回的范围具有零的长度,与两个迭代器指向具有考虑去后一个密钥对所述第一元件ķ根据容器的内部比较对象(key\_comp)。如果容器的比较对象返回false,则两个键被认为是等价的。
|
||||
|
||||
|
||||
```
|
||||
```cpp
|
||||
pair<const_iterator,const_iterator> equal_range (const key_type& k) const;
|
||||
pair<iterator,iterator> equal_range (const key_type& k);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
@ -2510,12 +2512,12 @@ unordered\_map、unodered\_multimap结构:
|
||||
|
||||
元组是一个能够容纳元素集合的对象。每个元素可以是不同的类型。
|
||||
|
||||
```
|
||||
```cpp
|
||||
template <class... Types> class tuple;
|
||||
```
|
||||
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream> // std::cout
|
||||
#include <tuple> // std::tuple, std::get, std::tie, std::ignore
|
||||
|
||||
@ -2578,7 +2580,7 @@ foo contains: 100 y
|
||||
|
||||
和上面的版本一样,除了每个元素都是使用allocator alloc构造的。
|
||||
|
||||
```
|
||||
```cpp
|
||||
default (1)
|
||||
constexpr tuple();
|
||||
copy / move (2)
|
||||
@ -2619,7 +2621,7 @@ template<class Alloc, class U1, class U2>
|
||||
tuple (allocator_arg_t aa, const Alloc& alloc, pair<U1,U2>&& pr);
|
||||
```
|
||||
Example
|
||||
```
|
||||
```cpp
|
||||
#include <iostream> // std::cout
|
||||
#include <utility> // std::make_pair
|
||||
#include <tuple> // std::tuple, std::make_tuple, std::get
|
||||
@ -2656,7 +2658,7 @@ pair的实现是一个结构体,主要的两个成员变量是first second 因
|
||||
* 可以将两个类型数据组合成一个如map<key, value>
|
||||
* 当某个函数需要两个返回值时
|
||||
|
||||
```
|
||||
```cpp
|
||||
template <class T1, class T2> struct pair;
|
||||
```
|
||||
#### pair::pair
|
||||
@ -2681,7 +2683,7 @@ template <class T1, class T2> struct pair;
|
||||
|
||||
构造成员 first 和 second 到位,传递元素first\_args 作为参数的构造函数 first,和元素 second\_args 到的构造函数 second 。
|
||||
|
||||
```
|
||||
```cpp
|
||||
default (1)
|
||||
constexpr pair();
|
||||
copy / move (2)
|
||||
@ -2700,7 +2702,7 @@ template <class... Args1, class... Args2>
|
||||
|
||||
Example
|
||||
|
||||
```
|
||||
```cpp
|
||||
#include <utility> // std::pair, std::make_pair
|
||||
#include <string> // std::string
|
||||
#include <iostream> // std::cout
|
||||
|
Loading…
x
Reference in New Issue
Block a user