C++ 学习 6 STL容器使用

  • array
  • vector
  • queue
  • list
  • set/map
  • unsorted set/map

    array

    std::array
    template < class T, size_t N > class array;
    头文件 #include < array >
    严格线性排序,连续存储,允许随机存取,可以通过指针加偏移地址访问,使用默认构造和析构函数静态分配空间,其大小是constant没有内存和时间的开销。
    member type definition notes
    value_type The first template parameter (T)
    reference value_type&
    const_reference const value_type&
    pointer value_type*
    const_pointer const value_type*
    iterator a random access iterator to value_type convertible to const_iterator
    const_iterator a random access iterator to const value_type
    reverse_iterator reverse_iterator
    const_reverse_iterator reverse_iterator
    size_type size_t unsigned integral type
    difference_type ptrdiff_t signed integral type

    成员函数
    迭代器
    begin
    返回的迭代器开始(公有成员类型)
    end
    返回的迭代器结束(公有成员类型)
    rbegin
    返回逆向迭代器的反向的开始(公共成有函数)
    rend
    返回逆向迭代器的反向结束(公有成员函数)
    cbegin
    返回常量迭代器的开始(公有成员函数)
    cend
    返回常量迭代器的结束(公有成员函数)
    crbegin
    返回逆向常量迭代器的反向开始(公有成员函数)
    crend
    返回逆向常量迭代器的反向结束(公有成员函数)
    容量
    size
    返回数组容器的大小(公有成员函数)
    max_size
    返回数组容器的最大值(公有成员函数)
    empty
    测试数组容器是否为空(公有成员函数)

元素存取
operator[]
访问元素(公有成员函数)
at
访问元素(公有成员函数)
front
访问第一个元素(公有成员函数)
back
访问最后一个元素(公有成员函数)
data
获得指向容器数据的指针(公有成员函数)

元素修改
fill
填充数组值(公有成员函数)
swap
交换内容(公有成员函数)
全局函数
get (array)
获取元素(元组接口)(函数模板)
operators (array)
数组容器的全局关系运算函数(函数模板)
参考:http://www.cplusplus.com/reference/array/array/?kw=array
http://blog.csdn.net/devourheavens/article/details/7434784

vector

原型std::vector
template < class T, class Alloc = allocator< T> > class vector; // generic template
头文件#include < vector >

Vector 是一种顺序容器, 是一种可变长度的array。就像array一样vector利用连续的存储空间,这就意味着可以像array一样高效的用指针加偏移量来存取其中的元素。与array不同的是它可以动态的改变大小,他的存储空间由容器自动管理。
实现上,vector利用动态分配的array来存储元素,这些元素可能被重新分配存储空间,由于需要改变其大小,具体实现是当存储空间不够时,利用allocator重新分配一个2倍大小空间,并将原array中的元素复制过去 。由于vector需要分配多余的空间应对可能的空间增长需求,所以它实际的capacity() 一般都大于size(),也就是说并不是每次insert()元素都会重新分配空间。因此相比array,vector会消耗更多的内存来交换高效的动态增长空间的能力。
对比其他的动态顺序容器(deque,list, forward_list)vector 可以非常高效的access任一元素,添加删除最后一个元素。但是对于insert(), remove() 任一位置元素需要同时拷贝其后面的所有元素。
模板参数:
T 元素类型, 只有在T能保证在moving 的时候不产生异常,当reallocation时编译器的具体实现可以优化move 元素而不是复制。
Alloc allocator 对象的类型用来声明strorage allocation model. 默认使用allocator 类模板。
成员类型:
value_type The first template parameter (T)
allocator_type The second template parameter (Alloc) defaults to: allocator< value_type >
reference value_type&
const_reference const value_type&
pointer allocator_traits< allocator_type >::pointer for the default allocator: value_type*
const_pointer allocator_traits< allocator_type >::const_pointer for the default allocator: const value_type*
iterator a random access iterator to value_type convertible to const_iterator
const_iterator a random access iterator to const value_type
reverse_iterator reverse_iterator< iterator >
const_reverse_iterator reverse_iterator< const_iterator >
difference_type a signed integral type, identical to:
iterator_traits< iterator >::difference_type usually the same as ptrdiff_t
size_type an unsigned integral type that can represent any non-negative value of difference_type usually the same as size_t
成员函数:
(constructor):1 空, empty容器,没有buffer
2. fill 构造 (num, value) num 个value。
3. range 构造 range [first,last) (c.begin(),c.end())
4. 复制构造, 以原顺序复制所有元素。
5. move 构造 如果alloc 重新指定则move否则除了所有权转移不发生.
6. initializer list 构造 按顺序拷贝
(destructor):根据指定的allocator ,调用lls allocator_traits::destroy 释放所有的存储capacity
operator =
copy (1)
vector& operator= (const vector& x);
move (2)
vector& operator= (vector&& x);
initializer list (3)
vector& operator= (initializer_list< value_type > il);
迭代器:
1. begin : 返回beginning :如果是空容器则返回的迭代器不可被解引用。
2. end:返回最后一个元素的下一个位置,如果为空则返回vector::begin
3. rbegin: 返回一个反向迭代器指向最后一个元素:不像vector::back只返回一个最后元素的引用,rbegin()返回一个反向的随机access 迭代器。
4. rend: 返回一个反向迭代器指向理论上的第一个元素之前的元素。vector::rbein,rend()包含所有元素不过是反向的。
cbegin: 返回一个const iterator指向第一个元素。
cend: 返回一个const iterator指向 end
crbegin(),crend…

