C++容器篇(三)

集合

set/multiset/bitset

set/multiset

#include<iostream>
#include<set>
#include<ctime>
#include<functional>
using namespace std;
/*
	set:集合
	1.数据自带排序
	2.没有重复,数据的唯一性

*/
class MM {
public:
	MM(string name,int age):name(name),age(age){}
	void print() {
		cout << name << " " << age << endl;
	}
	bool operator<(const MM& object)const {
		return this->name < object.name;
	}
protected:
	string name;
	int age;
};
void testData() {
	srand((unsigned int)time(nullptr));
	set<int> setData;
	set<int, less<int>> setData2;//和默认方式一样
	set<int, greater<int>> setData3;//从大到小排序
	int array[10];
	for (int i = 0; i < 10; i++) {
		int temp = rand() % 10;
		array[i] = temp;
		setData.insert(temp);
	}
	for (auto v : array) {
		cout << v << " ";

	}
	cout << endl;
	for (set<int>::iterator iter = setData.begin(); iter != setData.end(); iter++) {
		cout << *iter << " ";

	}
	cout << endl;
}
//多重集合只具有排序功能,不具有去重功能
void testMultiset() {
	multiset<int> mulData;
	for (int i = 0; i < 10; i++) {
		mulData.insert(rand() % 10);

	}
	for (auto v : mulData) {
		cout << v << " ";

	}
	cout << endl;
}
void testUserData() {
	set<MM> mmData;	//less<int>   <
	mmData.insert(MM("name1", 18));
	mmData.insert(MM("name2", 24));
	mmData.insert(MM("name2", 19));
	for (auto v : mmData) {
		v.print();
	}


}
int main() {
	testData();
	testUserData();
	testMultiset();
	return 0;
}

bitset

#include<iostream>
#include<set>
#include<ctime>
#include<functional>
using namespace std;
/*
	set:集合
	1.数据自带排序
	2.没有重复,数据的唯一性

*/
class MM {
public:
	MM(string name,int age):name(name),age(age){}
	void print() {
		cout << name << " " << age << endl;
	}
	bool operator<(const MM& object)const {
		return this->name < object.name;
	}
protected:
	string name;
	int age;
};
void testData() {
	srand((unsigned int)time(nullptr));
	set<int> setData;
	set<int, less<int>> setData2;//和默认方式一样
	set<int, greater<int>> setData3;//从大到小排序
	int array[10];
	for (int i = 0; i < 10; i++) {
		int temp = rand() % 10;
		array[i] = temp;
		setData.insert(temp);
	}
	for (auto v : array) {
		cout << v << " ";

	}
	cout << endl;
	for (set<int>::iterator iter = setData.begin(); iter != setData.end(); iter++) {
		cout << *iter << " ";

	}
	cout << endl;
}
//多重集合只具有排序功能,不具有去重功能
void testMultiset() {
	multiset<int> mulData;
	for (int i = 0; i < 10; i++) {
		mulData.insert(rand() % 10);
		//生成随机字符
		//rand()%26+'A';

	}
	for (auto v : mulData) {
		cout << v << " ";

	}
	cout << endl;
}
void testUserData() {
	set<MM> mmData;	//less<int>   <
	mmData.insert(MM("name1", 18));
	mmData.insert(MM("name2", 24));
	mmData.insert(MM("name2", 19));
	for (auto v : mmData) {
		v.print();
	}


}
int main() {
	testData();
	testUserData();
	testMultiset();
	return 0;
}

映射

map/multimap

#include<map>
#include<iostream>
#include<graphics.h>
using namespace std;
template <class _Ty1, class _Ty2>struct MyPair {
	_Ty1 first;
	_Ty2 second;
	MyPair(_Ty1 first,_Ty2 second):first(first),second(second){}
};
//map中存储的数据是一个数对类型
void testPair() {
	//键值对!
	pair<int, string> pairData(1, "string");
	MyPair<int, string> myPairData(1, "string");
	cout << pairData.first << " " << pairData.second << endl;
}
/*
map
1.自带排序,默认是从小到大
2.数据唯一性
*/
void testMap() {
	map<int, string> mapData;
	//三种插入方式
	//1.insert插入,要把数据先构建成一个pair对象
	mapData.insert(pair<int, string>(1, "string"));
	//2.make_pair构建数对插入
	mapData.insert(make_pair<int, string>(2, "string2"));
	//3.对于单映射有自己独特的插入方式,直接采用数组下标的方式插入
	//-1是键    map[first]=second;
	mapData[-1] = string("string-1");//数组在一定程度上可以说是映射
	mapData[1] = "string1";//等效于插入一个数对
	//相同的键采用覆盖的方式!!!!!

	//遍历:
	for (map<int, string>::iterator iter = mapData.begin(); iter != mapData.end(); iter++) {
		//*iter指的是pair类型
		cout << iter->first << " " << (*iter).second << endl;
	}

	for (auto v :  mapData) {
		//这里v是结构体,所以用.的方式访问
		cout << v.first << " " << v.second << endl;;
	}
	mapData.erase(1);//通过键删除

	map<string, IMAGE*> img;
	img["墙"] = new IMAGE;
	img["门"] = new IMAGE;
}
class MM {
public:
	MM() = default;
	MM(string name,int age):name(name),age(age){}
	void print() const{
		cout << name << " " << age << endl;
	}
	bool operator<(const MM& object)const {
		return this->name < object.name;
	}
protected:
	string name;
	int age;
};
class Boy {
public:
	Boy() = default;
	Boy(string name, int age) :name(name), age(age) {}
	void print() const{
		cout << name << " " << age << endl;
	}
protected:
	string name;
	int age;
};

