Day37.C++提高02(STL)

Day37.C++提高02(STL)

01.stack栈队列(先进后出)

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<stack>

void test01()
{
	stack<int> s;
	s.push(10);
	s.push(20);
	s.push(30);
	s.push(40);
	while (s.size())
	{
		cout << "栈顶为:" << s.top() << endl;
		//弹出栈顶元素
		s.pop();
	}
}

int main(void)
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

02.queue队列容器(先进先出)

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<queue>

void test01()
{
	int a;
	queue<int> q;
	q.push(10);
	q.push(20);
	q.push(30);
	q.push(40);

	while (!q.empty())
	{
		cout << "队头元素:" << q.front() << endl;
		cout << "队尾元素:" << q.back() << endl;
		//弹出队头元素
		q.pop();
	}
	
	cout << "q.size():" << q.size() << endl;
}

int main(void)
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

03.list链表容器

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<list>
#include<algorithm>
#include<string>


void printList(list<int>& L)
{
	for (list<int>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}


void test01()
{
	//构造
	cout << "==========================构造============================" << endl;
	list<int> L(10, 10);
	list<int> L2(L.begin(), L.end());

	printList(L);
	printList(L2);
	L.push_back(100);

	for (list<int>::reverse_iterator it = L.rbegin(); it != L.rend(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	cout << "=====================插入删除数据==========================" << endl;

	//插入数据
	list<int> L3;
	L3.push_back(10);
	L3.push_back(20);
	L3.push_back(30);
	L3.push_back(40);
	L3.push_front(20);
	L3.push_front(30);
	L3.push_front(40);

	printList(L3);
	cout << "======================删除两端数据===========================" << endl;
	L3.pop_back();
	L3.pop_front();
	printList(L3);

	L3.insert(L3.begin(), 100000);
	L3.insert(L3.begin(), 10, 100);
	printList(L3);

	L3.insert(L3.begin(), ++L3.begin(), --L3.end());
	printList(L3);

	//remove(elem);删除容器中所有与elem值匹配的元素。
	L3.remove(100);
	printList(L3);
	L3.clear();
	cout << "L3.size: " << L3.size() << endl;
}

void test02()
{
	list<int> L3;
	L3.push_back(10);
	L3.push_back(20);
	L3.push_back(30);
	L3.push_back(40);
	L3.push_front(20);
	L3.push_front(30);
	L3.push_front(40);

	cout << "L3的大小:" << L3.size() << endl;
	if (L3.empty())
	{
		cout << "L3为空" << endl;
	}
	else
	{
		cout << "L3不为空" << endl;
	}

	L3.resize(10, 10);
	printList(L3);
	L3.resize(12);
	printList(L3);
	L3.resize(3);
	printList(L3);

}


/*
list反转排序
reverse();//反转链表,比如list包含1,3,5元素,运行此方法后,list就包含5,3,1元素
sort();//list排序
*/

bool myCompare(int v1, int v2)
{
	return v1 > v2;
}

void test03()
{
	list<int> L;
	L.push_back(10);
	L.push_back(30);
	L.push_back(20);
	L.push_back(40);

	L.reverse();
	printList(L);

	//所有不支持随机访问的迭代器,不可以用系统提供的算法
	//如果不支持用系统提供算法,那么这个类内部会提供
	//sort(L.begin(), L.end());
	L.sort();//从小到大
	printList(L);
	L.sort(myCompare);
	printList(L);

}

//自定义数据类型
class Person
{
public:

	Person(string name, int age, int height)
	{
		this->m_Name = name;
		this->m_Age = age;
		this->m_Height = height;
	}

	//重载 == 让 remove可以删除自定义数据类型
	/*bool operator==(const Person& p)const
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age && this->m_Height == p.m_Height)
		{
			return true;
		}
		return false;
	}*/


public:

	string m_Name;
	int m_Age;
	int m_Height;
};

bool operator==(const Person& p1, const Person& p2)
{
	if (p1.m_Age == p2.m_Age && p1.m_Name == p2.m_Name && p1.m_Height == p2.m_Height)
	{
		return true;
	}
	return false;
}

//Person排序规则
bool personCompare(Person& p1, Person& p2)
{
	if (p1.m_Age == p2.m_Age)
	{
		return p1.m_Height < p2.m_Height;
	}
	return p1.m_Age > p2.m_Age;
}

void test04()
{
	list<Person> L;

	Person p1("德玛西亚", 26, 200);
	Person p2("亚瑟", 18, 185);
	Person p3("得雷福斯", 36, 175);
	Person p4("刘备", 56, 210);
	Person p5("金克丝", 18, 165);
	Person p6("孙尚香", 18, 164);
	Person p7("狗蛋", 26, 183);

	L.push_back(p1);
	L.push_back(p2);
	L.push_back(p3);
	L.push_back(p4);
	L.push_back(p5);
	L.push_back(p6);
	L.push_back(p7);
	
	L.sort(personCompare);

	for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << "  年龄:" << (*it).m_Age << "  身高:" << (*it).m_Height << endl;
	}

	//remove 删除自定义数据类型,需要对 == 进行重载
	L.remove(p6);
	cout << "====================================" << endl;

	for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << "  年龄:" << (*it).m_Age << "  身高:" << (*it).m_Height << endl;
	}

}



int main(void)
{
	//test01();
	//test02();
	//test03();
	test04();
	system("pause");
	return EXIT_SUCCESS;
}

04.set容器

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<set>

void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}


