序列式容器
一、定义
二、成员函数
1.array 、 vector、deque
2.list、forward_list
三、具体介绍
1.vector
(1)初始化
(2)扩容
2.deque
(1)简介
(2)使用实例
void test()
{
deque<int> d1;
d1.push_back(10);
d1.push_back(30);
d1.push_back(20);
d1.push_back(40);
d1.push_back(50);
d1.pop_back(); /* 删除最后的元素 */
d1.pop_front(); /* 删除最前面的元素 */
deque<int> d2;
d2.push_back(50);
d2.push_back(60);
d2.insert(d2.begin(), d1.begin(), d1.end());
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
void printDeque(const deque<int> &d)
{
for(deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
/* 回调函数 */
bool compare(int v1, int v2)
{
return v1 > v2; /* 此时的排序是从大到小 */
/* 如果从小到大应该改为"v1 < v2" */
}
void test()
{
deque<int> d;
d.push_back(3);
d.push_back(4);
d.push_back(1);
d.push_back(7);
d.push_back(2);
sort(d.begin(), d.end(), compare);
printDeque(d);
3.list
(1)双向链表
(2)一些独有方法
#include <list> //使用 list 需要包含此头文件
#include <iostream>
#include <algorithm> //使用STL中的算法需要包含此头文件
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 & a);
};
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 & a) {
o << a.n;
return o;
}
template <class T>
void Print(T first, T last)
{
for (; first != last; ++first)
cout << *first << " ";
cout << endl;
}
int main()
{
A a[5] = { 1, 3, 2, 4, 2 };
A b[7] = { 10, 30, 20, 30, 30, 40, 40 };
list<A> lst1(a, a + 5), lst2(b, b + 7);
lst1.sort();
cout << "1)"; Print(lst1.begin(), lst1.end()); //输出:1)1 2 2 3 4
lst1.remove(2); //删除所有和A(2)相等的元素
cout << "2)"; Print(lst1.begin(), lst1.end()); //输出:2)1 3 4
lst2.pop_front(); //删除第一个元素
cout << "3)"; Print(lst2.begin(), lst2.end()); //输出:3)30 20 30 30 40 40
lst2.unique(); //删除所有和前一个元素相等的元素
cout << "4)"; Print(lst2.begin(), lst2.end()); //输出:4)30 20 30 40
lst2.sort();
lst1.merge(lst2); //合并 lst2 到 lst1 并清空 lst2
cout << "5)"; Print(lst1.begin(), lst1.end()); //输出:5)1 3 4 20 30 30 40
cout << "6)"; Print(lst2.begin(), lst2.end()); //lst2是空的,输出:6)
lst1.reverse(); //将 lst1 前后颠倒
cout << "7)"; Print(lst1.begin(), lst1.end()); //输出 7)40 30 30 20 4 3 1
lst2.insert(lst2.begin(), a + 1, a + 4); //在 lst2 中插入 3,2,4 三个元素
list <A>::iterator p1, p2, p3;
p1 = find(lst1.begin(), lst1.end(), 30);
p2 = find(lst2.begin(), lst2.end(), 2);
p3 = find(lst2.begin(), lst2.end(), 4);
lst1.splice(p1, lst2, p2, p3); //将[p2, p3)插入p1之前,并从 lst2 中删除[p2,p3)
cout << "8)"; Print(lst1.begin(), lst1.end()); //输出:8)40 2 30 30 20 4 3 1
cout << "9)"; Print(lst2.begin(), lst2.end()); //输出:9)3 4
return 0;
} (4)应用实例
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<int> monkeys;
int n, m;
while (true) {
cin >> n >> m;
if (n == 0 && m == 0)
break;
monkeys.clear(); //清空list容器
for (int i = 1; i <= n; ++i) //将猴子的编号放入list
monkeys.push_back(i);
list<int>::iterator it = monkeys.begin();
while (monkeys.size() > 1) { //只要还有不止一只猴子,就要找一只猴子让其出列
for (int i = 1; i < m; ++i) { //报数
++it;
if (it == monkeys.end())
it = monkeys.begin();
}
it = monkeys.erase(it); //删除元素后,迭代器失效,
//要重新让迭代器指向被删元素的后面
if (it == monkeys.end())
it = monkeys.begin();
}
cout << monkeys.front() << endl; //front返回第一个元素的引用
}
return 0;
}