Set/multiset容器

一、Set构造函数

区别:
set不允许重复元素
multiset允许重复

void printSet(const set<int>& s)
{
	for (set<int>::const_iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	set<int> s1;

	//插入数据,只有inset
	s1.insert(20);
	s1.insert(30);
	s1.insert(10);
	s1.insert(40);

	//遍历容器
	//set容器特点:所有元素插入时候会自动被排序
	//set容器不允许插入重复值
	printSet(s1);

	//拷贝构造
	set<int> s2(s1);
	printSet(s2);

	//赋值
	set<int> s3;
	s3 = s2;
	printSet(s3);


}


int main() {
	test01();
	return 0;
}

二、Set大小和交换

void printSet(const set<int>& s)
{
	for (set<int>::const_iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	set<int> s1;

	//插入数据
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	s1.insert(50);

	printSet(s1);

	if (s1.empty())
	{
		cout << "s1为空" << endl;
	}
	else
	{
		cout << "s1不为空" << endl;
		cout << "s1的大小为:" << s1.size() << endl;
	}

	set<int> s2;

	//插入数据
	s2.insert(100);
	s2.insert(200);
	s2.insert(300);
	s2.insert(400);
	s2.insert(500);

	cout << "交换前:" << endl;
	printSet(s1);
	printSet(s2);
	s1.swap(s2);
	cout << "交换后:" << endl;
	printSet(s1);
	printSet(s2);
}

int main() {
	test01();
	return 0;
}

三、Set插入和删除

void printSet(const set<int>& s)
{
	for (set<int>::const_iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	set<int> s1;

	s1.insert(60);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	s1.insert(50);

	printSet(s1);

	//删除
	s1.erase(s1.begin());
	printSet(s1);

	//删除重载
	s1.erase(30); 
	printSet(s1);

	//清空
	s1.erase(s1.begin(), s1.end());
	//s1.clear();
	printSet(s1);
}

int main() {
	test01();
	return 0;
}

四、Set查找和统计

void test01()
{
	set<int> s1;

	s1.insert(60);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	set<int>::iterator pos = s1.find(30);
	if (pos != s1.end())
	{
		cout << "找到" << *pos << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

	//统计30的个数
	int num = s1.count(300);
	//对于set而言 统计结果 要么是0 要么是1
	cout << "num = " << num << endl;
}

int main() {
	test01();
	return 0;
}

五、Set和Multiset

void test01()
{
	set<int> s1;
	pair<set<int>::iterator, bool> ret = s1.insert(10);

	if (ret.second)
	{
		cout << "第一次插入成功" << endl;
	}
	else
	{
		cout << "第一次插入失败" << endl;
	}

	ret = s1.insert(10);
	if (ret.second)
	{
		cout << "第一次插入成功" << endl;
	}
	else
	{
		cout << "第一次插入失败" << endl;
	}

	multiset<int> ms;
	//允许插入重复值
	ms.insert(10);
	ms.insert(10);

	for (multiset<int>::const_iterator it = ms.begin(); it != ms.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main() {
	test01();
	return 0;
}

六、Pair对组的创建

void test01()
{
	//第一种方式
	pair<string, int>p("Tom", 20);
	cout << "姓名: " << p.first << "年龄:" << p.second << endl;

	//第二种方式
	pair<string, int>p2 = make_pair("Jerry", 30);
	cout << "姓名: " << p2.first << "年龄:" << p2.second << endl;


}

int main() {
	test01();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值