void test01()
{
	set<int> s1;
	//关联式容器 key进行排序,从小到大
	s1.insert(1);
	s1.insert(5);
	s1.insert(3);
	s1.insert(7);
	s1.insert(11);
	s1.insert(9);

	printSet(s1);

	s1.erase(s1.begin());
	printSet(s1);
	s1.erase(3);
	printSet(s1);
}

void test02()
{
	set<int> s1;
	s1.insert(5);
	s1.insert(2);
	s1.insert(4);
	s1.insert(1);
	s1.insert(3);

	//对于set 没有value  key就是value
	//查找键key是否存在,返回该键得元素的迭代器;若不存在,返回set.end();
	set<int>::iterator pos = s1.find(4);
	//判断是否找到
	if (pos != s1.end())
	{
		cout << "找到了,值为:" << *pos << endl;
	}
	else
	{
		cout << "没找到" << endl;
	}
	//count(key),查找键key的元素个数,对于set而言,结果只能是 0或1
	int num = s1.count(1);
	cout << "1 的个数为:" << num << endl;

	//lower_bound(keyElem);//返回第一个key>=keyElem 元素的迭代器。
	set<int>::iterator it1 = s1.lower_bound(2);
	if (it1 != s1.end())
	{
		cout << "s1.lower_bound(2) : " << *it1 << endl;
	}
	else
	{
		cout << "没找到!" << endl;
	}
	//upper_bound(keyElem);//返回第一个key> keyElem 元素的迭代器。
	set<int>::iterator it2 = s1.upper_bound(2);
	if (it2 != s1.end())
	{
		cout << "s1.lower_bound(2) : " << *it2 << endl;
	}
	else
	{
		cout << "没找到!" << endl;
	}
	//equal_range(keyElem);//返回容器中key与keyElem 相等的上下限的两个迭代器。
	//上下限 就是lower_bound  upper_bound
	pair<set<int>::iterator, set<int>::iterator> ret = s1.equal_range(2);
	//获取第一个值
	if (ret.first != s1.end())
	{
		cout << "找到equal_range中lower_bound的值:" << *(ret.first) << endl;
	}
	else
	{
		cout << "没找到" << endl;
	}
	//获取第二个值
	if (ret.second != s1.end())
	{
		cout << "找到equal_range中upper_bound的值:" << *(ret.second) << endl;
	}
	else
	{
		cout << "没找到" << endl;
	}
}

//set容器 不允许插入重复的键值
void test03()
{
	set<int> s1;
	pair<set<int>::iterator, bool> ret = s1.insert(10);

	if (ret.second)
	{
		cout << "第一次插入成功" << endl;
	}
	else
	{
		cout << "第一次插入失败" << endl;
	}
	ret = s1.insert(10);
	if (ret.second)
	{
		cout << "第二次插入成功" << endl;
	}
	else
	{
		cout << "第二次插入失败" << endl;
	}
	printSet(s1);
}

//指定set排序规则
//仿函数
class myCompare
{
public:

	bool operator()(const int& v1, const int& v2)const
	{
		return v1 > v2;
	}

};

//set容器排序
void test04()
{
	//再插入之前就指定排序规则
	set<int, myCompare> s1;

	s1.insert(1);
	s1.insert(3);
	s1.insert(4);
	s1.insert(2);
	s1.insert(6);
	s1.insert(5);

	for (set<int, myCompare>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}


//自定义数据类型
class Person
{
public:

	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;
};

class myPersonCompare
{
public:
	
	bool operator()(const Person& p1, const Person& p2)const
	{
		return p1.m_Age > p2.m_Age;
	}
};

//set存储自定义数据类型
//必须提前指定排序规则
void test05()
{
	set<Person, myPersonCompare> s1;

	Person p1("大娃", 100);
	Person p2("六娃", 50);
	Person p3("三娃", 80);
	Person p4("二娃", 90);

	s1.insert(p1);
	s1.insert(p2);
	s1.insert(p3);
	s1.insert(p4);

	for (set<Person, myPersonCompare>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << "姓名:" << (*it).m_Name << "  年龄:" << (*it).m_Age << endl;
	}
}


int main(void)
{
	//test01();
	//test02();
	//test03();
	//test04();
	test05();

	system("pause");
	return EXIT_SUCCESS;
}

05.pair对组

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<string>

//创建对组
void test01()
{
	//第一种创建
	pair<string, int> p(string("Tom"), 100);
	//取值
	cout << "姓名:" << p.first << endl;
	cout << "年龄:" << p.second << endl;

	//第二种创建
	pair<string, int> p2 = make_pair("Jerry", 200);
	//取值
	cout << "姓名:" << p2.first << endl;
	cout << "年龄:" << p2.second << endl;


}

int main(void)
{
	test01();

	system("pause");
	return EXIT_SUCCESS;
}

06.map容器

概述:

Map特性是,所有元素都会根据元素的键值自动排序。Map所有元素都是pair,
同时拥有实值和键值,pair 的第一元素被视为键值,第二元素被视为实值,
map不允许两个元素有相同的键值	

代码示例:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<map>

void test01()
{
	map<int, int> m;
	//插入值
	//四种方式
	//第一种
	m.insert(pair<int, int>(1, 10));
	//第二种 推荐
	m.insert(make_pair(2, 20));
	//第三种 不推荐
	m.insert(map<int, int>::value_type(3, 30));
	//第四种
	m[4] = 40;

	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << (*it).first << " :" << (*it).second << endl;
	}
}

