标准模板库(STL)之 set 用法【初级】

文档声明:
以下资料均属于本人在学习过程中产出的学习笔记,如果错误或者遗漏之处,请多多指正。并且该文档在后期会随着学习的深入不断补充完善。


资料仅供学习交流使用。
作者:Aliven888

1、简述

定义:
set是一种二进制搜索树,是按照特定顺序存储唯一元素的一个集合。

特点:
1、在 set 中,每一个元素都是唯一的;
2、在 set 中,元素的值是不能被修改(元素始终为const)的,但可以将新的元素插入容器中 或 将旧的元素从容器中删除。

2、接口函数

下面是SDK官网的接口,以及其功能简述。

2.1、Iterators(迭代器):

名称描述
beginReturn iterator to beginning (public member function )
endReturn iterator to end (public member function )
rbeginReturn reverse iterator to reverse beginning (public member function )
rendReturn reverse iterator to reverse end (public member function )
cbeginReturn const_iterator to beginning (public member function )
cendReturn const_iterator to end (public member function )
crbeginReturn const_reverse_iterator to reverse beginning (public member function )
crendReturn const_reverse_iterator to reverse end (public member function )

2.2、Capacity(容量):

名称描述
emptyTest whether container is empty (public member function )
sizeReturn container size (public member function )
max_sizeReturn maximum size (public member function )

2.3、Modifiers(修改):

名称描述
insertInsert element (public member function )
eraseErase elements (public member function )
swapSwap content (public member function )
clearClear content (public member function )
emplaceConstruct and insert element (public member function )
emplace_hintConstruct and insert element with hint (public member function )

2.4、Operations(操作):

名称描述
findGet iterator to element (public member function )
countCount elements with a specific value (public member function )
lower_boundReturn iterator to lower bound (public member function )
upper_boundReturn iterator to upper bound (public member function )
equal_rangeGet range of equal elements (public member function )

3、接口函数使用案例

接下来通过函数实例介绍下每个接口函数是如何使用,以及使用的过程中有哪些注意事项。
依赖的头文件:

   #include <set>

首先定义一个Vector对象,并附上初始值:

   std::set<int> m_setValue;
   
	m_setValue.insert(1);
	m_setValue.insert(2);
	m_setValue.insert(3);

3.1、Iterators(迭代器)

	
	//返回一个迭代器,指向容器的地址起始位置元素
	std::set<int>::iterator itBegin = m_setValue.begin();
	//std::set<int>::const_iterator itcBegin = m_setValue.cbegin();  //const 静态的

	//返回一个迭代器,指向容器中的past-the-end元素(最后一个元素的下一个位置)。
	std::set<int>::iterator itEnd = m_setValue.end();
	//std::set<int>::const_iterator itcEnd = m_setValue.cend();  //const 静态的

	//返回指向容器中最后一个元素的反向迭代器(即,它的反向开始)。
	std::set<int>::reverse_iterator itrBegin = m_setValue.rbegin();
	//std::set<int>::const_reverse_iterator itcrBegin = m_setValue.crbegin();  //const 静态的

	//返回一个反向迭代器,该反向迭代器指向set容器中第一个元素之前的理论元素(被视为其反向端)。
	std::set<int>::reverse_iterator itrEnd = m_setValue.rend();
	//std::set<int>::const_reverse_iterator itcrEnd = m_setValue.crend();   //const 静态的
	

3.2、Capacity(容量)

3.2.1 empty()

功能:
判断容器是否为空

返回值:
返回设置的容器是否为空(即其大小是否为0) 为空返回 true, 否则返回 false

代码演示:

	if (!m_setValue.empty())
	{
		qDebug("m_setValue is not empty.");
	}
3.2.2 size()

功能:
获取容器中当前元素的个数

返回值:
返回容器中的当前拥有的元素个数

代码演示:

	int iSize = m_setValue.size();
	qDebug("m_setValue size is = [%d]", iSize);
3.2.3 max_size()

功能:
获取容器最大所能容纳的元素个数。但是由于已知的系统或库实现限制,这是容器可以达到的最大潜在大小;绝不能保证容器能够达到该大小,有可能在达到该大小之前,它仍然存在无法分配存储的情况。

返回值:
返回设置的容器可以容纳的最大元素数。

代码演示:

	int iMax_size = m_setValue.max_size();
	qDebug("m_setValue max_size is = [%d]", iMax_size);

输出结果

STL set show…
m_setValue is not empty.
m_setValue size is = [3]
m_setValue max_size is = [214748364]

3.3、Operations(操作)

3.3.1 find(val)

功能:
在容器中搜索与val等效的元素

返回值:
如果找到了val元素,则返回一个指向val元素的迭代器;否则返回指向容器set :: end的迭代器。

代码演示:

	std::set<int>::iterator it = m_setValue.find(2);
	if (m_setValue.end() != it)
	{
		qDebug("m_setValue.find(2) value is = [%d]", *it);
	}
3.3.2 count(val)

功能:
在容器中搜索与val等效的元素的个数。

返回值:
因为set中的元素不能重复,如果容器包含等于val的元素,那么绝对是唯一的,所以找到的话会返回1,否则返回0。
该函数可以用于判断某个元素在set中是否存在,功能类似于find()函数

代码演示:

	if (0 == m_setValue.count(2))
	{
		qDebug("m_setValue Not Find Value 2");
	}
