#include<iostream>
using namespace std;
#include"algorithm"
#include"queue"
void main61()
{
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
cout << " 对头元素:" << q.front() << endl;
cout << "队列的大小" << q.size() << endl;
while (!q.empty())
{
cout << q.front() << " ";
q.pop();
}
}
class Teaher
{
public:
int age;
char name[32];
public:
void printT()
{
cout << "age:" << age << endl;
}
};
void main62()
{
Teaher t1, t2, t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;
queue<Teaher> q;
q.push(t1);
q.push(t2);
q.push(t3);
while (!q.empty())
{
Teaher tmp = q.front();
tmp.printT();
q.pop();
}
}
void main63()
{
Teaher t1, t2, t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;
queue<Teaher*> q;
q.push(&t1);
q.push(&t2);
q.push(&t3);
while (!q.empty())
{
Teaher *tmp = q.front();
tmp->printT();
q.pop();
}
}
void main()
{
main63();
cout << "hello" << endl;
system("pause");
return;
}
/*
stack是堆栈容器 是一种"先进后出"的容器
stack 是简单装饰deque容器而成为另外的容易
*/
void main51()
{
stack<int> s;
//入栈
for (int i = 0; i < 10; i++)
{
s.push(i + 1);
}
cout << "栈的大小" << s.size() << endl;
//出栈
while (!s.empty())
{
int tmp = s.top();//获取栈顶元素
cout << tmp << " ";
s.pop();//弹出栈顶元素
}
}
class Teacher
{
public:
int age;
char name[32];
public:
void printT()
{
cout << "age:" << age << endl;
}
};
void main52()
{
Teacher t1, t2, t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;
stack<Teacher> s;
s.push(t1);
s.push(t2);
s.push(t3);
while (!s.empty())
{
Teacher tmp = s.top();
tmp.printT();
s.pop();
}
}
void main53()
{
Teacher t1, t2, t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;
stack<Teacher*> s;
s.push(&t1);
s.push(&t2);
s.push(&t3);
while (!s.empty())
{
Teacher *p = s.top();
p->printT();
s.pop();
}
}
void main()
{
main52();
cout << "hello" << endl;
system("pause");
return;
}
void main71()
{
list < int> l;
cout << "list 大小" << l.size() << endl;
for (int i = 0; i < 10; i++)
{
l.push_back(i);
}
cout << "list大小" << l.size() << endl;
list<int>::iterator it = l.begin();
while (it != l.end())
{
cout << *it << " ";
it++;
}
//
it =l.begin();
it++;
//it=it+5;//err不可以随机存取元素
l.insert(it, 100);
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
//在3号位置插入数据,就是让新数据占据3号位置,原来的3号位置变成4号位置
}
//删除 左闭右开
void main72()
{
list < int> l;
cout << "list 大小" << l.size() << endl;
for (int i = 0; i < 10; i++)
{
l.push_back(i);
}
cout << "list大小" << l.size() << endl;
list<int>::iterator it1 = l.begin();
list<int>::iterator it2 = l.begin();
it2++;
it2++;
it2++;
l.erase(it1, it2);
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void main()
{
main72();
cout << "hello" << endl;
system("pause");
return;
}
void main81()
{
priority_queue<int> p1;//默认情况下 是最大值优先级队列
priority_queue<int, vector<int>, less<int>>p2;
priority_queue<int, vector<int>, greater<int>>p3;
p1.push(33);
p1.push(11);
p1.push(55);
p1.push(22);
cout << "对头元素:" << p1.top() << endl;
cout << "队列的大小" << p1.size() << endl;
while (p1.size() > 0)
{
cout << p1.top() << " ";
p1.pop();
}
cout << "测试最小值优先队列" << endl;
p3.push(33);
p3.push(11);
p3.push(55);
p3.push(22);
cout << "最小值优先级队列 队头元素:" << p3.top() << endl;
cout << "最小值优先级队列 队列大小:" << p3.size() << endl;
while (p3.size() > 0)
{
cout << p3.top() << " ";
p3.pop();
}
}
void main()
{
//main72();
cout << "hello" << endl;
system("pause");
return;
}
/*
set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列
元素插入过程是按排序规则插入,所以不能指定插入位置
set是红黑树变体 在插入和删除上比vector快
set不可以直接存取元素
set支持唯一键值,每个元素只能出现一次
不可以直接修改元素值,因为是自动排序的,如果需要修改一个元素值,必须先删除原有的元素,在插入新的元素
*/
void main91()
{
set<int> setl;
for (int i = 0; i < 5; i++)
{
int tmp = rand();
setl.insert(tmp);
}
setl.insert(100);
setl.insert(100);
setl.insert(100);
for (set<int>::iterator it = setl.begin(); it != setl.end(); it++)
{
cout << *it << " ";
}
while (!setl.empty())
{
set<int>::iterator it = setl.begin();
cout << *it << endl;
setl.erase(setl.begin());
}
}
void main92()
{
set<int> setl;
set<int, less<int>>set2;
set<int, greater<int>>set3;
for (int i = 0; i < 5; i++)
{
int tmp = rand();
set3.insert(tmp);
}
for (set<int>::iterator it = set3.begin(); it != set3.end(); it++)
{
cout << *it << " ";
}
}
//对于复杂的数据类型 怎么排序?
class Student
{
public:
Student(char *name, int age)
{
strcpy(this->name, name);
this->age = age;
}
public:
char name[64];
int age;
};
struct FuncStudent
{
bool operator()(const Student &left,const Student &right)
{
if (left.age < right.age)
{
return true;
}
else
{
return false;
}
}
};
void main93()
{
Student s1("s1", 31);
Student s2("s2", 22);
Student s3("s3", 44);
Student s4("s4", 11);
set<Student, FuncStudent> setl;
setl.insert(s1);
setl.insert(s2);
setl.insert(s3);
setl.insert(s4);
for (set<Student, FuncStudent>::iterator it = setl.begin(); it != setl.end(); it++)
{
cout << it->age << "\t" << it->name << endl;
}
}
void main94()//怎么测试插入成功还是失败
{
Student s1("s1", 31);
Student s2("s2", 22);
Student s3("s3", 44);
Student s4("s4", 11);
Student s5("s5", 31);
set<Student, FuncStudent> setl;
pair<set<Student, FuncStudent>::iterator, bool>pairl = setl.insert(s1);
if (pairl.second == true)
{
cout << "插入成功" << endl;
}
else
{
cout << "插入失败" << endl;
}
setl.insert(s2);
pair<set<Student, FuncStudent>::iterator, bool>pair5 = setl.insert(s5);
if (pairl.second == true)
{
cout << "插入成功" << endl;
}
else
{
cout << "插入失败" << endl;
}
for (set<Student, FuncStudent>::iterator it = setl.begin(); it != setl.end(); it++)
{
cout << it->age << "\t" << it->name << endl;
}
}
void main95()
{
set<int> setl;
for (int i = 0; i < 10; i++)
{
setl.insert(i+1);
}
for (set<int>::iterator it = setl.begin(); it != setl.end(); it++)
{
cout << *it << endl;
}
cout << endl;
set<int>::iterator it0 = setl.find(5);
cout << "it0" << *it0 << endl;
int numl = setl.count(5);
cout << "num1:" << numl << endl;
set<int>::iterator it1=setl.lower_bound(5);//>=5的迭代器位置
cout << "itl" << *it1 << endl;
set<int>::iterator it2 = setl.lower_bound(5);//>5的迭代器位置
cout << "it2" << *it2 << endl;
pair<set<int>::iterator, set<int>::iterator> mypair = setl.equal_range(5);
set<int>::iterator it3 = mypair.first;
set<int>::iterator it5 = mypair.second;
}