void printMap(const map<int, int>& m)
{
	for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << (*it).first << " :" << (*it).second << endl;
	}
}

void test02()
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(map<int, int>::value_type(3, 30));
	m[4] = 40;

	m.erase(1);
	printMap(m);

	map<int, int>::iterator pos = m.find(2);
	if (pos != m.end())
	{
		cout << "找到了,key:" << (*pos).first << "  value:" << (*pos).second << endl;
	}
	else
	{
		cout << "没找到" << endl;
	}
	
	//count(keyElem)
	//lower_bound(keyElem)
	//upper_bound(keyElem)
	//equal_range(keyElem)
	//同set容器

	//制定排序规则也类似set容器
}

int main(void)
{
	//test01();
	test02();

	system("pause");
	return EXIT_SUCCESS;
}

07.multimap案例

案例介绍:

- 公司招聘了10个员工,10名员工进入公司之后,需要指派员工在哪个部门工作
- 人员信息有:姓名 年龄 电话 工资等
- 通过multimap 进行信息的插入 保存 显示
- 分部门显示员工信息  显示全部员工的信息

代码实现:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<string>
#include<map>
#include<vector>
#include<ctime>

/*
公司招聘了10个员工,10名员工进入公司之后,需要指派员工在哪个部门工作
人员信息有:姓名 年龄 电话 工资等
通过multimap 进行信息的插入 保存 显示
分部门显示员工信息  显示全部员工的信息
*/

class Person
{
public:

	Person(string name, int age, string tel, int sal)
	{
		this->m_Name = name;
		this->m_Age = age;
		this->m_Tel = tel;
		this->m_Sal = sal;
	}

	string m_Name;
	int m_Age;
	string m_Tel;
	int m_Sal;

};

//创建五名员工
void getPersonVector(vector<Person>& v)
{
	string nameNum = "ABCDEFGHIJ";
	string name;
	int age = 0;
	string tel;
	int sal = 0;
	for (int i = 0; i < 10; ++i)
	{
		name = string("name") += nameNum[i];
		age = rand() % 25 + 25;
		sal = rand() % 20000 + 10000;
		tel = "10000000";

		v.push_back(Person(name, age, tel, sal));
	}

	/*for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << (*it).m_Name << endl;
	}*/
}

//分配部门并存储到 multimap容器中
void personLayMinistry(const vector<Person>& v, multimap<int, Person>& m)
{
	int layM = 0;
	for (vector<Person>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		layM = rand() % 4;

		m.insert(pair<int, Person>(layM, *it));
	}
	/*for (multimap<int, Person>::const_iterator it = m.begin(); it != m.end(); it++)
	{		
		cout << "姓名:" << (*it).second.m_Name << "  年龄:" << (*it).second.m_Age << "  电话:" << (*it).second.m_Tel << "  工资:" << (*it).second.m_Sal << endl;
	}*/

}


//分部门显示员工信息
void showPerson(const multimap<int, Person>& m)
{
	int numL = 10;
	for (multimap<int, Person>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		if (numL != (*it).first)
		{
			numL = (*it).first;
			switch (numL)
			{
			case 0:
				cout << "0部门员工" << endl;
				break;
			case 1:
				cout << "1部门员工" << endl;
				break;
			case 2:
				cout << "2部门员工" << endl;
				break;
			case 3:
				cout << "3部门员工" << endl;
				break;
			}
		}
		
		cout << "姓名:" << (*it).second.m_Name << "  年龄:" << (*it).second.m_Age << "  电话:" << (*it).second.m_Tel << "  工资:" << (*it).second.m_Sal << endl;
	}
}

void test01()
{
	vector<Person> pVector;
	//得到五名员工的信息
	getPersonVector(pVector);

	multimap<int, Person> pMultimap;
	personLayMinistry(pVector, pMultimap);

	//分部门显示员工信息
	showPerson(pMultimap);

}

int main(void)
{
	srand((unsigned int)time(NULL));
	test01();

	system("pause");
	return EXIT_SUCCESS;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值