STL容器篇之map与tuple

map

map叫做映射,y = x, 所谓的映射就是一种对应的关系,数组是下标对应数据的一种关系

注意:
1.map存储的数据是数对类型:pair类型(包含,first,second)
2.有序性,按照first从小到大排列
3.唯一性:first唯一

单映射

#include<iostream>
#include<string>
#include<map>

using namespace std;

int main()
{
	//包含2个数据类型,可以一样,也可以不一样

	map<int, string> date;
	pair<int, string>  tmp = {1, "nim"}; //pair其实也相当于一个模板
	
	cout << tmp.first << tmp.second << endl;

	date[1] = string("NI");

	date.insert(tmp);

	date[-100] = string("ni"); //这也跟数组下标不太一样,只要满足first类型,想怎么写就怎么写、

	date.insert(tmp);

	date.insert(pair<int, string>(1, "无名对象"));
	date.insert(make_pair<int, string>(1, "第三方函数"));

	date.insert(pair<int, string>(1, "重复运算"));

	for (auto& v : date)
	{
		cout << v.first << v.second << endl;  //新版for循环访问
	}

	for (map<int, string> ::iterator it = date.begin(); it != date.end(); it++)
	{
		cout << it->first << it->second << endl; //迭代器处理
	}

	system("pause");
	return 0;

}

多重映射

不支持下标法插入,存在二义性
有序性

同样map也有第三个参数,less和great分别表示从小到大,从大到小.

#include<iostream>
#include<string>
#include<map>

using namespace std;

int main()
{
	multimap<int, string> sdate;
	sdate.insert(pair<int, string>(1, "ni"));

	for (auto& v : sdate)
	{
		cout << v.first << v.second;
	}

	//greater 从大到小
	multimap<string, string, greater<string>> mdate;
	mdate.insert(make_pair<string, string>("ni", "niini"));
	mdate.insert(pair<string, string>("m", "mmmmmmmmmmmmmm"));

	for (auto& v : mdate)
	{
		cout << v.first << v.second;
	}
		
	system("pasue");
	return 0;
}

操作自定义类型

比较的方法,还是跟之前一样,写一个仿函数
打印的方法:用重载的方法,或者用接口函数去访问

#include<iostream>
#include<map>
#include<string>

using namespace std;

class MM
{
public:
	MM(int age, string name) : age(age), name(name) {}



	friend ostream& operator << (ostream& out, const MM & object) //加const 修饰
	{
		out << object.age << object.name << endl;
		return out;
	}

private:
	int age;
	string name;
};

class mm
{
public:
	mm(){}
	mm(int math, int Chinses): math(math), Chinese(Chinses) {}

	friend ostream& operator << (ostream& out,const mm& object1)  //加const修饰
	{
		out << object1.math << object1.Chinese << endl;
		return out;
	}
private:
	int math;
	int Chinese;
};

class compare
{
public: 
	bool operator ()(const MM& object1, const MM& object2) const
	{
		return object1.getAge() < object2. getAge();
	}

};

int main()
{

	map<MM, mm, compare> date;

	date[MM(1, "ni")] = mm(10, 100);
	date[MM(3, "nininini")] = mm(78, 67);

	for (auto& v : date)
	{
		cout << v.first << v.second;
	}

	system("pause");
	return 0;
}

tuple

tuple叫做元组,知道如何使用即可,低层使用的折叠参数可变模板

创建对象和初始化

1.forward_as_tuple
2.make_tuple
3.直接对象(进行初始化)

#include<iostream>
#include<tuple>
#include<string>

using namespace std;

int main()
{
	tuple<int, string, int, string> date1 = {1, "2", 2, "nini"};
	tuple<int, string> mm = { 1, "2" };


	tuple<int, string> make_date(1, "ni");
	tuple<string, string> forward_as_tuple("ni", "ninin");


	system("pause");
	return 0;
}

访问

1.使用tie
2.使用get<>(), get<参数>,
注意:这个参数要跟常量,不能用变量,常数变量也不行

#include<iostream>
#include<tuple>
#include<string>

using namespace std;

int main()
{
	tuple<int, string, int> date = { 9, "ni", 9 };

	//用get<1>函数访问

	cout << get<0>(date) << get<1>(date) << get<2>(date) << endl;

	//tie访问
	int age;
	string name;
	int age1;

	tie(age, name, age1) = date;

	cout << age << name << age1;

	cout << endl;
	//忽略方式访问

	tie(age, ignore,ignore) = date;
	cout << age;

	system("pause");
	return 0;
}

tuple的其他操作

使用tuple_cat将2个tuple容器连接起来

#include<iostream>
#include<tuple>
#include<string>

using namespace std;

int main()
{
	tuple<int, string> date1 = { 1, "nu" };
	tuple<int, string> date2 = { 2, "ninini" };

	//tuple_cat
	auto date3 = tuple_cat(date1, date2); //将2个tuple来连接起来

	int number1;
	int number2;
	string name1;
	string name2;

	tie(number1, name1, number2, name2) = date3;

	cout << number1 << name1 << number2 << name2 << endl;

	system("pause");
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温柔了岁月.c

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值