3.3.3 lower_bound(val)

功能:
官网给出的解释:
An iterator to the the first element in the container which is not considered to go before val, or set::end if all elements are considered to go before val. Member types iterator and const_iterator are bidirectional iterator types pointing to elements.

返回值:
但是通过我的演示发现,因为set会自动排序,所以lower_bound(val)返回的迭代器通常会指向val,哪怕 val 是最后一个元素值,其返回的迭代器依旧是指向 val, 而不是 std::set::end。

代码演示:

   //例如:当前 m_setValue[1,2,3]  此时 itLow3 指向 3 的迭代器,而不是 std::set::end.
   std::set<int>::iterator itLow3 = m_setValue.lower_bound(3);
3.3.4 upper_bound(val)

功能:
官网给出的解释:
An iterator to the the first element in the container which is considered to go after val, or set::end if no elements are considered to go after val. Member types iterator and const_iterator are bidirectional iterator types pointing to elements.

返回值:
但是通过我的演示发现,因为set会自动排序,所以upper_bound(val)返回的迭代器通常会指向val下一个位置, 如果 val 是最后一个元素值,其返回的迭代器指向 std::set::end。

代码演示:

	//例如:当前 m_setValue[1,2,3,4,5,6,7]  此时 itUp5 指向 6 的迭代器,而 itUp7 指向 std::set::end 
	std::set<int>::iterator itUp5 = m_setValue.upper_bound(5);
	std::set<int>::iterator itUp7 = m_setValue.upper_bound(7);
3.3.5 equal_range(val)

功能:
equal_range(val) 官方给出的解释是:返回set容器中两个相同的val的值范围

返回值:
但是因为set容器中不允许出现相同的val(每个val元素都是唯一的),所以该函数返回val指向的迭代器 和 val后面一个的迭代器,返回的两个迭代器和 lower_bound(val) & upper_bound(val)类似。

代码演示:

	std::pair<std::set<int>::const_iterator, std::set<int>::const_iterator> itEqual;
	itEqual = m_setValue.equal_range(4);

	qDebug("itEqual.first[%d] -- itEqual.second[%d]", *itEqual.first, *itEqual.second);

输出结果

m_setValue.find(2) value is = [2]
itEqual.first[4] – itEqual.second[5]

3.4、Modifiers(修改)

3.4.1 insert (…)

功能:
向容器中添加元素,以实现扩充容器的效果

常用接口函数有:

(1) pair<iterator, bool> insert(const value_type& val); 添加静态val值
    pair<iterator, bool> insert(value_type&& val); //添加val值
(2) iterator insert(const_iterator position, const value_type& val); //在position位置添加静态val值
    iterator insert(const_iterator position, value_type&& val);  //在position位置添加val值
(3) template <class InputIterator>
    void insert(InputIterator first, InputIterator last);  //将另一个同类型的数值范围添加到set容器中

返回值:

返回值类型:std::pair<std::set<int>::iterator, bool> 
参数1:std::set<int>::iterator:迭代器指向新加入的元素
参数2:bool : 表示该元素是否存在过(如果之前不存在,返回true,如果之前已存在,返回false)

说 明:
我们在使用insert添加容器元素值得时候,大多数情况下是不考虑返回值的。

代码演示:

	// 例如:m_setValue.insert(7) : bool值为false
	//       m_setValue.insert(8) : bool值为true
	std::pair<std::set<int>::iterator, bool> itInsert7;
	std::pair<std::set<int>::iterator, bool> itInsert8;
	itInsert7 = m_setValue.insert(7);
	itInsert8 = m_setValue.insert(8);
	qDebug("itInsert7.first[%d] - [%d]", *itInsert7.first, itInsert7.second);
	qDebug("itInsert8.first[%d] - [%d]", *itInsert8.first, itInsert8.second);

	//除了insert(val)外,还有其他两种用法,这里不再细说。
	//iterator insert (iterator position, const value_type& val)
	//template <class InputIterator>
	//void insert(InputIterator first, InputIterator last);
3.4.2 erase(…)

功能:
表示取出(删除)某个元素或者某个范围内的元素。

常用接口函数有:

(1) void erase(iterator position);  清除迭代器指向的某个元素
(2) size_type erase(const value_type& val);  清除容器中的val元素
(3) void erase(iterator first, iterator last);  清除迭代器[first, last]范围内的元素

返回值:
该函数在使用时,很少去关注其返回值。在C++98版本中,是没有返回值的;而在C++11版本中,返回一个迭代器(指向删除元素的后面的一个)。

代码演示:

	m_setValue.erase(8);
	m_setValue.erase(itInsert7.first);
	std::set<int>::iterator it3 = m_setValue.find(3);
	std::set<int>::iterator it5 = m_setValue.find(5);
	m_setValue.erase(it3, it5);
3.4.3 swap(…)

功能:
用x的内容交换容器的内容,x是另一组相同类型的内容。 大小可能有所不同。

返回值:
void

代码演示:

	std::set<int> setChild;
	setChild.insert(100);
	setChild.insert(200);
	setChild.insert(300);
	m_setValue.swap(setChild);
3.4.4 clear()

功能:
清空容器中的所有元素

返回值:
void

代码演示:

	m_setValue.clear();

4、注意事项

1、上面的演示过程都是按照顺序进行的,元素的增减也是有序的。所以举例效果需要按照顺序查看。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值