Capacity:
size:返回size
max_size:返回一个container最大能达到的size 取决于系统和库。不一定能达到。
resize:void resize (size_type n);
void resize (size_type n, const value_type& val);
capacity:返回 allocated strorage capacity
empty: return bool
reserve:void reserve (size_type n);
Request a change in capacity
shrink_to_fit:void shrink_to_fit();Requests the container to reduce its capacity to fit its size.This may cause a reallocation, but has no effect on the vector size and cannot alter its elements.
元素 access:
operator[]: reference operator[] (size_type n);
const_reference operator[] (size_type n) const;Returns a reference to the element at position n in the vector container.没有边界检查 at有。
at: reference at (size_type n);
const_reference at (size_type n) const;使用: c.at(i)
front,back。返回一个引用
data:
value_type* data() noexcept;
const value_type* data() const noexcept;返回容器地址

Modifiers:
assign:
range (1)
template < class InputIterator >
void assign (InputIterator first, InputIterator last);
fill (2)
void assign (size_type n, const value_type& val);
initializer list (3)
void assign (initializer_list < value_type > il);
push_back,pop_back(): add or delete element at end.
insert():insert elemnt
erase: erase element:
iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);
swap: swap content
void swap (vector& x);
Exchanges the content of the container by the content of x, which is another vector object of the same type. Sizes may differ.
clear:void clear() noexcept;Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
emplace:template < class… Args >
iterator emplace (const_iterator position, Args&&… args);The container is extended by inserting a new element at position. This new element is constructed in place using args as the arguments for its construction.
emplace_back :Construct and insert element at the end

Allocator:
get_allocator:allocator_type get_allocator() const noexcept;Returns a copy of the allocator object associated with the vector.
非成员函数重载:
relaition operators:
template < class T, class Alloc >
bool operator== (const vector< T,Alloc >& lhs, const vector< T,Alloc >& rhs);
(2)
template < class T, class Alloc >
bool operator!= (const vector< T,Alloc >& lhs, const vector< T,Alloc >& rhs);
(3)
template < class T, class Alloc >
bool operator < (const vector< T,Alloc >& lhs, const vector< T,Alloc >& rhs);
(4)
template < class T, class Alloc >
bool operator <= (const vector< T,Alloc >& lhs, const vector< T,Alloc >& rhs);
(5)
template < class T, class Alloc >
bool operator > (const vector< T,Alloc >& lhs, const vector< T,Alloc >& rhs);
(6)
template < class T, class Alloc >
bool operator>= (const vector< T,Alloc >& lhs, const vector< T,Alloc >& rhs);

swap:
template < class T, class Alloc >
void swap (vector< T,Alloc >& x, vector< T,Alloc >& y);

模板特化:
template < class T, class Alloc = allocator< T > > class vector; // generic template
template < class Alloc > class vector< bool,Alloc >; // bool specialization
This is a specialized version of vector, which is used for elements of type bool and optimizes for space.
参考:http://www.cplusplus.com/reference/vector/vector/?kw=vector

list

std::list
template < class T, class Alloc = allocator< T> > class list;
头文件#include < list >
list容器就是一个双向链表,可以高效地进行插入删除元素。

使用list容器之前必须加上头文件:#include;

list属于std命名域的内容,因此需要通过命名限定:using std::list;也可以直接使用全局的命名空间方式:using namespace std;

构造函数

  list< int> c0; //空链表

  list< int> c1(3); //建一个含三个默认值是0的元素的链表

  list< int> c2(5,2); //建一个含五个元素的链表,值都是2

  list< int> c4(c2); //建一个c2的copy链表

  list< int> c5(c1.begin(),c1.end()); c5含c1一个区域的元素[_First, _Last)。

成员函数

c.begin() 返回指向链表第一个元素的迭代器。

c.end() 返回指向链表最后一个元素之后的迭代器。

1 list< int> a1{1,2,3,4,5};
2 list< int>::iterator it;
3 for(it = a1.begin();it!=a1.end();it++){
4 cout << *it << “\t”;
5 }
6 cout << endl;
c.rbegin() 返回逆向链表的第一个元素,即c链表的最后一个数据。

c.rend() 返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。

1 list< int> a1{1,2,3,4,5};
2 list< int>::reverse_iterator it;
3 for(it = a1.rbegin();it!=a1.rend();it++){
4 cout << *it << “\t”;
5 }
6 cout << endl;
operator= 重载赋值运算符。

1 list< int> a1 {1,2,3,4,5},a2;
2 a2 = a1;
3 list< int>::iterator it;
4 for(it = a2.begin();it!=a2.end();it++){
5 cout << *it << endl;
6 }
c.assign(n,num) 将n个num拷贝赋值给链表c。

