1、函数模板
//利用模板提供通用的交换函数
template<typename T>
void mySwap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
2、Vector容器
#include <iostream>
using namespace std;
#include <Vector>
void test02()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
//遍历
for(vector<int> ::iterator it = v.begin(); it != v.end();it++)
{
cout << "遍历: " << *it << endl;
}
}
int main()
{
test02();
//cout << "遍历: " << endl;
return 0;
}
3、string容器(查找)
#include <iostream>
using namespace std;
#include <string>
void test4()
{
string str1 = "abcdef";
int pos = str1.find("de"); //索引位置
cout << "con =" << pos << endl;
}
int main()
{
test4();
return 0;
}
4、vector常用操作
* `push_back(ele);` //尾部插入元素ele
* `pop_back();` //删除最后一个元素
* `insert(const_iterator pos, ele);` //迭代器指向位置pos插入元素ele
* `insert(const_iterator pos, int count,ele);`//迭代器指向位置pos插入count个元素ele
* `erase(const_iterator pos);` //删除迭代器指向的元素
* `erase(const_iterator start, const_iterator end);`//删除迭代器从start到end之间的元素
* `clear();` //删除容器中所有元素
5、vector互换元素,用来减少空间
void test02()
{
vector<int> v;
for (int i = 0; i < 100000; i++) {
v.push_back(i);
}
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
v.resize(3);
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
//收缩内存
vector<int>(v).swap(v); //匿名对象
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
}
6、vector预留空间
#include <vector>
void test01()
{
vector<int> v;
//预留空间
v.reserve(100000);
int num = 0;
int* p = NULL;
for (int i = 0; i < 100000; i++) {
v.push_back(i);
if (p != &v[0]) {
p = &v[0];
num++;
}
}
cout << "num:" << num << endl;
}
int main() {
test01();
system("pause");
return 0;
}
7、deque容器,(头尾部插入速度快)赋值操作
#include <deque>
void printDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
//赋值操作
void test01()
{
deque<int> d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printDeque(d1);
deque<int>d2;
d2 = d1;
printDeque(d2);
deque<int>d3;
d3.assign(d1.begin(), d1.end());
printDeque(d3);
deque<int>d4;
d4.assign(10, 100);
printDeque(d4);
}
int main() {
test01();
system("pause");
return 0;
}
8、deque插入和删除
两端插入操作:
- `push_back(elem);` //在容器尾部添加一个数据
- `push_front(elem);` //在容器头部插入一个数据
- `pop_back();` //删除容器最后一个数据
- `pop_front();` //删除容器第一个数据
指定位置操作:
* `insert(pos,elem);` //在pos位置插入一个elem元素的拷贝,返回新数据的位置。
* `insert(pos,n,elem);` //在pos位置插入n个elem数据,无返回值。
* `insert(pos,beg,end);` //在pos位置插入[beg,end)区间的数据,无返回值。
* `clear();` //清空容器的所有数据
* `erase(beg,end);` //删除[beg,end)区间的数据,返回下一个数据的位置。
* `erase(pos);` //删除pos位置的数据,返回下一个数据的位置。
7、deque存储
- `at(int idx); ` //返回索引idx所指的数据
- `operator[]; ` //返回索引idx所指的数据
- `front(); ` //返回容器中第一个数据元素
- `back();` //返回容器中最后一个数据元素
实例:
#include <deque>
void printDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
//数据存取
void test01()
{
deque<int> d;
d.push_back(10);
d.push_back(20);
d.push_front(100);
d.push_front(200);
for (int i = 0; i < d.size(); i++) {
cout << d[i] << " ";
}
cout << endl;
for (int i = 0; i < d.size(); i++) {
cout << d.at(i) << " ";
}
cout << endl;
cout << "front:" << d.front() << endl;
cout << "back:" << d.back() << endl;
}
int main() {
test01();
system("pause");
return 0;
}
8、deque排序
`sort(iterator beg, iterator end)` //对beg和end区间内元素进行排序
9、list链表构造函数
* `list<T> lst;` //list采用采用模板类实现,对象的默认构造形式:
* `list(beg,end);` //构造函数将[beg, end)区间中的元素拷贝给本身。
* `list(n,elem);` //构造函数将n个elem拷贝给本身。
* `list(const list &lst);` //拷贝构造函数。
实例:
#include <list>
void printList(const list<int>& L) {
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
list<int>L2(L1.begin(),L1.end());
printList(L2);
list<int>L3(L2);
printList(L3);
list<int>L4(10, 1000);
printList(L4);
}
int main() {
test01();
system("pause");
return 0;
}
10、list赋值交换
* `assign(beg, end);` //将[beg, end)区间中的数据拷贝赋值给本身。
* `assign(n, elem);` //将n个elem拷贝赋值给本身。
* `list& operator=(const list &lst);` //重载等号操作符
* `swap(lst);` //将lst与本身的元素互换。
实例:
#include <list>
void printList(const list<int>& L) {
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
//赋值和交换
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
//赋值
list<int>L2;
L2 = L1;
printList(L2);
list<int>L3;
L3.assign(L2.begin(), L2.end());
printList(L3);
list<int>L4;
L4.assign(10, 100);
printList(L4);
}
//交换
void test02()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
list<int>L2;
L2.assign(10, 100);
cout << "交换前: " << endl;
printList(L1);
printList(L2);
cout << endl;
L1.swap(L2);
cout << "交换后: " << endl;
printList(L1);
printList(L2);
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
11、list判断是否为空
#include <list>
void printList(const list<int>& L) {
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
//大小操作
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
if (L1.empty())
{
cout << "L1为空" << endl;
}
else
{
cout << "L1不为空" << endl;
cout << "L1的大小为: " << L1.size() << endl;
}
//重新指定大小
L1.resize(10);
printList(L1);
L1.resize(2);
printList(L1);
}
int main() {
test01();
system("pause");
return 0;
}
12、list 插入和删除
#### list 插入和删除
**功能描述:**
* 对list容器进行数据的插入和删除
**函数原型:**
* push_back(elem);//在容器尾部加入一个元素
* pop_back();//删除容器中最后一个元素
* push_front(elem);//在容器开头插入一个元素
* pop_front();//从容器开头移除第一个元素
* insert(pos,elem);//在pos位置插elem元素的拷贝,返回新数据的位置。
* insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。
* insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。
* clear();//移除容器的所有数据
* erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置。
* erase(pos);//删除pos位置的数据,返回下一个数据的位置。
* remove(elem);//删除容器中所有与elem值匹配的元素。
* #include <list>
void printList(const list<int>& L) {
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
//插入和删除
void test01()
{
list<int> L;
//尾插
L.push_back(10);
L.push_back(20);
L.push_back(30);
//头插
L.push_front(100);
L.push_front(200);
L.push_front(300);
printList(L);
//尾删
L.pop_back();
printList(L);
//头删
L.pop_front();
printList(L);
//插入
list<int>::iterator it = L.begin();
L.insert(++it, 1000);
printList(L);
//删除
it = L.begin();
L.erase(++it);
printList(L);
//移除
L.push_back(10000);
L.push_back(10000);
L.push_back(10000);
printList(L);
L.remove(10000);
printList(L);
//清空
L.clear();
printList(L);
}
int main() {
test01();
system("pause");
return 0;
}
13、list 数据存取
front();
//返回第一个元素。back();
//返回最后一个元素。
14、 list 反转和排序
reverse();
//反转链表sort();
//链表排序
15、set和multiset
* set不允许容器中有重复的元素
* multiset允许容器中有重复的元素
16、set插入和删除
* `insert(elem);` //在容器中插入元素。
* `clear();` //清除所有元素
* `erase(pos);` //删除pos迭代器所指的元素,返回下一个元素的迭代器。
* `erase(beg, end);` //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
* `erase(elem);`
* `find(key);` //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
* `count(key);` //统计key的元素个数
17、pair对组创建
pair<type, type> p ( value1, value2 );
pair<type, type> p = make_pair( value1, value2 );
#include <string>
//对组创建
void test01()
{
pair<string, int> p(string("Tom"), 20);
cout << "姓名: " << p.first << " 年龄: " << p.second << endl;
pair<string, int> p2 = make_pair("Jerry", 10);
cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}
int main() {
test01();
system("pause");
return 0;
}
18、set容器排序(自定义排序,从大到小):利用仿函数
#include <set>
class MyCompare
{
public:
bool operator()(int v1, int v2) {
return v1 > v2;
}
};
void test01()
{
set<int> s1;
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
s1.insert(50);
//默认从小到大
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
cout << *it << " ";
}
cout << endl;
//指定排序规则
set<int,MyCompare> s2;
s2.insert(10);
s2.insert(40);
s2.insert(20);
s2.insert(30);
s2.insert(50);
for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
19、map构造和赋值
map<T1, T2> mp;
//map默认构造函数:map(const map &mp);
//拷贝构造函数
#include <map>
void printMap(map<int,int>&m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int,int>m; //默认构造
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
printMap(m);
map<int, int>m2(m); //拷贝构造
printMap(m2);
map<int, int>m3;
m3 = m2; //赋值
printMap(m3);
}
int main() {
test01();
system("pause");
return 0;
}
20、map大小和交换
- `size();` //返回容器中元素的数目
- `empty();` //判断容器是否为空
- `swap(st);` //交换两个集合容器
21、 map插入和删除
- `insert(elem);` //在容器中插入元素。
- `clear();` //清除所有元素
- `erase(pos);` //删除pos迭代器所指的元素,返回下一个元素的迭代器。
- `erase(beg, end);` //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
- `erase(key);` //删除容器中值为key的元素。
22、map查找和统计
find(key);
//查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();count(key);
//统计key的元素个数
#include <map>
//查找和统计
void test01()
{
map<int, int>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
//查找
map<int, int>::iterator pos = m.find(3);
if (pos != m.end())
{
cout << "找到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl;
}
else
{
cout << "未找到元素" << endl;
}
//统计
int num = m.count(3);
cout << "num = " << num << endl;
}
int main() {
test01();
system("pause");
return 0;
}
23、 map容器排序(仿函数,可以改变排序规则)
#include <map>
class MyCompare {
public:
bool operator()(int v1, int v2) {
return v1 > v2;
}
};
void test01()
{
//默认从小到大排序
//利用仿函数实现从大到小排序
map<int, int, MyCompare> m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
m.insert(make_pair(4, 40));
m.insert(make_pair(5, 50));
for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key:" << it->first << " value:" << it->second << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
24、