void tesUserData() {
	map<MM, Boy> mbData;//一定是重载键里面的比较,不是随便写在哪个类都行
	mbData[MM("小芳", 18)] = Boy("小华", 20);
	mbData[MM("小芳", 18)].print();
	mbData[MM("小张", 20)] = Boy("小黄", 19);
	cout << "配对信息:" << endl;
	for (pair<MM, Boy> v : mbData) {//v:pair<MM,Boy>是个数对,v.first是MM对象
		v.first.print();
		v.second.print();
		
	}
}
void testMulMap() {
	//多重映射,没有什么限制,什么样的对应关系都可以插入到映射中
	//因为存在相同的键,所以不能采用下标法插入
	multimap<int, string> mulData;
	mulData.insert(pair<int, string>(1, "string"));
	mulData.insert(pair<int, string>(1, "string1"));
	mulData.insert(pair<int, string>(2, "string"));
	mulData.insert(pair<int, string>(3, "string"));
	mulData.insert(make_pair<int, string>(3, "string"));
	for (auto v : mulData) {
		cout << v.first << " " << v.second << endl;
	}

}



int main() {
	testMap();
	tesUserData();
	testMulMap();
	return 0;
}

列表

list

#include<iostream>
#include<list>
#include<vector>
#include<array>
#include<initializer_list>
using namespace std;
//主要是用在构造函数中

class MM {
public://构造函数是三个参数就只能让对象三个参数
	MM(string string1,string string2,string string3):string1(string1),string2(string2),string3(string3){}
protected:
	string string1;
	string string2;
	string string3;
};
class Boy {
public:
	Boy(const initializer_list<string>& list) {
		for (auto iter = list.begin(); iter != list.end();iter++) {
			cout << *iter << endl;
		}
	}
protected:

};
void print(initializer_list<int> list) {
	for (auto iter = list.begin(); iter != list.end(); iter++) {
		cout << *iter << " ";

	}
	cout << endl;
}
int main() {
	array<int, 3> arr = { 1,2,3 };
	vector<int> vec = { 1,2,3,4,5,6,7 };
	vector<int> vec1 = { 1,2,3,4,5 };
	Boy boy = { "string" };
	print({ 1 });
	print({ 1,2 });
	print({ 1,2,3,4,5,6 });
	return 0;
}

tuple

#include<tuple>
#include<iostream>
using namespace std;          

void testTuple() {
	//把任何类型的一系列数据当作一组处理
	tuple<string, int, int, string> mmInfo = { "mm",18,10001,"guiyang" };
	tuple<double, double, double> mmScore = make_tuple(99.99, 98.99, 97.0);
	tuple<string, string> value = forward_as_tuple("小张", "小美");
	//结构体数组
	tuple<string, int, int, string> array[3];
}
void visiteData() {
	tuple<string, int, int, string> mmInfo = { "mm",18,10001,"guiyang" };
	//get方法,只能传常量,只能一个个来,不能用gfor循环
	cout << get<0>(mmInfo) << "\t";
	cout << get<1>(mmInfo) << "\t";
	cout << get<3>(mmInfo) << "\t";
	cout << endl;
	//tie的方式访问
	string name;
	int age;
	int num;
	string add;
	tie(name, age, num, add) = mmInfo;
	cout << name << " " << age << " " << num << " " << add << endl;
}
void Exoperator() {
	tuple<string, int, int, string> mmInfo = { "mm",18,10001,"guiyang" };
	tuple<double, double, double> mmScore = make_tuple(99.99, 98.99, 97.0);
	tuple< string, int, int, string, double, double, double> result = tuple_cat(mmInfo, mmScore);
	//auto result = tuple_cat(mmInfo, mmScore);
}

int main() {
	visiteData();

	return 0;
}

折叠参数...

..._Ty

可增长模板参数的模板函数

可增长模板参数的类模板 

#include<iostream>
#include<initializer_list>
using namespace std;
//...Args多个类型的参数包
//1.如何通过参数包定义变量:Arg...args(变量名)
//2.如何使用参数包args...
//参数包的展开:1.递归的方式展开,自己做一个参数包的剥离过程;

//2.通过列表的方式进行剥离参数
// 递归终止函数
template<class _Ty>void print(_Ty data) {//剥离到只剩下一个参数的时候调用此递归终止函数
	cout << data << endl;
}
template<class _Ty, class ...Args>void print(_Ty data, Args...args) {//{1,"string",1.1}
	cout << data << "\t";//data=1   {"string",1.1}
	print(args...);		//data="string"   {1.1}
}
//2.采用列表的方式
template<class _Ty>void printData(_Ty data) {
	cout << data << "\t";
}

template<class ...Args>void printArgs(Args... args) {
	initializer_list<int>{(printData(args), 0)...};
	//int array[]= { (printData(args), 0)... };//等效于上面的
	cout << endl;
	//逗号表达式取得是最后一个有效的值
}
int main() {
	print(1, 2, 3, 4, "string", 12.32);
	print(23, "fed");
	printArgs(1, 2, 3, 4, "string", 12.32);
	printArgs(23, "fed");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值