vector
#include <iostream>
#include <vector>
using namespace std;
template<class T>
void PrintVector(T s, T e) {
for(; s != e; ++s)
cout << *s << endl " ";
cout << endl;
}
void main() {
int a[5] = {1, 2, 3, 4, 5};
vector<int> v(a, a+5); // 将a中的元素给v
// 两个随机迭代器相减,输出 1) 5
cout << "1) " << v.end() - v.begin();
// 2) 1 2 3 4 5
cout << "2) "; PrintVector(v.begin(), v.end());
v.insert(v.begin() + 2, 13); // 在数组中插入元素13
// 3) 1 2 13 3 4 5
cout << "3) "; PrintVector(v.begin(), v.end());
v.erase(v.begin() + 2); // 删除位于 begin() + 2的元素
// 4) 1 2 3 4 5
cout << "4) "; PrintVector(v.begin(), v.end());
vector<int> v2(4, 100); // v2 有4个100
// 将v的一段插入v2的开头
v2.insert(v2.begin(), v.begin() + 1, v.begin() + 3);
// 5) v2: 2 3 100 100 100 100
cout << "5) "; PrintVector(v2.begin(), v2.end());
// 删除 v 上的一个区间,即 2,3
v.erase(v.begin() + 1, b.begin() + 3);
// 6) 1 4 5
cout << "6) "; PrintVector(v.begin(), v.end());
}
vector实现二维数组
vector<vector<int> >
这里要把最后两个”>”分开,不然有的编译器会认为是右移运算符#include <iostream>
#include <vector>
using namespace std;
void main() {
vector<vector<int> > v(3);
for(int i = 0; i < v.size(); ++i)
for(int j = 0; j < 4; ++j)
v[i].push_back(j);
for(int i = 0; i < v.size(); ++i) {
for(int j = 0; j < v[i].size(); ++j)
cout << v[i][j] << " ";
cout << endl;
}
}
deque
双向链表list
#include <list>
#include <iostream>
#include <algotirhm>
using namespace std;
class A {
private:
int n;
public:
A (int n_) { n = n_;}
friend bool operator<(const A & a1, cosnt 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 PrintList(const list<T> & lst) {
// 不推荐的写法,还是两个迭代器为参数比较好
int tmp = lst.size();
if(tmp > 0) {
typename list<T>::const_iterator i;
i = lst.begin();
for(; i != lst.end(); ++i)
cout << *i << ",";
cout << endl;
}
}
void main() {
list<A> lst1, lst2;
lst1.push_back(1); lst1.push_back(2);
lst1.push_back(3); lst1.push_back(4);
lst1.push_back(2);
lst2.push_back(10); lst2.push_back(20);
lst2.push_back(30); lst2.push_back(30);
lst2.push_back(30); lst2.push_back(40);
lst2.push_back(40);
// 1) 1,3,2,4,2
cout << "1) "; PrintList(lst1);
// 2) 40,20,10,30,30,30,40
cout << "2) "; PrintList(lst2);
lst2.sort();
// 3) 10,20,30,30,30,40,40
cout << "3) "; PrintList(lst2);
lst2.pop_front();
// 4) 20,30,30,30,40,40
cout << "4) "; PrintList(lst2);
lst1.remove(2); // 删除所有和A(2)相等的元素
// 5) 1,3,4
cout << "5) "; PrintList(lst1);
lst2.unique(); // 删除所有和前一个元素相等的元素
// 6) 20,30,40
cout << "6) "; PrintList(lst2);
lst1.merge(lst2); // 合并lst2到lst1并清空lst2
// 7) 1,3,4,20,30,40
cout << "7) "; PrintList(lst1);
// 8)
cout << "8) "; PrintList(lst2);
lst1.reverse();
// 9) 40,30,20,4,3,1
cout << "9) "; PrintList(lst1);
lst2.push_back(100); lst2.push_back(200);
lst2.push_back(300); lst2.push_back(400);
list<A>::iterator p1, p2, p3;
p1 = find(lst1.begin(), lst1.end(), 3);
p2 = find(lst2.begin(), lst2.end(), 200);
p3 = find(lst2.begin(), lst2.end(), 400);
// 将[p2, p3)插入p1之前,并从lst2中删除[p2, p3)
lst1.splice(p1, lst2, p2, p3);
// 10) 40,30,20,4,200,300,3,1
cout << "10) "; PrintList(lst1);
// 11) 100,400
cout << "11) "; PrintList(lst2);
}
模板作为参数的写法
- dev中在模板参数类型前面要加typename
- splice的用法