链表
#include <iostream>
#include <string>
#include <list>
#include <functional> //仿函数less和greater头文件
using namespace std;
//list基础数据类型操作
void testList()
{
list<string> strlist;
//尾插入
strlist.push_back("string1");
strlist.push_back("string2");
//头插入
strlist.push_front("string3");
//不删除式遍历
list<string>::iterator iter;
for (iter = strlist.begin(); iter != strlist.end(); iter++)
{
cout << *iter << endl;//string3 string1 string2
}
cout << "是否为空:" << boolalpha << strlist.empty() << endl;//false
cout << "元素个数:" << strlist.size() << endl;//3
//删除式遍历
while (!strlist.empty())
{
cout << strlist.front() << endl; //strlist.back()尾元素
//头部删除
strlist.pop_front(); //strlist.pop_back()删除尾节点
}
cout << "元素个数:" << strlist.size() << endl;//0
//指定位置操作
list<int> inum;
for (int i = 0; i < 3; i++)
{
inum.push_front(i);
}
auto temp = find(inum.begin(), inum.end(), 2);//find返回值是迭代器,未找到返回结束位置
cout << *temp << endl;//2
//在指定位置前面插入
inum.insert(temp, 33);
for (auto v : inum)
{
cout << v <<endl;//33,2,1,0
}
//根据迭代器删除
inum.erase(temp);
for (auto v : inum)
{
cout << v << endl;//33,1,0
}
//其他操作
inum.reverse();//反转链表
inum.sort(greater<int>());//从大到小排序(默认从小到大)greater<int>()仿函数,排序准则
//inum.swap()交换
}
//删除相同的元素
void deletenum()
{
int arr[] = { 1, 2, 2, 2, 2, 2, 3, 4, 5, 6, 66 };
list<int> intlist;
intlist.assign(arr, arr + 11);//批量拷贝
for (list<int>::iterator iter = intlist.begin(); iter != intlist.end();)
{
if (*iter == 2)
{
iter = intlist.erase(iter);
}
else
iter++;
}
for (auto v : intlist)
{
cout << v << "\t";//1 3 4 5 6 66
}
}
//操作自定义类型数据
class Boy
{
public:
Boy(string name, int age,int num) :name(name), age(age),num(num){}
void print()
{
cout << name << "\t" << age << "\t" << num << endl;
}
bool operator==(const string name)const
{
return this->name == name;
}
bool operator<(const Boy& boy)const
{
return this->name < boy.name;
}
string Getname()const
{
return name;
}
int Getage()const
{
return age;
}
protected:
string name;
int age;
int num;
};
bool compareByName(const Boy& boy1, const Boy& boy2)
{
return boy1.Getname() < boy2.Getname();
}
bool compareByAge(const Boy& boy1, const Boy& boy2)
{
return boy1.Getage() < boy2.Getage();
}
void testclass()
{
list<Boy> classlist;
string name;
int age;
int num;
while (1)
{
cout << "input" << endl;
cin >> name >> age >> num;
classlist.push_back(Boy(name, age, num));
while (cin.get() != '\n');
cout << "是否继续输入";
if (cin.get() == 'n')
break;
}
cout << "姓名\t年龄\t代号" << endl;
for (auto v : classlist)
{
v.print();
}
//自定义类型查找
auto result = find(classlist.begin(), classlist.end(), "name1");
classlist.sort(less<Boy>());
//自定义类型的自定义排序(重载)
classlist.sort(compareByAge);
}
int main()
{
testList();
deletenum();
testclass();
return 0;
}
栈
#include <iostream>
#include <string>
#include <stack>
using namespace std;
//栈的基本操作
void testStack()
{
//push(data)插入
//pop()删除
//top()获取栈顶元素
//size()元素个数
//empty()判断是否为空
stack<int> intstack;
for (int i = 0; i < 3; i++)
{
intstack.push(i);
}
while (!intstack.empty())
{
cout << intstack.top() << endl;
intstack.pop();
}
cout << intstack.size() << endl;
}
//转换成进制
void Numtop(int data)
{
stack<int> b;
while (data)
{
b.push(data % 2);
data = data / 2;
}
if (b.size() < 8)
{
for (int i = b.size(); i < 8; i++)
b.push(0);
}
while (!b.empty())
{
cout << b.top() ;
b.pop();
}
cout << endl;
}
队列
#include <iostream>
#include <string>
#include <queue>
#include <deque>
#include <vector>
using namespace std;
//不删除原容器元素遍历
void pop_queue(queue<int> qdata)
{
while (!qdata.empty())
{
cout << qdata.front() << "\t";//获取对头元素
qdata.pop();//出队
}
}
//队列基本操作
void testqueue()
{
queue<int> qdata;
for (int i = 0; i < 3; i++)
{
qdata.push(i);//入队
}
pop_queue(qdata);
cout <<"元素个数:"<< qdata.size() << endl;
}
//头部出队
void pop_front_deque(deque<int> adata)
{
while (!adata.empty())
{
cout << adata.front();
adata.pop_front();
}
cout << endl;
}
//尾部出队
void pop_back_deque(deque<int> adata)
{
while (!adata.empty())
{
cout << adata.back();
adata.pop_back();
}
cout << endl;
}
//双向队列
void testDeque()
{
deque<int> ddata;
for (int i = 0; i < 3; i++)
{
ddata.push_back(i); //尾插法入队
ddata.push_front(i); //头插法入队
}
ddata.push_back(99); //尾插法入队
pop_front_deque(ddata);//21001299
pop_back_deque(ddata);//99210012
}
//优先队列,按照数据优先权出队
/*
template <class _Ty,class _container=vector<_Ty>,class _Pr=less<_Ty>>
class my_priority_queue
{
public:
protected:
};
*/
void pq()
{
priority_queue<int> pqdata1;//默认从大的出队
priority_queue<int, vector<int>> pqdata2;
priority_queue<int, vector<int>,less<int>> pqdata3;
pqdata1.push(11);
pqdata1.push(44);
pqdata1.push(22);
pqdata1.push(33);
while (!pqdata1.empty())
{
cout << pqdata1.top() << "\t";//44,33,22,11
pqdata1.pop();
}
cout << endl;
}
int main()
{
testqueue();
testDeque();
pq();
return 0;
}