STL容器之initializer_list与set

initializer_list

initializer_list创建的对象,初始值可以有很多个,像vector 一样
想多少个,就多少个。

include<iostream>
#include<initializer_list>

using namespace std;

int main()
{ 
	initializer_list<int> date = { 1, 2, 3, 9 }; //列表  想写多少就写多少
	
	system("pause");
	return 0;
}

## 案例一:vector动态数组的简单模拟

可以用作函数的参数(可以传一个,二个,想传多少个就多少个)
同时,注意,这里是常链表,用cons修饰
这里使用了委托构造

#include<iostream>
#include<initializer_list>

using namespace std;

template <class T>
class m_vector
{
public:
	m_vector(int cursize) : cursize(0)
	{
		men = new T[cursize];
	}

	m_vector( const initializer_list<T>& object): m_vector(object.size())//委托构造,初始化
	{
		for (auto& v : object)
		{
			men[cursize++] = v;
		}
	}

	void printDate()
	{
		for (int i = 0; i < cursize; i++)
		{
			cout << men[i];
		}
	}

private:
	T* men;
	int cursize;
};

int main()
{
	m_vector <int> mm = { 1, 6, 8 }; //常数列表用const修饰

	mm.printDate();

	system("pause");
	return 0;
}

案例二(实现n个数的加法)

#include<iostream>
#include<initializer_list>

using namespace std;

int add(initializer_list<int> date)
{
	int cout = 0;

	for (auto& v : date)
	{
		cout += v;
	}

	return cout;

}

int main()
{
	//利用initializer_list 可以求任意数之和
	cout << add({ 1, 3, 4 }) << endl;;

	cout << add({ 2, 4, 8, 9, 0, 9, 8, 6 });

	system("pause");
	return 0;
}

set

set也叫做集合的意思,有以下2个特性
1.有序性:默认的排序是从小到大进行排列
2.唯一性:相同值只保留一个、

单集合

有序性

#include<iostream>
#include<set>

using namespace std;

int main()
{
	set<int> date = { 1, 3, 0 ,8, 6, 4 };  //set容器自动排序 从小到大

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

	system("pasue");
	return 0;
}

在这里插入图片描述

唯一性

#include<iostream>
#include<set>

using namespace std;

int main()
{

	set<int> date = { 9, 9, 7, 8, 0, 6 };

	date.insert(2); //insert()插入函数,插入一个2;

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

	system("pause");
	return 0;
}

在这里插入图片描述

删除元素

#include<iostream>
#include<set>

using namespace std;

int main()
{
	set<int> date = { 1, 8, 9, 6 };

	cout << date.size() << endl;

	date.erase(find(date.begin(), date.end(), 9)); //删除元素,erase(),通常结合find算法加迭代器

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

	system("pause");
	return 0;

}

多重集合

多重集合,只具有排序功能,不具有去重功能

#include<iostream>
#include<set>

using namespace std;

int main()
{
	multiset<int> date = { 1, 3, 4, 5, 6,5 };

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

less与greater

#include
#include

using namespace std;

int main()
{
set<int,less> date = { 1, 2, 4, 3, 9, 5, 6, 6 }; //less从小到大排列

for (auto& v : date)
{
	cout << v;
}
cout << endl;
 
set<int, greater<int>> date1 = { 0, 3, 4, 1, 2, 2 , 3 ,4 ,1 }; //greater 从大到小排列

for (auto& v : date1)
{
	cout << v;
}

}

自定义类型

set处理自定义类型
比较的方法,不推荐使用重载,使用仿函数
同样后面打印的话,使用新式for循环加接口函数较为方便,使用函数重载的话,重载就较为复杂

#include<iostream>
#include<set>
#include<string>

using namespace std;

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

	int getAge() const { return age;}
	string getName() const { return name; }

private:
	int age;
	string name;
};

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

int main()
{
	set<MM, compareName> date;
	date.insert(MM(10, "张三"));
	date.insert(MM(13, "李"));
	date.insert(MM(89, "nininninin"));
	
	for (auto& v : date)
	{
		cout << v.getAge() << " " << v.getName() << endl;;  
	}
	//此处用函数重载,较为麻烦,可以使用接口函数进行访问;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

温柔了岁月.c

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

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

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

打赏作者

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

抵扣说明:

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

余额充值