序列式容器的共性:vector deque list
- 置顶元素个数和初始值(初始值默认为零初始化)
- 插入:insert(pos, n, element) insert(pos, pos_beg, pos_end)
- 赋值:assign(n, element) assign(pos_beg, pos_end)
- 调整:resize(n, element = 零初始化)
- 首尾:front(), back()
- 末尾插入和删除数据:push_back(element) pop_back()只删除,返回void
共性部分
#include <iostream>
#include <deque>
#include <algorithm>
#include <string>
using namespace std;
template <typename T>
void print(T b, T e, string s = "; "){
while(b!=(e-1)) {
cout << *b++ << s;
}
cout << *b << endl;
}
int main(){
deque<string> ds;
//末尾插入数据
ds.push_back("ashaji");
ds.push_back("makain");
ds.push_back("spansd");
ds.push_back("uertnd");
print(ds.begin(), ds.end(), " - ");
//指定位置插入单个数据
ds.insert(ds.begin()+2, 3, "charushuju");
print(ds.begin(), ds.end(), " - ");
//指定位置插入数组数据
string s[3] = {"aaa","bbb","ccc"};
ds.insert(ds.end()-2, s, s+3);
print(ds.begin(), ds.end(), " * ");
//末尾删除
ds.pop_back();
ds.pop_back();
print(ds.begin(), ds.end(), " * ");
//指定位置删除
ds.erase(ds.end()-2);
print(ds.begin(), ds.end(), " * ");
//调整容器大小 - 零初始化
ds.resize(10);
print(ds.begin(), ds.end(), " * ");
//调整容器大小 - 制定初始值
ds.resize(12, "?uu?");
print(ds.begin(), ds.end(), " - ");
//批量赋值:容器大小, 值
ds.assign(5, "assign");
print(ds.begin(), ds.end(), " - ");
//通过返回指针引用来赋值
ds.front() = "front_fuzhi";
ds.back() = "back_fuzhi";
print(ds.begin(), ds.end(), " - ");
getchar();
return 0;
}
Vector容器的个性:
- 为了避免频繁分配内存,因此每次分配都会有余量,查询目前分配的容量:capacity()
- 如果不希望频繁分配,可以实现约定好容量:reserve(n)
- 支持下标访问:.operator[](i)(没有越界检查),.at(i)(有越界异常抛出)
- Vector迭代器在插入或删除数据后可能会失效:指容器的首指针因可能引起的内存分配而改变。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <exception>
#include <typeinfo>
using namespace std;
template <typename T>
void print(T b, T e, string s = "; "){
while(b!=(e-1)) {
cout << *b++ << s;
}
cout << *b << endl;
}
int main(){
vector<double> vd, vv;
for(int i=0; i<9; i++){
vd.push_back(i+0.1);
//因可能的容量变化引起vector容器失效
cout << &*vd.begin() << " ---- ";
//观察内存分配
cout << vd.size() << " " << vd.capacity() << endl;
}
vv.reserve(9);
for(int i=0; i<9; i++){
vv.push_back(i+0.1);
//观察内存分配
cout << vv.size() << " / " << vv.capacity() << endl;
}
try{
vd.at(10);
}catch(exception& e){
cout << "Exception: " << e.what() << endl;
cout << "TheType: " << typeid(e).name() << endl;
}
getchar();
return 0;
}
deque容器的个性:
- 一般是由多个vector来实现的,它叫做 double-ended queue,支持两端插入删除、支持下标
- 下标:.operator[](i)不检查越界,.at(i)检查越界抛出异常
- 没有容量一说
- 增删:push_front(element) pop_front() push_back(element) pop_back()
#include <iostream>
#include <deque>
#include <algorithm>
#include <string>
#include <exception>
#include <typeinfo>
using namespace std;
template <typename T>
void print(T b, T e, string s = "; "){
while(b!=(e-1)) {
cout << *b++ << s;
}
cout << *b << endl;
}
int main(){
deque<char> ds;
ds.push_back(97);
ds.push_back('c');
ds.push_front('t');
ds.push_front('o');
print(ds.begin(), ds.end());
cout << ds[1]++ << endl;
cout << ds[1] << endl;
ds.pop_back();
print(ds.begin(), ds.end());
getchar();
return 0;
}
list容器的个性:
- 是一个双向链表,支持前后增删数据
- 条件删除:remove(element)删除指定元素的所有数据
- 不支持下标运算
- 除重:unique() 相邻的重复元素只保留一个,不相邻的不管
- 排序:sort(compare_fun = less),默认用小于符号
- 倒置:reverse()把列表内所有数据颠倒
- 转移:.splice(pos, list2) splice(pos, list2, pos2) splice(pos, list2, pos2_beg, pos2_end)
- 归并:merge(list2) 把另一个列表的数据归并过来,但要实现排好序
#include <iostream>
#include <list>
#include <algorithm>
#include <string>
#include <exception>
#include <cassert>
using namespace std;
template <typename T>
void print(T b, T e, string s = "; "){
// while((b+1)!=e) {
// cout << *b++ << s;
// }
// cout << *b << endl;
while(b!=e) {
cout << *b++ << s;
}
cout << endl;
}
//按照除3的余数大小排序,函数名字无所谓。
bool compare(int n1, int n2){
return (n1%3)<(n2%3);
}
int main(){
int a[10] = {3, 8, 8, 8, 5, 5, 1, 8, 8, 7};
int b[6] = {9, 3, 5, 2, 6, 7};
list<int> li(a, a+10);
list<int> lili(b, b+6);
print(li.begin(), li.end());
//去除相连重复
li.unique();
print(li.begin(), li.end());
//排序
li.sort();
print(li.begin(), li.end());
//去除重复
li.unique();
print(li.begin(), li.end());
//翻转
li.reverse();
print(li.begin(), li.end());
//转移,清空了lili
li.splice(li.begin(), lili);
print(li.begin(), li.end());
print(lili.begin(), lili.end());//为空了
//assert(!lili.empty());
//清除所有指定数据
li.remove(5);
print(li.begin(), li.end());
//融合,需事先排好序
list<int> lilili(b, b+6);
li.sort();
lilili.sort();
li.merge(lilili);
print(li.begin(), li.end());
//赋值
lili.assign(b, b+6);
print(lili.begin(), lili.end());
//大到小排序
lili.sort(greater<int>());
print(lili.begin(), lili.end());
//自定义排序
lili.sort(compare);
print(lili.begin(), lili.end());
getchar();
return 0;
};