这部分比较偏向算法的使用,固定的模板,需要向里面填入参数,所以上代码与注释理解更为方便
1.vector(容器)
#include <iostream> #include <vector> using namespace std; void printV(vector<int>::iterator begin, vector<int>::iterator end) { for (; begin != end; begin++) cout <<*begin << " "; cout << endl; } void printV(vector<int> &v) { vector<int>::iterator it = v.begin(); for (; it != v.end(); it++) cout <<*it << " "; cout << endl; } // 1、数据的访问 void func1() { vector<int> v; for (int i = 0; i < 10; i++) v.push_back(i+1); cout << "容器的第一个元素 : " << v.front() << endl; cout << "容器的最后一个元素: " << v.back() << endl; cout << "容器的元素个数 : " << v.size() << endl; while (!v.empty()) { cout << v.back() << " "; v.pop_back(); } cout << endl; } // 2、构造 void func2() { // vector 和 string 一样 都可以通过[] 和 at() 函数来遍历数据 // 定义一个容器,预先分配10个空间 vector<int> v(10); cout << v.size() << endl; for (unsigned int i = 0; i < v.size(); i++) v[i] = i+1; // 使用 [] 的一定要保证不能越界,[]和push_back不一样,不会开辟新空间 printV(v.begin(), v.end()); vector<int> v2(10); v2.push_back(1); v2.push_back(2); cout << v2.size() << endl; vector<int> v3 = v2; v3 = v; vector<int> v4(10, 1); printV(v4.begin(), v4.end()); vector<int> v5(v.begin()+5, v.begin()+10); printV(v5.begin(), v5.end()); // 重新调整空间大小 v.resize(1000); cout << v.size() << endl; } //3、 遍历 void func3() { vector<int> v(10); for (unsigned int i = 0; i < v.size(); i++) v[i] = i+1; // 迭代器 vector<int>::reverse_iterator rit = v.rbegin(); for (; rit != v.rend(); rit++) cout << *rit << " "; cout << endl; } // 4、删除和插入 void func4() { vector<int> v(10); for (unsigned int i = 0; i < v.size(); i++) v[i] = i+1; // 迭代器删除 v.erase(v.begin()); printV(v); v.erase(v.begin()+5, v.begin()+8); printV(v); v.insert(v.begin(), 20); v.insert(v.begin()+3, 45); v.insert(v.end(), 98); printV(v); vector<int> v2(10); v2.insert(v2.begin()+5, v.begin()+3, v.begin()+5); printV(v2); // 容器清空 v.clear(); v.erase(v.begin(), v.end()); // 定义一个空的容器,和当前容器进行交换 vector<int> v3; v2.swap(v3); cout << v2.size() << endl; } // 5、数据删除 void func5() { vector<int> v; v.push_back(1); v.push_back(3); v.push_back(7); v.push_back(3); v.push_back(4); v.push_back(3); vector<int>::iterator it; for (it = v.begin(); it != v.end();) { if (*it == 3) // 返回值是当前元素下一个元素的迭代器 it = v.erase(it); else it++; } printV(v); } int main() { // func1(); // func2(); // func3(); // func4(); func5(); return 0; }
2.deque(双端队列)
#include <iostream>
#include <deque>
using namespace std;
void printD(deque<int> &d)
{
deque<int>::iterator it = d.begin();
for (; it != d.end(); it++)
cout << *it << " ";
cout <<endl;
}
void func1()
{
deque<int> d;
d.push_back(1);
d.push_back(2);
d.push_back(3);
d.push_front(-1);
d.push_front(-2);
d.push_front(-3);
d.pop_back();
d.pop_front();
printD(d);
}
3.栈和队列
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#include <stack>
#include <queue>
using namespace std;
// 栈
void func1()
{
stack<int> s;
s.push(8);
s.push(4);
s.push(2);
s.push(19);
void func1()
{
stack<int> s;
s.push(8);
s.push(4);
s.push(2);
s.push(19);
cout << "栈的元素个数:" << s.size() << endl;
cout << "栈顶元素 :" << s.top() << endl;
cout << "栈顶元素 :" << s.top() << endl;
while (!s.empty())
{
cout << s.top() << endl;
s.pop();
}
}
{
cout << s.top() << endl;
s.pop();
}
}
// 队列
void func2()
{
queue<int> q;
q.push(8);
q.push(4);
q.push(2);
q.push(19);
void func2()
{
queue<int> q;
q.push(8);
q.push(4);
q.push(2);
q.push(19);
cout << "队列的元素个数:" << q.size() << endl;
cout << "队头元素 :" << q.front() << endl;
cout << "队尾元素 :" << q.back() << endl;
cout << "队头元素 :" << q.front() << endl;
cout << "队尾元素 :" << q.back() << endl;
while (!q.empty())
{
cout << q.front() << endl;
q.pop();
}
}
{
cout << q.front() << endl;
q.pop();
}
}
class MyQueue
{
public:
// 入队
void push(int num)
{
s1.push(num);
}
{
public:
// 入队
void push(int num)
{
s1.push(num);
}
// 出队
void pop()
{
if (s2.empty())
{
while (s1.size()>1)
{
s2.push(s1.top());
s1.pop();
}
void pop()
{
if (s2.empty())
{
while (s1.size()>1)
{
s2.push(s1.top());
s1.pop();
}
if (!s1.empty())
s1.pop();
}
else
s2.pop();
}
s1.pop();
}
else
s2.pop();
}
bool empty()
{
if (s1.empty() && s2.empty())
return true;
else
return false;
}
{
if (s1.empty() && s2.empty())
return true;
else
return false;
}
int front()
{
if (s2.empty())
{
while (!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
{
if (s2.empty())
{
while (!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
return s2.top();
}
private:
stack<int> s1;
stack<int> s2;
};
}
private:
stack<int> s1;
stack<int> s2;
};
void func3()
{
MyQueue q;
q.push(8);
q.push(4);
q.push(2);
q.push(19);
{
MyQueue q;
q.push(8);
q.push(4);
q.push(2);
q.push(19);
while (!q.empty())
{
cout << q.front() << endl;
q.pop();
}
}
{
cout << q.front() << endl;
q.pop();
}
}
int main()
{
// func1();
// func2();
func3();
return 0;
}
{
// func1();
// func2();
func3();
return 0;
}
#include <iostream>
#include <list>
#include <queue>
using namespace std;
void printL(list<int> &l)
{
list<int>::iterator it = l.begin();
while (it != l.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}
void func1()
{
list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(2);
l.push_back(2);
l.push_back(2);
l.push_back(2);
l.push_back(2);
l.push_front(-1);
l.push_front(-2);
l.push_back(2);
printL(l);
list<int>::iterator it = l.begin();
it++;
it--;
// 链表迭代器不允许随机访问
// it = it + 1;
l.erase(l.begin());
printL(l);
// 根据值删除元素
l.remove(2);
printL(l);
l.reverse();
printL(l);
}
void func2()
{
// 优先级队列默认是从大到小进行排序的
priority_queue<int, vector<int>, less<int>> pq1; // === >priority_queue<int> pq;
priority_queue<int, vector<int>, greater<int>> pq;
pq.push(9);
pq.push(7);
pq.push(19);
pq.push(17);
pq.push(4);
pq.push(3);
while (!pq.empty())
{
cout << pq.top() << endl;
pq.pop();
}
}
int main()
{
// func1();
func2();
return 0;
}
5.set
#include <iostream>
#include <set>
#include <string>
using namespace std;
void printS(set<int> &s)
{
set<int>::iterator it = s.begin();
while (it != s.end())
{
cout << *it <<" ";
it++;
}
cout << endl;
}
void printS(set<int, greater<int>> &s)
{
set<int, greater<int>>::iterator it = s.begin();
while (it != s.end())
{
cout << *it <<" ";
it++;
}
cout << endl;
}
// 1、set插入、删除
void func1()
{
// set 二叉树 数据是唯一的,不能重复,重复插入数据是无效的
// set中的数据是不允许修改的
set<int> s;
s.insert(3);
s.insert(3);
s.insert(3);
s.insert(3);
s.insert(3);
s.insert(31);
s.insert(13);
s.insert(2);
s.insert(1);
s.insert(54);
s.insert(32);
s.insert(16);
s.insert(11);
cout << s.size() << endl;
printS(s);
s.erase(s.begin());
printS(s);
// 可以直接删除值
s.erase(13);
printS(s);
}
// 2、set的排序
void func2()
{
set<int> s1; // 默认是从小到大排序 set<int,less<int>> s1;
set<int, greater<int>> s;
s.insert(3);
s.insert(31);
s.insert(13);
s.insert(2);
s.insert(1);
s.insert(54);
s.insert(32);
s.insert(16);
s.insert(11);
printS(s);
}
class Student
{
public:
Student(int id, string name)
{
this->id = id;
this->name = name;
}
void show() const
{
// cout <<"id = " << id << ", name = " << name << endl;
printf ("id = %d, name = %s\n", id, name.c_str());
}
int getID() const
{
return id;
}
string getName() const
{
return name;
}
private:
int id;
string name;
};
// 自定义的用于比较两个学生大小的类
// 需要重载 () 运算符 =====> 比较两个学生的大小
class MyCompare
{
public:
bool operator()(const Student &left, const Student &right)
{
// return (left.getID() < right.getID()); // 从小到大进行排序
// return (left.getID() > right.getID());
// return (left.getName() < right.getName());
return (left.getName() > right.getName());
}
};
void printS(set<Student, MyCompare> &s)
{
set<Student, MyCompare>::iterator it = s.begin();
while (it != s.end())
{
it->show();
it++;
}
cout << endl;
}
// 3、自定义类型排序
void func3()
{
Student s3(3, "小孙");
Student s5(5, "小周");
Student s1(1, "小赵");
Student s4(4, "小李");
Student s2(2, "小钱");
Student s7(7, "小郑");
Student s6(6, "小吴");
//MyCompare mc;
//cout << mc(s1, s2) << endl;
// set 数据在插入过程要进行排序 ====> 1、遍历 2、比较大小
// 内置类型 编译器知道如何比较大小
// 对于自定义类型,编译器不知道如何进行大小比较
// =====> 给编译器提供一种比较大小的方式
set<Student, MyCompare> s;
s.insert(s1);
s.insert(s2);
s.insert(s3);
s.insert(s4);
s.insert(s5);
s.insert(s6);
s.insert(s7);
printS(s);
}
// 4、set插入的返回值
void func4()
{
set<int> s;
s.insert(3);
s.insert(31);
// pair<iterator, bool> 是一个对组,将两个数据打包放一块
// 第一个参数 是迭代器,如果插入成功,指向插入元素的迭代器
// 第二个参数 代表插入是否成功,成功返回true, 失败返回false
pair<set<int>::iterator, bool> ret = s.insert(3);
if (ret.second)
cout << "插入成功,插入数据为:" << *(ret.first) << endl;
else
cout << "数据插入失败" << endl;
ret = s.insert(4);
if (ret.second)
cout << "插入成功,插入数据为:" << *(ret.first) << endl;
else
cout << "数据插入失败" << endl;
}
// 5、set的查找
void func5()
{
set<int> s;
s.insert(3);
s.insert(4);
s.insert(2);
s.insert(13);
s.insert(31);
// 返回值是迭代器,如果找到返回找到的元素的迭代器,如果没有,返回 end()
set<int>::iterator it = s.find(3);
if (it != s.end())
cout <<"找到元素: " << *it << endl;
// >= 返回第一个大于等于要查找的元素 的元素迭代器
it = s.lower_bound(2);
if (it != s.end())
cout <<"找到元素: " << *it << endl;
// > 返回第一个大于要查找的元素 的元素迭代器
it = s.upper_bound(2);
if (it != s.end())
cout <<"找到元素: " << *it << endl;
// 第一个返回值:等价于lower_bound >= 返回第一个大于等于要查找的元素 的元素迭代器
// 第二个返回值:等价于upper_bound > 返回第一个大于要查找的元素 的元素迭代器
pair<set<int>::iterator, set<int>::iterator> ret = s.equal_range(3);
if (ret.first != s.end())
cout <<"找到第一个大于等于3的元素: " << *(ret.first) << endl;
if (ret.second != s.end())
cout <<"找到第一个大于3的元素 : " << *(ret.second) << endl;
}
// 6、multiset
void func6()
{
multiset<int> ms;
ms.insert(1);
ms.insert(2);
ms.insert(3);
ms.insert(1);
ms.insert(1);
multiset<int>::iterator it = ms.begin();
while (it != ms.end())
{
cout << *it <<" ";
it++;
}
cout << endl;
cout << ms.count(1) << endl;
}
int main()
{
// func1();
// func2();
// func3();
// func4();
// func5();
func6();
return 0;
}
6.map
#include <iostream>
#include <map>
#include <string>
using namespace std;
void printM(map<int, string> &m)
{
map<int, string>::iterator it = m.begin();
while (it != m.end())
{
cout << "id = " << it->first << ", name = " << it->second << endl;
it++;
}
}
// 1、map数据插入
void func1()
{
// 键值对,是一对数据
// map<int, string>
// 键是 int 类型 ===> id
// 值是 string 类型 ===> name
map<int, string> m;
// 1、通过pair构建对组进行插入
m.insert(pair<int, string>(1, "小明"));
m.insert(pair<int, string>(7, "小张"));
// 2、make_pair 直接插入
m.insert(make_pair(3, "小赵"));
m.insert(make_pair(2, "小钱"));
// 3、通过map的类型进行插入
m.insert(map<int, string>::value_type(6, "小王"));
m.insert(map<int, string>::value_type(4, "小吴"));
// 4、通过 [] 进行插入
// [] 里面装的是键,键可以是任意类型
m[5] = "小孙";
m[8] = "小周";
printM(m);
}
// 2、插入比较
void func2()
{
map<string, int> m;
m.insert(pair<string, int>("小赵", 2));
m.insert(make_pair("小张", 5));
m.insert(map<string, int>::value_type("小吴", 4));
m["小王"] = 7;
// insert方式插入是有返回值的,可以判断插入是否成功
// 如果键已经存在,则不允许插入
pair<map<string, int>::iterator, bool> ret = m.insert(pair<string, int>("小赵", 2));
if (ret.second)
cout << "插入成功" << endl;
else
cout << "插入失败" << endl;
// [] 没有返回值,如果键存在,则用新的值进行替换原来的值
// 如果键不存在,则创建一个新的键并未其赋值
m["小王"] = 8;
}
// 查找:通过键来查找
void func3()
{
map<string, int> m;
m.insert(pair<string, int>("小赵", 2));
m.insert(make_pair("小张", 5));
m.insert(map<string, int>::value_type("小吴", 4));
m["小王"] = 7;
// 如果存在,则返回的指向找到的那个元素的迭代器
// 如果不存在,则返回 end()
map<string, int>::iterator it = m.find("小吴");
if (it != m.end())
cout << "id = " << it->second << ", name = " << it->first << endl;
pair<map<string, int>::iterator, map<string, int>::iterator> ret = m.equal_range("小吴");
if (ret.first != m.end())
cout << "id = " << ret.first->second << ", name = " << ret.first->first << endl;
if (ret.second != m.end())
cout << "id = " << ret.second->second << ", name = " << ret.second ->first << endl;
}
int main()
{
// func1();
// func2();
func3();
return 0;
}
7multimap
#include <iostream>
#include <map>
#include <string>
using namespace std;
class User
{
public:
User(int id, string name, string tel)
{
this->id = id;
this->name = name;
this->tel = tel;
}
void show()
{
printf ("id = %d, name = %s, tel = %s\n", id, name.c_str(), tel.c_str());
}
string getName()
{
return name;
}
private:
int id;
string name;
string tel;
};
class UserManager
{
public:
// group 代表要添加到哪一个分组
void add(int id, string name, string tel, string group)
{
// 创建新用户
User *p = new User(id, name, tel);
// 添加新用户
m.insert(make_pair(group, p));
}
// 查看
void show()
{
multimap<string, User *>::iterator it = m.begin();
while(it != m.end())
{
printf ("分组: %-10s", it->first.c_str());
it->second->show();
it++;
}
}
// 删除
void del(string name)
{
multimap<string, User *>::iterator it = m.begin();
while(it != m.end())
{
if (name == it->second->getName())
{
m.erase(it);
break;
}
it++;
}
}
// 查看分组成员
void showGroup(string group)
{
pair<multimap<string, User *>::iterator, multimap<string, User *>::iterator> ret = m.equal_range(group);
multimap<string, User *>::iterator it1 = ret.first;
multimap<string, User *>::iterator it2 = ret.second;
cout << group << ": " << endl;
while (it1 != it2)
{
it1->second->show();
it1++;
}
}
private:
// 用户需要分组:家庭成员、同学、同事、陌生人、骚扰电话...
// 键:分组
// 值:用户
multimap<string, User *> m;
};
void add(UserManager &um)
{
//int id;
//string name;
//string tel;
//cout << "请输入id:";
//cin>>id;
//cout << "请输入用户名:";
//cin>>name;
//cout << "请输入电话号码:";
//cin>>tel;
um.add(1, "小张", "12412515", "家庭成员");
um.add(7, "小王", "12412515", "同事");
um.add(3, "小吴", "12412515", "同学");
um.add(6, "小赵", "12412515", "好友");
um.add(4, "小李", "12412515", "好友");
um.add(9, "小郑", "12412515", "家庭成员");
um.add(2, "小周", "12412515", "家庭成员");
}
int main()
{
UserManager um;
add(um);
um.show();
um.del("小郑");
cout << "------------------------------" << endl;
um.show();
cout << "------------------------------" << endl;
um.showGroup("家庭成员");
cout << "------------------------------" << endl;
um.showGroup("同事");
return 0;
}