c.assign(beg,end) 将[beg,end)区间的元素拷贝赋值给链表c。

1 int a[5] = {1,2,3,4,5};
2 list< int> a1;
3 list< int>::iterator it;
4 a1.assign(2,10);
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
9 a1.assign(a,a+5);
10 for(it = a1.begin();it!=a1.end();it++){
11 cout << *it << ” “;
12 }
13 cout << endl;
c.front() 返回链表c的第一个元素。

c.back() 返回链表c的最后一个元素。

1 list a1{1,2,3,4,5};
2 if(!a1.empty()){
3 cout << “the first number is:” << a1.front() << endl;
4 cout << “the last number is:” << a1.back() << endl;
5 }
c.empty() 判断链表是否为空。

1 list< int> a1{1,2,3,4,5};
2 if(!a1.empty())
3 cout << “a1 is not empty” << endl;
4 else
5 cout << ” a1 is empty” << endl;
c.size() 返回链表c中实际元素的个数。

1 list< int> a1{1,2,3,4,5};
2 cout << a1.size() << endl;
c.max_size() 返回链表c可能容纳的最大元素数量。

1 list< int> a1{1,2,3,4,5};
2 cout << a1.max_size() << endl;
c.clear() 清除链表c中的所有元素。

1 list< int> a1{1,2,3,4,5};
2 list< int>::iterator it;
3 cout << “clear before:”;
4 for(it = a1.begin();it!=a1.end();it++){
5 cout << *it << “\t”;
6 }
7 cout << endl;
8 a1.clear();
9 cout << “clear after:”;
10 for(it = a1.begin();it!=a1.end();it++){
11 cout << *it << “\t”;
12 }
13 cout << endl;
c.insert(pos,num) 在pos位置插入元素num。

c.insert(pos,n,num) 在pos位置插入n个元素num。

c.insert(pos,beg,end) 在pos位置插入区间为[beg,end)的元素。

1 list< int> a1{1,2,3,4,5};
2 list< int>::iterator it;
3 cout << “insert before:”;
4 for(it = a1.begin();it!=a1.end();it++){
5 cout << *it << ” “;
6 }
7 cout << endl;
8
9 a1.insert(a1.begin(),0);
10 cout << “insert(pos,num) after:”;
11 for(it = a1.begin();it!=a1.end();it++){
12 cout << *it << ” “;
13 }
14 cout << endl;
15
16 a1.insert(a1.begin(),2,88);
17 cout << “insert(pos,n,num) after:”;
18 for(it = a1.begin();it!=a1.end();it++){
19 cout << *it << ” “;
20 }
21 cout << endl;
22
23 int arr[5] = {11,22,33,44,55};
24 a1.insert(a1.begin(),arr,arr+3);
25 cout << “insert(pos,beg,end) after:”;
26 for(it = a1.begin();it!=a1.end();it++){
27 cout << *it << ” “;
28 }
29 cout << endl;
c.erase(pos)    删除pos位置的元素。

1 list< int> a1{1,2,3,4,5};
2 list< int>::iterator it;
3 cout << “erase before:”;
4 for(it = a1.begin();it!=a1.end();it++){
5 cout << *it << ” “;
6 }
7 cout << endl;
8 a1.erase(a1.begin());
9 cout << “erase after:”;
10 for(it = a1.begin();it!=a1.end();it++){
11 cout << *it << ” “;
12 }
13 cout << endl;
c.push_back(num) 在末尾增加一个元素。

c.pop_back() 删除末尾的元素。

c.push_front(num) 在开始位置增加一个元素。

c.pop_front() 删除第一个元素。

1 list< int> a1{1,2,3,4,5};
2 a1.push_back(10);
3 list< int>::iterator it;
4 cout << “push_back:”;
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
9
10 a1.pop_back();
11 cout << “pop_back:”;
12 for(it = a1.begin();it!=a1.end();it++){
13 cout << *it << ” “;
14 }
15 cout << endl;
16
17 a1.push_front(20);
18 cout << “push_front:”;
19 for(it = a1.begin();it!=a1.end();it++){
20 cout << *it << ” “;
21 }
22 cout << endl;
23
24 a1.pop_front();
25 cout << “pop_front:”;
26 for(it = a1.begin();it!=a1.end();it++){
27 cout << *it << ” “;
28 }
29 cout << endl;
resize(n) 从新定义链表的长度,超出原始长度部分用0代替,小于原始部分删除。

resize(n,num) 从新定义链表的长度,超出原始长度部分用num代替。

1 list< int> a1{1,2,3,4,5};
2 a1.resize(8);
3 list< int>::iterator it;
4 cout << “resize(n):”;
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
9
10 a1.resize(10, 10);
11 cout << “resize(n,num):”;
12 for(it = a1.begin();it!=a1.end();it++){
13 cout << *it << ” “;
14 }
15 cout << endl;
c1.swap(c2); 将c1和c2交换。

swap(c1,c2); 同上。

