双向链表
- list
#include <list>
#include <iostream>
#include <string>
#include <functional> //less和greator头文件
using namespace std;
list<int>::iterator myFind(list<int>& iNum, int data)
{
for (list<int>::iterator iter = iNum.begin(); iter != iNum.end(); iter++)
{
if (*iter == data)
{
return iter;
}
}
return iNum.end();
}
//基本操作: 操作基本数据类型
void testList()
{
list<int> iNum;
list<string> strNum;
//插入
strNum.push_back("string1"); //尾插发
strNum.push_back("string2");
strNum.push_front("string3");
//string3 string1 string2
//遍历
//不删除方式遍历
list<string>::iterator iter;
for (iter = strNum.begin(); iter != strNum.end(); iter++)
{
cout << *iter << " ";
}
cout << endl;
cout << "是否为空:" << boolalpha << !strNum.empty() << endl;
cout << "元素个数:" << strNum.size() << endl;
//删除方式遍历
//string3 string1 string2
while (!strNum.empty())
{
cout << strNum.front() << " "; //back()
strNum.pop_front(); //头部删除 pop_front();
}
cout << endl;
cout << "元素个数:" << strNum.size() << endl;
//指定位置操作
//iterator find(iterator begin,iterator end,data);
for (int i = 0; i < 3; i++)
{
iNum.push_back(i);
}
auto result = find(iNum.begin(), iNum.end(), 2);
//没找到返回是end结束的位置
if (result == iNum.end())
{
cout << "未找到指定位置" << endl;
} //insert
iNum.insert(result, 100);
for (auto v : iNum)
{
cout << v << "\t";
}
cout << endl;
//删除函数
iNum.erase(result);
for (auto v : iNum)
{
cout << v << "\t";
}
cout << endl;
//其他操作
iNum.reverse(); //反转链表
for (auto v : iNum)
{
cout << v << "\t";
}
cout << endl;
iNum.sort(less<int>()); //排序
for (auto v : iNum)
{
cout << v << "\t";
}
cout << endl;
}
void testDelete()
{
int array[4] = { 1, 2, 2, 3 };
list<int> data;
data.assign(array, array + 4);
//相同元素的删除
for (list<int>::iterator iter = data.begin(); iter != data.end();)
{
if (*iter == 2)
{
iter = data.erase(iter);
}
else
{
iter++;
}
}
for (auto v : data)
{
cout << v << "\t";
}
cout << endl;
}
//操作自定义类型数据
class MM
{
public:
MM(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 MM& object) const
{
return this->name < object.name;
}
string getName() const
{
return name;
}
int getAge() const
{
return age;
}
protected:
string name;
int age;
int num;
};
//比较的多样化
bool compareByName(const MM& object1, const MM& object2)
{
return object1.getName() < object2.getName();
}
bool compareByAge(const MM& object1, const MM& object2)
{
return object1.getAge() < object2.getAge();
}
void testUserData()
{
list<MM> mmData;
string name;
int age;
int num;
while (1)
{
cout << "input MM:" << endl;
cin >> name >> age >> num;
mmData.push_back(MM(name, age, num));
cout << "是否继续输入?" << endl;
while (cin.get() != '\n');
if (cin.get() == 'n')
break;
}
cout << "姓名\t年龄\t编号" << endl;
for (MM v : mmData)
{
v.print();
}
//二进制“==”: 没有找到接受“MM”类型的左操作数的运算符(或没有可接受的转换)
auto result = find(mmData.begin(), mmData.end(), string("name1"));
//list<MM>::iterator result= find(mmData.begin(), mmData.end(), string("name1"));
list<list<MM>::iterator> resultList;
resultList.push_back(result);
//重载方式
mmData.sort(less<MM>()); //重载<
//mmData.sort(greater<MM>()); //重载>
//不采用重载方式,需要自己写比较准则
mmData.sort(compareByName);
mmData.sort(compareByAge);
}
int main()
{
//testList();
//testDelete();
testUserData();
return 0;
}
结果
string3string1string2
是否为空:true
元素个数:3
string3string1string2
元素个数:0
栈
- stack
#include <stack>
#include <iostream>
#include <string>
using namespace std;
void testStack()
{
//穿脱原则
//FILO
// 1 2 3
//3 2 1
//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() << "\t";
intStack.pop();
}
cout << endl;
}
void NumTobinary(int data)
{
stack<int> bin;
while (data)
{
bin.push(data % 2);
data = data / 2;
}
if (bin.size() < 8)
{
for (int i = bin.size(); i < 8; i++)
{
bin.push(0);
}
}
while (!bin.empty())
{
cout << bin.top();
bin.pop();
}
cout << endl;
}
int main()
{
//testStack();
NumTobinary(7);
return 0;
}
结果
00000111
队列
- queue/deque/priority_queue
双向队列
#include <deque>
#include <iostream>
#include <string>
using namespace std;
//从头出来
void pop_front_dequeue(deque<int> dData)
{
while (!dData.empty())
{
cout << dData.front()<<" ";
dData.pop_front();
}
cout << endl;
}
//从尾出来
void pop_back_dequeue(deque<int> dData)
{
while (!dData.empty())
{
cout << dData.back()<<" ";
dData.pop_back();
}
cout << endl;
}
void testDeque()
{
deque<int> deData;
for (int i = 0; i < 3; i++)
{
deData.push_back(i);//尾插法入队
deData.push_front(i);//头插法入队
}
deData.push_back(999);
//0 0
//1 0 0 1
//2 1 0 0 1 2
pop_front_dequeue(deData);
pop_back_dequeue(deData);
}
int main()
{
testDeque();
return 0;
}
结果输出
2 1 0 0 1 2 999
999 2 1 0 0 1 2
普通队列
#include <queue>
#include <iostream>
#include <string>
using namespace std;
void pop_queue(queue<int> qData)
{
while (!qData.empty())
{
cout << qData.front() << " ";//获取对头元素
qData.pop();//出队
}
cout << endl;
}
void testQueue()
{
queue<int> qData;
for (int i = 0; i < 3; i++)
{
qData.push(i);//入队
}
while (!qData.empty())
{
cout << qData.front() << " ";//获取对头元素
qData.pop();//出队
}
cout << endl;
cout << qData.size() << endl;
}
int main()
{
testQueue();
return 0;
}
0 1 2
0
优先队列
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
/*
*/
template <class _Ty, class _Container = vector<_Ty>, class _Pr = less<_Ty>>
class my_priority_queue
{
public:
protected:
};
void testCreatePriorityQueue()
{
//默认的方式,一级下面三种 都一样,大的先出队
priority_queue<int> pqData;
priority_queue<int, vector<int>> pqData2; //默认排序准则
priority_queue<int, vector<int>, less<int>> pqData3; //所有参数都完整
//优先队列,是按照数据的优先权出队,VIP服务
pqData.push(12);
pqData.push(0);
pqData.push(34);
while (!pqData.empty())
{
cout << pqData.top() << " ";
pqData.pop(); //出队
}
cout << endl;
//贪心算法
priority_queue<int, vector<int>, less<int>> pqDataG;
pqDataG.push(12);
pqDataG.push(0);
pqDataG.push(34);
while (!pqDataG.empty())
{
cout << pqDataG.top() << " ";
pqDataG.pop(); //出队
}
cout << endl;
}
int main()
{
testCreatePriorityQueue();
return 0;
}
0 1 2
0