STL-set/multiset关联式容器

目录

一、常见接口

1.0 迭代器

1.1 构造函数

1.2 增删查

1.3 查找和统计

二、multiset

2.1 构造

2.2 查找

2.3 删除

2.4 统计


关联式容器是⽤来存储数据的,与序列式容器不同的是,关联式容器逻辑结构通常是⾮线性结构,两个位置有紧密的关联关系,交换⼀下,他的存储结构就被破坏了。STL库中的set底层是基于红黑树实现的。

一、常见接口

详细解释与使用请参见官方网站:set - C++ Reference (cplusplus.com)

1.0 迭代器

迭代器是⼀个双向迭代器

// 正向迭代器
iterator begin();
iterator end();
// 反向迭代器
reverse_iterator rbegin();
reverse_iterator rend();

//遍历操作
void test1()
{
	set<int> s;
	s.insert(5);
	s.insert(2);
	s.insert(7);
	s.insert(3);
	s.insert(4);

	set<int>::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it << " ";
		it++;
	}
}

1.1 构造函数

构造:

  • set<T> st; //默认构造函数:

  • set(const set &st); //拷贝构造函数

  • set (InputIterator first, InputIterator last;//迭代器区间构造

  • set& operator=(const set &st); //重载赋值操作符

  • set (initializer_list<value_type> il); //列表构造

//构造
void test2()
{
	//initializer
	set<string> strset = { "sort", "insert", "add" };
	//拷贝
	set<string> str(strset);
	//迭代器区间
	set<int> s;
	s.insert(5);
	s.insert(2);
	s.insert(7);
	s.insert(3);
	s.insert(4);
	set<int> s1(s.begin(), s.end());
}

1.2 增删查

  • insert(elem); //在容器中插入元素。
  • clear(); //清除所有元素
  • erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
  • erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
  • erase(elem); //删除容器中值为elem的元素。
//删除
void test3()
{
	set<int> s = { 4,2,7,2,8,5,9 };
	for (auto e : s)
	{
		cout << e << " ";
	}
	// 删除最⼩值
	s.erase(s.begin());
	for (auto e : s)
	{
		cout << e << " ";
	}
	cout << endl;
	// 直接删除x
	int x;
	cin >> x;
	int num = s.erase(x);
	if (num == 0)
	{
		cout << x << " 不存在!" << endl;
	}
	for (auto e : s)
	{
		cout << e << " ";
	}
	cout << endl;
	//删除区间
	// 返回 >= 30
	auto itlow = s.lower_bound(3);
	// 返回 > 60
	auto itup = s.upper_bound(6);
	// 删除这段区间的值
	s.erase(itlow, itup);
	for (auto e : s)
	{
		cout << e << " ";
	}
	cout << endl;
}

注意:

删除仍然会造成迭代器失效 

1.3 查找和统计

  • find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
  • count(key); //统计key的元素个数
//查找和统计
void test4()
{
	set<int> s1;
	//插入
	s1.insert(11);
	s1.insert(5);
	s1.insert(2);
	s1.insert(40);

	//查找
	set<int>::iterator pos = s1.find(30);

	if (pos != s1.end())
	{
		cout << "找到了元素 : " << *pos << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}

	//统计
	int num = s1.count(2);
	cout << "num = " << num << endl;
}

 注意

set标准库不支持冗余值进入,count接口是为了与multiset类对称。但是在实际应用中,我们可以用count接口判断某个值是否存在,比起find判断存在来要更简单易用。

void test5()
{
	set<int> s;
	s.insert(5);
	s.insert(2);
	s.insert(7);
	s.insert(3);
	s.insert(4);
	int num = s.count(2);
	if (num)
		cout << "有" << endl;
	else
		cout << "无" << endl;
}

二、multiset

multiset和set的使⽤基本完全类似,主要区别点在于multiset⽀持值冗余。

与set的接口相比,只在以下几个接口有区别

2.1 构造

void test6()
{
	// 相⽐ set 不同的是,multiset 是排序,但是不去重
    multiset<int> ms = { 4,2,7,2,4,8,4,5,4,9 };
	auto it = ms.begin();
	while (it != ms.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

 

2.2 查找

void test6()
{
	// 相⽐ set 不同的是,x 可能会存在多个,find 查找中序的第⼀个
	multiset<int> ms = { 4,2,7,2,4,8,4,5,4,9 };
	int x;
	cin >> x;
	auto pos = ms.find(x);
	while (pos != ms.end() && *pos == x)
	{
		cout << *pos << " ";
		++pos;
	}
	cout << endl;
}

 

2.3 删除

void test6()
{
	// 相⽐ set 不同的是,erase 给值时会删除所有的x
	multiset<int> ms = { 4,2,7,2,4,8,4,5,4,9 };
	int x;
	cin >> x;
	ms.erase(x);
	for (auto e : ms)
	{
		cout << e << " ";
	}
	cout << endl;
}

2.4 统计

void test6()
{
	// 相⽐ set 不同的是,erase 给值时会删除所有的x
	multiset<int> ms = { 4,2,7,2,4,8,4,5,4,9 };
	int x;
	cin >> x;
	cout << ms.count(x) << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值