1 list< int> a1{1,2,3,4,5},a2,a3;
2 a2.swap(a1);
3 list::iterator it;
4 cout << “a2.swap(a1):”;
5 for(it = a2.begin();it!=a2.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
9
10 swap(a3,a2);
11 cout << “swap(a3,a2):”;
12 for(it = a3.begin();it!=a3.end();it++){
13 cout << *it << ” “;
14 }
15 return 0;
c1.merge(c2) 合并2个有序的链表并使之有序,从新放到c1里,释放c2。

c1.merge(c2,comp) 合并2个有序的链表并使之按照自定义规则排序之后从新放到c1中,释放c2。

1 list< int > a1{1,2,3},a2{4,5,6};
2 a1.merge(a2);
3 list< int>::iterator it;
4 cout << “a1.merge(a2):”;
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
9
10 a2.merge(a1,[](int n1,int n2){return n1>n2;});
11 cout << “a2.merge(a1,comp):”;
12 for(it = a2.begin();it!=a2.end();it++){
13 cout << *it << ” “;
14 }
15 cout << endl;

c1.splice(c1.beg,c2) 将c2连接在c1的beg位置,释放c2

1 list a1{1,2,3},a2{4,5,6};
2 a1.splice(a1.begin(), a2);
3 list::iterator it;
4 cout << “a1.merge(a2):”;
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;

c1.splice(c1.beg,c2,c2.beg) 将c2的beg位置的元素连接到c1的beg位置,并且在c2中施放掉beg位置的元素

1 list< int > a1{1,2,3},a2{4,5,6};
2 a1.splice(a1.begin(), a2,++a2.begin());
3 list< int >::iterator it;
4 cout << “a1.merge(a2):”;
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
9 return 0;

c1.splice(c1.beg,c2,c2.beg,c2.end) 将c2的[beg,end)位置的元素连接到c1的beg位置并且释放c2的[beg,end)位置的元素

1 list< int > a1{1,2,3},a2{4,5,6};
2 a1.splice(a1.begin(),a2,a2.begin(),a2.end());
3 list< int >::iterator it;
4 cout << “a1.merge(a2):”;
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
9 return 0;

remove(num) 删除链表中匹配num的元素。

1 list a1{1,2,3,4,5};
2 a1.remove(3);
3 list::iterator it;
4 cout << “remove():”;
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
复制代码
remove_if(comp) 删除条件满足的元素,参数为自定义的回调函数。

1 list< int > a1{1,2,3,4,5};
2 a1.remove_if([](int n){return n<3;});
3 list< int >::iterator it;
4 cout << “remove_if():”;
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;

reverse() 反转链表

1 list< int > a1{1,2,3,4,5};
2 a1.reverse();
3 list::iterator it;
4 cout << “reverse:”;
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
复制代码
unique() 删除相邻的元素

1 list< int > a1{1,1,3,3,5};
2 a1.unique();
3 list< int >::iterator it;
4 cout << “unique:”;
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
9 return 0;

c.sort() 将链表排序,默认升序

c.sort(comp) 自定义回调函数实现自定义排序

1 list< int > a1{1,3,2,5,4};
2 a1.sort();
3 list::iterator it;
4 cout << “sort():”;
5 for(it = a1.begin();it!=a1.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
9
10 a1.sort([](int n1,int n2){return n1>n2;});
11 cout << “sort(function point):”;
12 for(it = a1.begin();it!=a1.end();it++){
13 cout << *it << ” “;
14 }
15 cout << endl;
复制代码
重载运算符

operator==

operator!=

operator<

operator<=

operator>

operator>=
参考:
http://www.cnblogs.com/scandy-yuan/archive/2013/01/08/2851324.html

forward_list

std::forward_list
template < class T, class Alloc = allocator< T> > class forward_list;
头文件 #include < forward_list>
前向链表是序列容器,使固定时间插入和擦除操作序列内的任何地方。

前向链表的实现方式和单链表相同;单链表可以存储所包含的每个元素在不同的和无关的存储位置。

在序列中顺序保存每个元素指向下一个元素的关联。

forward_list容器与list容器的主要设计区别是list保持内部唯一的一个链接到下一个元素,而后者则保持每个元素的两个链接:一个指向下一个元素和一个前一个。允许高效在两个方向迭代,但每个元素的消耗额外的存储空间,并轻微较高的时间开销插入和删除元素的迭代。forward_list对象,从而比list对象更有效率,虽然他们只能向前遍历。

与其他的基本标准序列容器(array,vector和deque)相比,forward_list一般在容器内的任何位置中的元素的插入、提取和移动操作效率更高,因此在算法中较密集的使用这些操作,例如排序算法。

相比其他序列容器,forward_lists的主要缺点是缺乏直接访问他们的位置的元素,例如,要进入第六个元素在forward_list的一个遍历从一开始就到那个位置,这需要线性时间之间的距离。他们也消耗了一些额外的内存保持连接信息相关联的每个元素(这可能是一个小型的元素的大链表的重要因素)。

考虑到forward_list类模板设计的性能:根据设计,它是作为一个简单的手写C风格的单链表一样高效,实际上是仅有的一个标准容器故意缺乏其大小的成员函数是出于效率的考虑。由于其作为一个链表的性质,有一个大小成员在固定的时间,需要保持一个内部的计数器保存其大小(和链表一样)。这会消耗一些额外的存储和使插入和删除操作,效率较低。为了获得一个forward_list对象的大小,可以用其开始和结束,这是一个线性时间的操作距离算法。

容器属性
序列

在一个严格的线性序列中序列容器的元素是有序的。个别元素的访问是通过他们在这个序列中的位置。

链表
每个元素保持如何找到下一个元素的信息,允许常量时间在特定元素(甚至整个范围)后进行插入和擦除操作,但没有直接随机存取。

分配器的获取
容器使用一个分配器对象动态地处理其存储需求。

模板参数
T
元素的类型。
作为成员类型forward_list:: value_type的别名。

Alloc
用来定义存储分配模型的分配器对象的类型。默认情况下使用了分配器类模板,它定义了最简单的内存分配模式和单独存在。

别名成员类型forward_list的::allocator_type的。

在参考的forward_list成员函数,这些相同的名称被假定为模板参数。

成员类型
成员类型 定义 备注
value_type The first template parameter (T)
allocator_type The second template parameter (Alloc) defaults to: allocator
reference value_type&
const_reference const value_type&
pointer allocator_traits::pointer for the default allocator: value_type*
const_pointer allocator_traits::const_pointer for the default allocator: const value_type*
iterator a forward iterator to value_type convertible to const_iterator
const_iterator a forward iterator to const value_type
size_type an unsigned integral type usually the same as size_t
difference_type a signed integral type usually the same as ptrdiff_t
成员函数
构造函数
构造函数 forward_list (公有成员函数 )
析构函数
析构函数 forward_list (公有成员函数 )
=操作运算符
分配内容 (公有成员函数)
迭代器
before_begin
返回指向开始之前的迭代器指针 (公有成员函数 )
begin
返回指向开始的迭代器指针 (公有成员类型 )
end
返回指向结束的迭代器指针 (公有成员函数 )
cbefore_begin
返回指向开始之前的常迭代器指针 (公有成员函数 )
cbegin
返回指向指向开始的常迭代器指针 (公有成员函数 )
cend
返回指向结束的常迭代器指针 (公有成员函数 )
容量
empty
判断是否为空 (公有成员函数 )
max_size
返回容量的最大值 (公有成员函数 )
元素的获取
front
获得第一个元素值 (公有成员函数 )
修饰符
assign
分配内容 (公有成员函数 )
emplace_front
构造并插入元素到首位置 (公有成员函数 )
push_front
插入元素到首位置 (公有成员函数 )
pop_front
删除首位置的元素 (公有成员函数 )
emplace_after
构造并插入元素 (公有成员函数 )
insert_after
插入元素 (公有成员函数 )
erase_after
擦除元素 (公有成员函数 )
swap
交换内容 (公有成员函数 )
resize
改变容量大小 (公有成员函数 )
clear
清除内容 (公有成员函数 )
操作
splice_after
移动元素从另一个前向链表 (公有成员函数 )
remove
删除特定值的元素 (公有成员函数 )
remove_if
删除符合条件的元素 (公有成员模板函数 )
unique
删除重复值 (成员函数 )
merge
合并排序链表 (公有成员函数 )
sort
容器中的元素排序 (公有成员函数 )
reverse
反转元素的顺序 (公有成员函数 )
观察者
get_allocator
获取分配器 (公有成员函数 )
全局函数
operators (forward_list)
链表的全局关系运算函数 (函数模板 )
swap (forward_list)
交换两个前向链表的内容(函数模板 )

同理boost::slist

参考:http://blog.csdn.net/devourheavens/article/details/7497172
http://www.cplusplus.com/reference/forward_list/forward_list/?kw=forward_list

deque

std::deque
template < class T, class Alloc = allocator< T> > class deque;
头文件 #include < deque>
这里写图片描述
deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素,deque在接口上和vector非常相似,deque的实现比较复杂,内部会维护一个map(注意!不是STL中的map容器)即一小块连续的空间,该空间中每个元素都是指针,指向另一段(较大的)区域,这个区域称为缓冲区,缓冲区用来保存deque中的数据。因此deque在随机访问和遍历数据会比vector慢。
构造函数

  deque c 创建一个空的deque

  deque< Elem> c1(c2) 复制一个deque。

  deque< Elem> c(n) 创建一个deque,含有n个数据,数据均已缺省构造产生。

  deque c(n, elem) 创建一个含有n个elem拷贝的deque。

  deque< Elem> c(beg,end) 创建一个以[beg;end)区间的deque。

  ~deque< Elem>() 销毁所有数据,释放内存。

成员函数

c.begin()返回指向第一个元素的迭代器

c.end()返回指向最后一个元素下一个位置的迭代器

1 deque< int> d {1,2,3,4,5};
2 deque< int>::iterator it;
3 for(it=d.begin();it!=d.end();it++){
4 cout << *it << ” “;
5 }
6 cout << endl;
c.rbegin()返回指向反向队列的第一个元素的迭代器(即原队列的最后一个元素)

c.rend()返回指向反向队列的最后一个元素的下一个位置(即原队列的第一个元素的前一个位置)

1 deque< int> d {1,2,3,4,5};
2 deque< int>::reverse_iterator it;
3 for(it=d.rbegin();it!=d.rend();it++){
4 cout << *it << ” “;
5 }
6 cout << endl;
operator=赋值运算符重载

1 deque< int> d1 {1,2,3,4,5},d2;
2 d2 = d1;
3 deque< int>::iterator it;
4 for(it=d2.begin();it!=d2.end();it++){
5 cout << *it << ” “;
6 }
7 cout << endl;
c.assign(n,num)将n个num拷贝复制到容器c

c.assign(beg,end)将[beg,end)区间的数据拷贝复制到容器c

1 deque< int> d1 {1,2,3,4,5},d2;
2 d2.assign(2, 8);
3 deque< int>::iterator it;
4 cout << “d2.assign(n,num):”;
5 for(it=d2.begin();it!=d2.end();it++){
6 cout << *it << ” “;
7 }
8 d2.assign(d1.begin(), d1.begin()+3);
9 cout << “d2.assign(beg,end):”;
10 for(it=d2.begin();it!=d2.end();it++){
11 cout << *it << ” “;
12 }
13 cout << endl;

c.at(pos)返回索引为pos的位置的元素,会执行边界检查,如果越界抛出out_of_range异常

1 deque< int> d {1,2,3,4,5};
2 cout << “d.at(pos):” << d.at(2);
3 return 0;
c.operator[]下标运算符重载

1 deque< int> d {1,2,3,4,5};
2 cout << “d[2]:” << d[2];
3 return 0;
c.empty()判断c容器是否为空

1 deque d {1,2,3,4,5};
2 if(!d.empty()){
3 cout << “d is not empty!” << endl;
4 }else{
5 cout << “d is empty!” << endl;
6 }
7 return 0;
c.front()返回c容器的第一个元素

c.back()返回c容器的最后一个元素

1 deque< int> d {1,2,3,4,5};
2 if(!d.empty()){
3 cout << “d.front():” << d.front() << endl;
4 cout << “d.back(): ” << d.back() << endl;
5 }
c.size()返回c容器中实际拥有的元素个数

1 deque< int> d {1,2,3,4,5};
2 cout << “d.size():” << d.size() << endl;
3 return 0;
c.max_size()返回c容器可能存放元素的最大数量

1 deque< int> d {1,2,3,4,5};
2 cout << “d.max_size():” << d.max_size() << endl;
3 return 0;
c.clear()清除c容器中拥有的所有元素

1 deque< int> d {1,2,3,4,5};
2 deque< int>::iterator it;
3 cout << “clear before:” ;
4 for(it=d.begin();it!=d.end();it++){
5 cout << *it << ” “;
6 }
7 cout << endl;
8 d.clear();
9 cout << “clear after:” ;
10 for(it=d.begin();it!=d.end();it++){
11 cout << *it << ” “;
12 }
13 cout << endl;
c.insert(pos,num)在pos位置插入元素num

c.insert(pos,n,num)在pos位置插入n个元素num

c.insert(pos,beg,end)在pos位置插入区间为[beg,end)的元素

1 deque< int> d {1,2,3,4,5};
2 deque< int>::iterator it;
3 cout << “insert before:” ;
4 for(it=d.begin();it!=d.end();it++){
5 cout << *it << ” “;
6 }
7 cout << endl;
8 d.insert(d.end(),22);
9 d.insert(d.end(), 3,88);
10 int a[5] = {1,2,3,4,5};
11 d.insert(d.begin(),a,a+3);
12 cout << “insert after:” ;
13 for(it=d.begin();it!=d.end();it++){
14 cout << *it << ” “;
15 }
16 cout << endl;
c.erase(pos)删除pos位置的元素c.erase(beg,end)删除区间为[beg,end)的元素

c.erase(beg,end)删除区间为[beg,end)之间的元素

1 deque< int> d {1,2,3,4,5};
2 d.erase(d.begin());
3 deque< int>::iterator it;
4 cout << “erase(pos) after:” ;
5 for(it=d.begin();it!=d.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
9 d.erase(d.begin(), d.begin()+3);
10 cout << “erase(beg,end) after:” ;
11 for(it=d.begin();it!=d.end();it++){
12 cout << *it << ” “;
13 }
14 cout << endl;

c.push_back(num)在末尾位置插入元素

c.pop_back()删除末尾位置的元素

c.push_front(num)在开头位置插入元素

c.pop_front()删除开头位置的元素

1 deque< int> d {1,2,3,4,5};
2 d.push_back(10);
3 deque< int>::iterator it;
4 cout << “push_back(num):” ;
5 for(it=d.begin();it!=d.end();it++){
6 cout << *it << ” “;
7 }
8 cout << endl;
9
10 d.pop_back();
11 cout << “pop_back(num):” ;
12 for(it=d.begin();it!=d.end();it++){
13 cout << *it << ” “;
14 }
15 cout << endl;
16
17 d.push_front(10);
18 cout << “push_front(num):” ;
19 for(it=d.begin();it!=d.end();it++){
20 cout << *it << ” “;
21 }
22 cout << endl;
23
24 d.pop_front();
25 cout << “pop_front(num):” ;
26 for(it=d.begin();it!=d.end();it++){
27 cout << *it << ” “;
28 }
29 cout << endl;
30 return 0;

c.resize(num)从新定义容器的大小

1 deque< int> d {1,2,3,4,5};
2 cout << “d.size():” << d.size() << endl;
3 d.resize(d.size()+5);
4 cout << “d.resize() after:” << d.size() <

stack

std::stack
template < class T, class Container = deque< T> > class stack;
头文件 #include < stack>
栈是一种容器适配器,特别为后入先出而设计的一种(LIFO ),那种数据被插入,然后再容器末端取出

栈实现了容器适配器,这是用了一个封装了的类作为他的特定容器,提供了一组成员函数去访问他的元素,元素从特定的容器,也就是堆栈的头取出元素。
参数示意:

T: 元素类型
Container: 被用于存储和访问元素的的类型
成员函数

stack::stack

explicit stack ( const Container& ctnr = Container() );

用于构造一个栈适配器对象。

ctor
initialize (1)
explicit stack (const container_type& ctnr);
move-initialize (2)
explicit stack (container_type&& ctnr = container_type());
allocator (3)
template < class Alloc> explicit stack (const Alloc& alloc);
init + allocator (4)
template < class Alloc> stack (const container_type& ctnr, const Alloc& alloc);
move-init + allocator (5)
template < class Alloc> stack (container_type&& ctnr, const Alloc& alloc);
copy + allocator (6)
template < class Alloc> stack (const stack& x, const Alloc& alloc);
move + allocator (7)
template < class Alloc> stack (stack&& x, const Alloc& alloc);

Test whether container is empty (public member function )
size
Return size (public member function )
top
Access next element (public member function )
push
Insert element (public member function )
emplace
Construct and insert element (public member function )
pop
Remove top element (public member function )
swap
Swap contents (public member function )

Non-member function overloads
relational operators
Relational operators for stack (function )
swap (stack)
Exchange contents of stacks (public member function )

Non-member class specializations
uses_allocator< stack>
Uses allocator for stack (class template ):
template < class T, class Container, class Alloc>
struct uses_allocator< stack< T,Container>,Alloc>;

template < class T, class Container, class Alloc>
struct uses_allocator< stack< T,Container>,Alloc>

queue

std::queue
template

template<class _Ty, class _Container = deque<_Ty> >
class queue
{   // FIFO queue implemented with a container
public:
    typedef _Container container_type;
    typedef typename _Container::value_type value_type;
    typedef typename _Container::size_type size_type;
    typedef typename _Container::reference reference;
    typedef typename _Container::const_reference const_reference;

    queue() : c()
    {   // construct with empty container
    }

    explicit queue(const _Container& _Cont) : c(_Cont)
    {   // construct by copying specified container
    }

    bool empty() const
    {   // test if queue is empty
        return (c.empty());
    }

    size_type size() const
    {   // return length of queue
        return (c.size());
    }

    reference front()
    {   // return first element of mutable queue
        return (c.front());
    }

    const_reference front() const
    {   // return first element of nonmutable queue
        return (c.front());
    }

    reference back()
    {   // return last element of mutable queue
        return (c.back());
    }

    const_reference back() const
    {   // return last element of nonmutable queue
        return (c.back());
    }

    void push(const value_type& _Val)
    {   // insert element at beginning
        c.push_back(_Val);
    }

    void pop()
    {   // erase element at end
        c.pop_front();
    }

    const _Container& _Get_container() const
    {   // get reference to container
        return (c);
    }

protected:
    _Container c;   // the underlying container
};

multiset

std::multiset
template < class T, // multiset::key_type/value_type
class Compare = less < T>, // multiset::key_compare/value_compare
class Alloc = allocator< T> > // multiset::allocator_type > class multiset;
头文件 #include < set>
使用set或multiset之前,必须加入头文件< set>
Set、multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素。
sets和multiset内部以平衡二叉树实现(红黑数)
1. 常用函数
1) 构造函数和析构函数
set c:创建空集合,不包含任何元素

set c(op):以op为排序准则,产生一个空的set

set c1(c2):复制c2中的元素到c1中

set c(const value_type first, const value_type last):复制[first, last)之间元素构成新集合

set c(const value_type first, const value_type last,op):以op为排序准则,复制[first, last)之间元素构成新集合。

c.~set()销毁所有元素,释放内存

multiset mc:创建空集合,不包含任何元素

multiset mc(op):以op为排序准则,产生一个空的set

multiset c1(c2):复制c2中的元素到c1中

multiset c(const value_type first, const value_type last):复制[first, last)之间元素构成新集合

multiset c(const value_type first, const value_type last,op):以op为排序准则,复制[first, last)之间元素构成新集合。

c.~set()销毁所有元素,释放内存
In a multiset, the value of an element also identifies it (the value is itself the key, of type T). The value of the elements in a multiset cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.

Internally, the elements in a multiset are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).

multiset containers are generally slower than unordered_multiset containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.
参考:http://www.cplusplus.com/reference/set/multiset/?kw=multiset

multimap

std::multimap
template < class Key, // multimap::key_type
class T, // multimap::mapped_type
class Compare = less < Key>, // multimap::key_compare
class Alloc = allocator< pair< const Key,T> > // multimap::allocator_type > class multimap;
头文件#include < map>
begin() 返回指向第一个元素的迭代器
clear() 删除所有元素
count() 返回一个元素出现的次数
empty() 如果multimap为空则返回真
end() 返回一个指向multimap末尾的迭代器
equal_range() 返回指向元素的key为指定值的迭代器对
erase() 删除元素
find() 查找元素
get_allocator() 返回multimap的配置器
insert() 插入元素
key_comp() 返回比较key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向mulitmap尾部的逆向迭代器
rend() 返回一个指向multimap头部的逆向迭代器
size() 返回multimap中元素的个数
swap() 交换两个multimaps
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数


构造函数
explicit multimap(const Pred& comp = Pred(), const A& al = A());
multimap(const multimap& x);
multimap(const value_type *first, const value_type *last,
const Pred& comp = Pred(), const A& al = A());

begin
语法:
iterator begin();
begin()函数返回一个迭代器,指向multimap的第一个元素。
clear
语法:
void clear();
clear()函数删除multimap中的所有元素。
count
语法:
size_type count( const key_type &key );
count()函数返回multimap中键值等于key的元素的个数。
empty
语法:
bool empty();
empty()函数返回真(true)如果multimap为空,否则返回假(false)。
end
语法:
iterator end();
end()函数返回一个迭代器,指向multimap的尾部。
equal_range
语法:
pair equal_range( const key_type &key );
equal_range()函数查找multimap中键值等于key的所有元素,返回指示范围的两个迭代器。
erase
语法:
void erase( iterator pos );
void erase( iterator start, iterator end );
size_type erase( const key_type &key );
erase()函数删除在pos位置的元素,或者删除在start和end之间的元素,或者删除那些值为key的所有元素。
find
语法:
iterator find( const key_type &key );
find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向multimap尾部的迭代器。
get_allocator
语法:
allocator_type get_allocator();
get_allocator()函数返回multimap的配置器。
insert
语法:
iterator insert( iterator pos, const TYPE &val );
void insert( input_iterator start, input_iterator end );
pair insert( const TYPE &val );
insert()函数:

插入val到pos的后面,然后返回一个指向这个元素的迭代器。
插入start到end的元素到multimap中。
只有在val不存在时插入val。返回值是一个指向被插入元素的迭代器和一个描述是否插入的bool值。
key_comp
语法:
key_compare key_comp();
key_comp()函数返回一个比较key的函数。
lower_bound
语法:
iterator lower_bound( const key_type &key );
lower_bound()函数返回一个迭代器,指向multimap中键值>=key的第一个元素。
max_size
语法:
size_type max_size();
max_size()函数返回multimap能够保存的最大元素个数。
rbegin
语法:
reverse_iterator rbegin();
rbegin()函数返回一个指向multimap尾部的逆向迭代器。
rend
语法:
reverse_iterator rend();
rend()函数返回一个指向multimap头部的逆向迭代器。
size
语法:
size_type size();
size()函数返回multimap中保存的元素个数。
swap
语法:
void swap( multimap &obj );
swap()交换obj和现mulitmap中的元素。
upper_bound
语法:
iterator upper_bound( const key_type &key );
upper_bound()函数返回一个迭代器,指向multimap中键值>key的第一个元素。
value_comp
语法:
value_compare value_comp();
value_comp()函数返回一个比较元素value的函数。
示例:

#include < iostream>
#include < map>
#include < string>
using namespace std;
void main()
{
multimap

unordered_multiset/map

std::unordered_multiset
template < class Key, // unordered_multiset::key_type/value_type
class Hash = hash< Key>, // unordered_multiset::hasher
class Pred = equal_to< Key>, // unordered_multiset::key_equal
class Alloc = allocator< Key> // unordered_multiset::allocator_type > class unordered_multiset;
template < class Key, // unordered_map::key_type
class T, // unordered_map::mapped_type
class Hash = hash< Key>, // unordered_map::hasher
class Pred = equal_to< Key>, // unordered_map::key_equal
class Alloc = allocator< pair< const Key,T> > // unordered_map::allocator_type > class unordered_map;
头文件 #include < unordered_set/map>
unordered_set、unodered_multiset、unordered_Unord、unodered_multiUnord都是无序容器,都是以哈希表实现的。

关于hash:
http://kb.cnblogs.com/page/189480/

http://blog.csdn.net/sin_geek/article/details/51112431

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值