ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++STL容器(三)

2021-12-20 19:02:17  阅读:122  来源: 互联网

标签:容器 cout insert STL iter C++ pair include muldata


集合

1.数据自带排序

2.数据具有唯一性(单集合)

3.自定义类型数据需要重载<

#include <iostream>
#include <set>
#include <string>
#include <ctime>
#include <bitset>
using namespace std;
//基本操作
void testSet()
{
	srand((unsigned int)time(nullptr));//随机数种子
	set<int> setdata1;
	set<int, less<int>> setdata2;
	for (int i = 0; i < 10; i++)
	{
		setdata1.insert(rand() % 10);
	}
	for (set<int>::iterator iter = setdata1.begin(); iter != setdata1.end(); iter++)
	{
		cout << *iter << endl;
	}
}
//自定义类型操作
class Boy
{
public:
	Boy(string name, int age) :name(name), age(age){}
	bool operator<(const Boy& boy)const
	{
		return this->name < boy.name;
	}
	void print()
	{
		cout << name << "\t" << age << endl;
	}
protected:
	string name;
	int age;
};
void testusedata()
{
	set<Boy> boydata;
	boydata.insert(Boy("name1", 1));
	boydata.insert(Boy("name2", 2));
	boydata.insert(Boy("name1", 3));
	boydata.insert(Boy("name2", 4));
	for (auto v : boydata)
	{
		v.print();
	}
}
//多重集合
void testmultiset()
{
	multiset<int> muldata;
	for (int i = 0; i < 10; i++)
	{
		muldata.insert(rand() % 10);
	}
	for (auto v : muldata)
	{
		cout << v << endl;
	}
}
//bitset
void testbitset()
{
	bitset<8> bsdata(1110000);//多少个二进制位
	cout << bsdata << endl;
	cout<<bsdata.flip() << endl;//反转
}
int main()
{
	testSet();
	testusedata();
	testmultiset();
	testbitset();
	return 0;
}

映射

1.自带排序

2.数据唯一性

3.自定义数据类型重载键的符号(<)

4.多重映射,存在相同的键,不可采用下标

#include <iostream>
#include <map>
#include <string>
using namespace std;
//数对
void testpair()
{
	pair<int, string> pdata(1, "string");
	cout << pdata.first << " " << pdata.second << endl;
}
//单映射
void testmap()
{
	map<int, string> mapdata;
	//1.insert插入
	mapdata.insert(pair<int, string>(1, "string1"));
	//2.make_pair构建数对插入
	mapdata.insert(make_pair<int,string>(1,"string0"));
	//3.单映射,可以直接采用数组下标方式
	mapdata[-1] = string("string-1");
	for (map<int, string>::iterator iter = mapdata.begin(); iter != mapdata.end(); iter++)
	{
		//相同的键采用覆盖的方式
		cout << iter->first << "\t" << iter->second << endl;//-1 string-1     1 string1先插后出
	}
	//删除(删除键值)
	mapdata.erase(1);
}
class A
{
public:
	A() = default;
	A(string name, int age) :name(name), age(age){}
	bool operator<(const A& a)const
	{
		return this->name < a.name;
	}
	void print()const
	{
		cout << name << "\t" << age << endl;
	}
protected:
	string name;
	int age;
};
class B
{
public:
	B(string name, int age) :name(name), age(age){}
	B() = default;
	void print()const
	{
		cout << name << "\t" << age << endl;
	}
protected:
	string name;
	int age;
};
//自定义类型
void testclass()
{
	map<A, B> mapclass;
	mapclass[A("classA1", 1)] = B("classB1", 1);
	mapclass[A("classA1", 1)].print();
	mapclass[A("classA2", 2)] = B("classB2", 2);
	for (auto v : mapclass)
	{
		v.first.print();
		v.second.print();
	}
}
//多重映射
void testmultimap()
{
	multimap<int, string> muldata;
	muldata.insert(pair<int, string>(1, "string1"));
	muldata.insert(pair<int, string>(1, "string2"));
	muldata.insert(pair<int, string>(2, "string1"));
	muldata.insert(pair<int, string>(2, "string2"));
	muldata.insert(pair<int, string>(3, "string1"));
	for (auto v : muldata)
	{
		cout << v.first << "\t" << v.second << endl;
	}
}
int main()
{
	testmap();
	testclass();
	testmultimap();
	return 0;
}

列表

#include <iostream>
#include <array>
#include <vector>
#include <list>
#include <initializer_list>
using namespace std;
class A
{
public:
	A(int a1, int a2, int a3, int a4) :a1(a1), a2(a2), a3(a3), a4(a4){}
	A(const initializer_list<int> inilist)
	{
		for (auto iter = inilist.begin(); iter != inilist.end(); iter++)
		{
			cout << *iter << "\t";
		}
		cout << endl;
	}
protected:
	int a1;
	int a2;
	int a3;
	int a4;
};
void print(const initializer_list<int> inilist)
{
	for (auto iter = inilist.begin(); iter != inilist.end(); iter++)
	{
		cout << *iter << "\t";
	}
	cout << endl;
}
int main()
{
	array<int, 3> arrdata = { 1, 2, 3 };
	vector<int> vecdata = { 1, 2, 3, 4, 5, 6 };
	A a = { 1, 2, 3, 4 };
	A b = { 1, 2, 3 };
	print({ 1, 2, 3, 4, 5 });
	return 0;
}

元组

1.整合数据为一组

#include <iostream>
#include <tuple>
#include <string>
using namespace std;
//元组
int main()
{
	//初始化
	tuple<string, int, int, string> boy = make_tuple("name", 1, 1, "111");
	tuple<string, string> name = forward_as_tuple("name1", "name2");
	tuple<string, int, int, string> boy1[2];
	//查看
	//1.get 不可用for
	cout << get<0>(boy) << "\t";
	cout << get<1>(boy) << "\t";
	cout << get<2>(boy) << "\t";
	cout << get<3>(boy) << "\t" << endl;
	//2.tie
	string boyname;
	int age;
	int num;
	string tel;
	tie(boyname, age, num, tel) = boy;
	cout << boyname << "\t" << age << "\t" << num << "\t" << tel << endl;
	//链接
	auto result = tuple_cat(boy, name);
	return 0;
}

标签:容器,cout,insert,STL,iter,C++,pair,include,muldata
来源: https://blog.csdn.net/ProgramAlcohol/article/details/122029031

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有