包括三个基本组件:
- 容器
- 迭代器
- 算法
容器
顺序容器:
- vector
- deque:双向队列
- list:双链表,从任何地方插入和删除
关联容器:
- set
- multiset
- map
- multimap
容器适配器:
- stack
- queue
- priority_queue
容器都是类模板,实例化后成为容器类,用容器类定义的对象称为容器对象。
成员函数
所有容器都有的成员函数:
- int size():返回容器中元素的个数
- bool empty():判断容器是否为空
顺序容器和关联容器中的成员函数:
- begin():返回指向容器中第一个元素的迭代器
- end():返回指向容器中最后一个元素后面的位置的迭代器
- rbegin():返回指向最后一个元素的反向迭代器
- rend():返回反转后最后一个元素后面位置的元素
- erase():从容器中删除一个或多个元素
- clear():清空容器
顺序容器中的成员函数:
- front():返回容器中第一个元素的引用
- back():返回容器中最后一个元素的引用
- push_back():在容器末尾增加新的元素
- pop_back():删除容器末尾的元素
- insert():插入一个或多个元素
vector()
#include <iostream> #include <vector> using namespace std; int main() { vector<int> c; int i; c.assign(10, 5); // 10个5赋给c for (i = 0; i < 10; i++) cout << c[i] << endl; cout << endl; cout << c.size() << endl; return 0; }
#include <iostream> #include <vector> using namespace std; int main() { vector<int> c(20); // 使用assign()之后c的大小也发生了变化 c.assign(10, 5); for (int i = 0; i < c.size(); ++i) { c[i] = 5 - i; cout << c[i] << endl; } c.push_back(100); int t = c.at(0); cout << t << endl; t = c.front(); cout << t << endl; t = *(c.begin()); cout << t << endl; t = c.back(); cout << t << endl; return 0; }
#include <iostream> #include <vector> using namespace std; int main() { vector<int> c(20); // 使用assign()之后c的大小也发生了变化 c.assign(10, 5); for (int i = 0; i < c.size(); ++i) { c[i] = 5 - i; cout << c[i] << endl; } c.push_back(100); int t = c.at(0); cout << t << endl; t = c.front(); // 返回第一个元素 cout << t << endl; // 返回第一个元素的地址 t = *(c.begin()); cout << t << endl; t = c.back(); cout << t << endl; return 0; }
vector插入元素
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v(3); v[0] = 2; v[1] = 7; v[2] = 9; for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } cout << endl; v.insert(v.begin(), 8); // 在最前面插入新元素 for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } cout << endl; v.insert(v.begin() + 2, 1); for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; } cout << endl; v.insert(v.end(), 3); for (int i = 0; i < v.size(); ++i) cout << v[i] << " "; cout << endl; return 0; }
erase
#include <iostream> #include <vector> using namespace std; int main() { vector<int> c(5, 123); int i; int temp; cin >> temp; c.push_back(temp); c.insert(c.begin() + 2, 12); // for (i = 0; i < 7; i++) { // cout << c[i] << endl; // } cout << c.empty() << endl; c.erase(c.begin(), c.end()); cout << c.empty() << endl; for (i = 0; i < 7; i++) { cout << c[i] << endl; } return 0; }
这里虽然已经把vector中的数据都清空了,但是最后一个for循环还是会把之前写入的数据输出出来。
迭代器
迭代器的定义方式
- 正向迭代器
容器类名::iterator 迭代器名
- 常量迭代器
容器类名::const_iterator 迭代器名
- 反向迭代器
容器类名::reverse_iterator 迭代器名
- 常量反向迭代器
容器类名::const_reverse_iterator 迭代器名
迭代器的类型
- 正向迭代器
- ++p, p++
- 双向迭代器
- ++p, p++, --p, p--
- 随机访问迭代器
- p += i
#include <iostream> #include <vector> using namespace std; int main() { vector<int> c; for (int i = 0; i < 5; ++i) { c.push_back(i); cout << c[i] << " "; } cout << endl; vector<int>::iterator p; p = c.begin(); p++; *p = 20; for (int i = 0; i < 5; ++i) { cout << c[i] << " "; } return 0; }
#include <iostream> #include <vector> using namespace std; int main() { vector<int> arr; arr.push_back(6); arr.push_back(8); arr.push_back(3); arr.push_back(8); vector<int>::iterator it; for (it = arr.begin(); it != arr.end();) { if (*it == 8) { it = arr.erase(it); } else { ++it; } } cout << "After remove 8: "; for (it = arr.begin(); it < arr.end(); ++it) cout << *it << " "; return 0; }
反向迭代器
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v; for (int i = 0; i < 5; ++i) v.push_back(i); vector<int>::iterator it; for (it = v.begin(); it != v.end(); ++it) { cout << *it << " "; *it *= 2; } cout << endl; for (vector<int>::reverse_iterator j = v.rbegin(); j != v.rend(); ++j) cout << *j << " "; cout << endl; for (vector<int>::iterator j = v.end() - 1; j >= v.begin(); --j) cout << *j << " "; return 0; }
用于操作迭代器的三个函数模板:
- advance(p, n): 使迭代器向前或向后移动n个元素
- distance(p, q): 计算两个两个迭代器之间的距离
- iter_swap(p, q): 用于交换两个迭代器指向的值
#include <algorithm> #include <iostream> #include <list> using namespace std; int main() { int a[5] = {1, 2, 3, 4, 5}; list<int> lst(a, a + 5); list<int>::iterator p = lst.begin(); for (int i = 0; i < lst.size(); ++i) { cout << *(p++) << " "; } p = lst.begin(); advance(p, 2); // 双向循环链表? cout << " 1)" << *p << endl; advance(p, -1); cout << " 2)" << *p << endl; list<int>::iterator q = lst.end(); q--; cout << "3)" << distance(p, q) << endl; iter_swap(p, q); cout << "4)"; for (p = lst.begin(); p != lst.end(); ++p) cout << *p << " "; return 0; }
算法
模板
函数模板
template <typename 类型参数1, typename 类型参数2, ....>
返回值类型 函数名(参数列表) {
// TODO
}
#include <iostream> using namespace std; template <typename T> void Swap(T *a, T *b) { T temp = *a; *a = *b; *b = temp; } int main() { //交换int 变量的值 int n1 = 100, n2 = 200; Swap(&n1, &n2); cout << n1 << ", " << n2 << endl; //交换char 变量的值 char c1 = 'A', c2 = 'B'; Swap(&c1, &c2); cout << c1 << ", " << c2 << endl; return 0; }
类模板
template<typename 类型参数1, typename 类型参数2, …>
class 类名
{
//TODO:
};
ClassName <类型> 对象名:
vector<int> a;
#include <iostream> using namespace std; template <typename T1, typename T2> class Point { private: T1 m_x; T2 m_y; public: Point(T1 x, T2 y) : m_x(x), m_y(y) {} T1 getX() const; void setX(T1 x); T2 getY() const; void setY(T2 y); }; template <typename T1, typename T2> // 模板头 T1 Point<T1, T2>::getX() const { /* 函数头 */ return m_x; } template <typename T1, typename T2> void Point<T1, T2>::setX(T1 x) { m_x = x; } template <typename T1, typename T2> T2 Point<T1, T2>::getY() const { return m_y; } template <typename T1, typename T2> void Point<T1, T2>::setY(T2 y) { m_y = y; } int main() { Point<int, int> p1(10, 20); cout << "x = " << p1.getX() << ". y = " << p1.getY() << endl; Point<int, char *> p2(10, "East longitude 180"); cout << "x = " << p2.getX() << ". y = " << p2.getY() << endl; Point<char *, char *> *p3 = new Point<char *, char *>("East longitude 180", "north latitude 210"); cout << "x = " << p3->getX() << ". y = " << p3->getY() << endl; return 0; }
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v(3);
v[0] = 2;
v[1] = 7;
v[2] = 9;
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
cout << endl;
v.insert(v.begin(), 8); // 在最前面插入新元素
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
cout << endl;
v.insert(v.begin() + 2, 1);
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
cout << endl;
v.insert(v.end(), 3);
for (int i = 0; i < v.size(); ++i) cout << v[i] << " ";
cout << endl;
return 0;
}