C++ 标准模板库 STL 顺序容器
动态数组 vector
vector #include <vector>
动态数组:其元素在内存中是连续存放的,随机存取任何元素都可以在常数时间内完成,在该容器的尾部增删元素也几乎能够在常数时间内完成具有较好的性能。
一个 vector 常用函数使用实例如下:
#include <iostream>#include <vector>using namespace std;template<class T>void printVector(T s, T e){ for(;s != e; ++s){ cout << *s << ' '; } cout << endl;}int main(){ int a[5] = {1,2,3,4,5}; vector<int> v(a,a+5); cout << "1)" << v.end() - v.begin() << endl; cout << "2)"; printVector(v.begin(), v.end()); v.insert(v.begin()+3, 100); // insert()函数插入元素 cout << "3)"; printVector(v.begin(), v.end()); vector<int> v2(5,0); v2.insert(v2.begin()+2,v.begin(),v.end()); // insert() 函数数据块插入 cout << "4)"; printVector(v2.begin(), v2.end()); v.erase(v.begin()+3); // erase() 函数删除元素 cout << "5)"; printVector(v.begin(), v.end()); v.erase(v.begin()+2,v.begin()+4); // erase() 函数删除数据块 cout << "6)"; printVector(v.begin(), v.end()); return 0;}
构造函数
容量管理函数
增删函数
position
)或一系列元素([first,last)
),这有效地减少了被去除的元素的数量,从而破坏了容器的大小 clear 从 vector 中删除所有的元素(被销毁),留下 size 为 0 的容器 emplace 通过在 position(参数)位置处插入新元素 args(参数)来扩展容器 emplace_back 在 vector 的末尾插入一个新的元素,紧跟在当前的最后一个元素之后
索引函数
迭代器函数
用 vector 实现二维数组
#include <iostream>#include <vector>using namespace std;int main(){ vector<vector<int>> arr(3); // arr中有 3 个元素,每个元素都是 vector<int> 容器 for(int i=0; i<arr.size(); ++i){ for(int j=0; j<3; ++j){ arr[i].push_back(j); } } for(int i=0; i<arr.size(); ++i){ for(int j=0; j<3; ++j){ cout << arr[i][j] << ' '; } cout << endl; } return 0;}
双向队列 deque
deque #include <deque>
双向队列:其元素在内存中是连续存放的,随机存取任何元素都可以在常数时间内完成,在该容器的两端增删元素也几乎能够在常数时间内完成具有较好的性能。
所有适用于 vector 的操作都适用于 deque,除此之外,deque 还有 push_front / pop_front
在最前端插入或删除元素的操作,复杂的都是 O ( 1 ) O(1) O(1) 。
双向链表 list
list #include <list>
双向链表:其元素在内存中是不连续存放的,不支持随机存取,在该容器的任何位置增删元素几乎都能够在常数时间内完成具有较好的性能。
list 除了具有所有顺序容器都有的成员函数之外,还支持以下8个成员函数:
一个 list 的成员函数使用实例如下:
#include <iostream>#include <list>#include <algorithm>using namespace std;class A{ private: int n; public: A(int n_){n = n_;} friend bool operator<(const A & a1, const A & a2); friend bool operator==(const A & a1, const A & a2); friend ostream& operator<<(ostream & o, const A & a2);};bool operator<(const A & a1, const A & a2){ return a1.n < a2.n;}bool operator==(const A & a1, const A & a2){ return a1.n == a2.n;}ostream& operator<<(ostream & o, const A & a2){ o << a2.n; return o;}template <class T>void printList(T start, T end){ for(;start != end; ++start){ cout << *start << ','; } cout << endl;}int main(){ list<A> lst1, lst2; lst1.push_back(1); lst1.push_back(3); lst1.push_back(2); lst1.push_back(4); lst1.push_back(2); lst2.push_back(10); lst2.push_back(50); lst2.push_back(30); lst2.push_back(30); lst2.push_back(40); lst2.push_back(40); lst2.push_back(30); lst2.push_back(20); cout << "1) "; printList(lst1.begin(),lst1.end()); cout << "2) "; printList(lst2.begin(),lst2.end()); lst2.sort(); // 调用 sort 成员函数进行 lst2 排序 cout << "3) "; printList(lst2.begin(),lst2.end()); lst2.unique(); // 删除 lst2 中所有和前一个元素相等的元素 cout << "4) "; printList(lst2.begin(),lst2.end()); lst2.pop_front(); // 将 lst2 的第一个元素删除 cout << "5) "; printList(lst2.begin(),lst2.end()); lst1.remove(2); // 删除 lst1 中所有值为 2 的元素 cout << "6) "; printList(lst1.begin(),lst1.end()); lst2.merge(lst1); // 将 lst1 中的元素合并到 lst2 并将 lst1 清空 cout << "7) "; printList(lst2.begin(),lst2.end()); lst2.reverse(); // 颠倒 lst2 cout << "8) "; printList(lst2.begin(),lst2.end()); lst1.push_back(100); lst1.push_back(200); lst1.push_back(300); lst1.push_back(400); cout<<"9) "; printList(lst1.begin(),lst1.end()); // 找到列表中的指定元素 list<A>::iterator p1,p2,p3; p1 = find(lst2.begin(),lst2.end(),4); p2 = find(lst1.begin(),lst1.end(),200); p3 = find(lst1.begin(),lst1.end(),400); lst2.splice(p1,lst1,p2,p3); // 将 lst1 中 [p2,p3) 区间内的元素插入到 lst2 中 p1 位置之前,并将这些元素从 lst1 中删除 cout<<"10) "; printList(lst1.begin(),lst1.end()); cout<<"11) "; printList(lst2.begin(),lst2.end()); return 0;}
到此这篇关于C++ 标准模板库 STL 顺序容器详解的文章就介绍到这了,更多相关C++ 标准模板库 STL 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
沿途跟着一条河,你看着它在晨光暮霭中变换着色彩,