C++STL容器篇(三)

目录

集合

映射

列表

元祖


集合

set/multiset/bitset

#include<set>
#include<iostream>
#include<ctime>
#include<string>
using namespace std;
/*
   set:集合
   数据自带排序性
   数据唯一性
*/
class Mickey {
public:
	Mickey(string name,int age):name(name),age(age){}
	void print() { cout << name << "  " << age << endl; }
	bool operator<(const Mickey& object) const{ return (this->name )< (object.name); }
protected:
	string name;
	int age;
};
void testSet() //
{
	srand((unsigned int)time(nullptr));
	set<int>setData;      //默认方式从小到大,同下
	//set<int,less<int>>setData;  和默认方式一样
	//set<int,greater<int>>setData;  从大到小
	for (int i = 0; i < 10; i++) {
		setData.insert(rand() % 10);
	}
	for (auto v : setData)
		cout << v << "  ";
	cout << endl;
}
void testMickey() {
	set<Mickey>data;
	data.insert(Mickey("name3", 18));
	data.insert(Mickey("name4", 15));
	data.insert(Mickey("name1", 30));
	for (auto v : data)
		v.print();
}
//multiset:多重集合,只具有排序性,不具有去重功能
void testMultiset() {
	multiset<int>data;
	srand((unsigned int)time(nullptr));
	for (int i = 0; i < 10; i++)
	data.insert(rand() % 10);
	for (auto v : data)
		cout << v << "  ";
	cout << endl;
}
int main() {
	testSet();
	testMickey();
	testMultiset();
	return 0;
}
#include<iostream>
#include<bitset>
using namespace std;
void testBitset() {
	//多个二进制位
	bitset<8>data(00111110);
	cout << data << endl;
	data.flip();
	cout << data << endl;
	cout << data.all() << endl;
	cout << data.any() << endl;
	cout << data.size() << endl;
	cout << data.none() << endl;
	bitset<8>data1(8);
	cout << data1<< endl;
}
int main() {

	testBitset();

	return 0;
}

 

映射

map/multiset

#include<iostream>
#include<map>
#include<string>
using namespace std;
/*
map:自带排序性,默认从小到大
具有唯一性
*/
template<class _Ty1,class _Ty2>
class myPair {
public:
	myPair(_Ty1 first,_Ty2 second):first(first),second(second) {}
protected:
	_Ty1 first;
	_Ty2 second;
};
void testMap() {
	map<int ,string>data;
	//insert插入
	data.insert(pair<int, string>(1, "name1"));
	//make_pair构建数对插入
	data.insert(make_pair<int, string>(2, "name2"));
	//单映射可以直接采用数组下标方式插入
	//数组才一定程序下可以当做数对
	//map[first]=second;
	data[4] = string("name4");
	cout << data[4] << endl;//用的时候直接用即可,相比数组来说这个下标没有任何要求
	for (auto iter = data.begin(); iter != data.end(); iter++)
		cout << iter->first << " " << iter->second << "  ";
	cout << endl;
	for (auto v : data)
		cout << v.first << "  " << v.second << "  ";
	cout << endl;
	data.erase(1);//删除函数
	for (auto v : data)
		cout << v.first << "  " << v.second << "  ";
	cout << endl;
}
class Boy {
public:
	Boy() = default;
	Boy(string name,int age):name(name),age(age){}
	void print() const{ cout << name << "  " << age <<"  "; }
	bool operator<(const Boy& boy)const {return  this->name < boy.name; }
protected:
	string name;
	int age;
};
class Girl {
public:
	Girl() = default;
	Girl(string name, int age) :name(name), age(age) {}
	void print()const { cout << name << "  " << age<<"  "; }
protected:
	string name;
	int age;
};
void testDIY() {
	map<Boy, Girl>data;
	data[Boy("小杰", 12)] = Girl("小美", 23);
	data[Boy("小凯", 25)] = Girl("小筱", 28);
	data[Boy("小俊", 16)] = Girl("小芳", 33);
	for (auto v : data)
	{
		v.first.print();
		v.second.print();
		cout << endl;
	}
}
void testMultimap() {
	multimap<int, string>data;
	data.insert(pair<int, string>(12,"小洁" ));
	data.insert(pair<int, string>(12, "小君"));
	data.insert(pair<int, string>(23, "小理"));
	for (auto v : data)
		cout << v.first << "  " << v.second << endl;
}
int main() {
	testMap();
	testDIY();
	testMultimap();
	return 0;
}

列表

initializer_list

#include<iostream>
#include<array>
#include<list>
#include<vector>
#include<initializer_list>
using namespace std;
class Obtain {
public:
Obtain(string a,string b,string c):a(a),b(b),c(c){}
Obtain(const initializer_list<string>& list) {
	for (auto iter = list.begin(); iter != list.end(); iter++)
		cout << *iter << "  ";
	cout << endl;
}
protected:
	string a;
	string b;
	string c;
};
void print(const initializer_list<int>& list) {
	for (auto iter = list.begin(); iter != list.end(); iter++)
		cout << *iter << "  ";
}
int main() {
	array<int, 3>data={1, 2, 3};
	vector<int>data1= {1, 2, 3, 4, 5};
	vector<int>data2= {1, 2, 3, 4};
	Obtain data3={"string1", "string2", "string3"};
	Obtain data4 = { "string1", "string2" };
	Obtain data_4 = { "string1" };
	initializer_list<string>data5 = { "string1","string2","string3" };
	print({ 1 });
	print({ 1,2 });
	print({ 1,2,3 });
	print({ 1,2,3,4 });
	return 0;
}

元祖

tuple

#include<iostream>
#include<tuple>
using namespace std;
void testTuple() {
// 把任何类型的数据当做一组处理
	tuple<string, int, int, string>data = { "小杰",12,10,"1234" };
	tuple<double, double, double>score = { 98,98,89 };
	tuple<string, string>data1 = forward_as_tuple("小张", "小美");
	tuple<string, int, int, string>a[3];
	//get方式访问不能用for循环
	cout << get<0>(data) << "  ";
	cout << get<1>(data) << "  ";
	cout << get<2>(data) << "  ";
	cout << get<3>(data) << "  ";
	cout << endl;
	//tie方式访问
	string name;
	int age;
	int num;
	string tel;
	tie(name, age, num, tel) = data;
	cout << name << "  " << age << "  " << num << "  " << tel << endl;	
}
void EXoperator() {
	tuple<string, int, int, string>data = { "小杰",12,10,"1234" };
	tuple<double, double, double>score = { 98,98,89 };
	auto result = tuple_cat(data, score);//把两组数据链接
	cout << get<0>(result) << "  ";
	cout << get<1>(result) << "  ";
	cout << get<2>(result) << "  ";
	cout << get<3>(result) << "  ";
	cout << get<4>(result) << "  ";
	cout << get<5>(result) << "  ";
	cout << get<6>(result) << "  ";
}
int main() {
	testTuple();
	EXoperator();
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值