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

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//返回指向容器中最后一个元素的反向迭代器(即,它的反向开始)。
std::set<int>::reverse_iterator itrBegin = m_setValue.rbegin();
//std::set<int>::reverse_iterator
itcrBegin
=
m_setValue.crbegin(); //const 静态的
返回值: 但是通过我的演示发现,因为 set 会自动排序,所以 lower_bound(val)返回
的迭代器通常会指向 val,哪怕 val 是最后一个元素值,其返回的迭代器依旧 是指向 val, 而不是 std::set::end。
代码演示: //例如:当前 m_setValue[1,2,3] 此时 itLow3 指向 3 的迭代器,而不是
返回值: 但是通过我的演示发现,因为 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)
2.2、Capacity(容量):
名称
empty size max_size
描述
Test whether container is empty (public member function) Return container size (public member function) Return maximum size (public member function)
输出结果 > 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 等效的元素
3.2、Capacity(容量)
3.2.1 empty()
功能: 判断容器是否为空
返回值: 返回设置的容器是否为空(即其大小是否为 0) 为空返回 true, 否则返回
false
代码演示: if (!m_setValue.empty()) { qDebug("m_setValue is not empty."); }
2.3、Modifiers(修改):
名称
描述
insert erase swap clear emplace emplace_hint
Insert element (public member function) Erase elements (public member function) Swap content (public member function) Clear content (public member function) Construct and insert element (public member function) Construct and insert element with hint (public member function)
3.1、Iterators(迭代器)
//返回一个迭代器,指向容器的地址起始位置元素 std::set<int>::iterator itBegin = m_setValue.begin(); //const 静态的 //std::set<int>::iterator itcBegin = m_setValue.cbegin(); //返回一个迭代器,指向容器中的 past-the-end 元素(最后一个元素的下 一个位置)。 std::set<int>::iterator itEnd = m_setValue.end(); //std::set<int>::iterator itcEnd = m_setValue.cend(); //const 静 态的
文档声明: 以下资料均属于本人在学习过程中产出的学习笔记,如果错误或者遗漏之处, 请多多指正。并且该文档在后期会随着学习的深入不断补充完善。
资料仅供学习交流使用。 作者:Aliven888
1、简述
定义:
set 是一种二进制搜索树,是按照特定顺序存储唯一元素的一个集合。
特点:
1、在 set 中,每一个元素都是唯一的; 2、在 set 中,元素的值是不能被修改(元素始终为 const)的,但可以将新的 元素插入容器中 或 将旧的元素从容器中删除。
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"); }
功能: equal_rangeቤተ መጻሕፍቲ ባይዱval) 官方给出的解释是:返回 set 容器中两个相同的 val 的值范 围
返回值: 但是因为 set 容器中不允许出现相同的 val(每个 val 元素都是唯一的),所以 该函数返回 val 指向的迭代器 和 val 后面一个的迭代器,返回的两个迭代器和 lower_bound(val) & upper_bound(val)类似。
3.2.2 size()
功能: 获取容器中当前元素的个数
返回值: 返回容器中的当前拥有的元素个数
代码演示: int iSize = m_setValue.size(); qDebug("m_setValue size is = [%d]", iSize);
3.2.3 max_size()
功能: 获取容器最大所能容纳的元素个数。但是由于已知的系统或库实现限制,这
3、接口函数使用案例
接下来通过函数实例介绍下每个接口函数是如何使用,以及使用的过程中有哪些 注意事项。 依赖的头文件:
#include <set>
首先定义一个 Vector 对象,并附上初始值: std::set<int> m_setValue; m_setValue.insert(1); m_setValue.insert(2); m_setValue.insert(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.
cbegin
cend crbegin
crend
Return const_iterator to beginning (public member function) Return const_iterator to end (public member function) Return const_reverse_iterator to reverse beginning (public member function) Return const_reverse_iterator to reverse end (public member function)
返回值: 如果找到了 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); }
是容器可以达到的最大潜在大小;绝不能保证容器能够达到该大小,有可能在达 到该大小之前,它仍然存在无法分配存储的情况。
返回值: 返回设置的容器可以容纳的最大元素数。
代码演示: int iMax_size = m_setValue.max_size(); qDebug("m_setValue max_size is = [%d]", iMax_size);
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.
2.4、Operations(操作):
名称
描述
find count
lower_bound upper_bound equal_range
Get iterator to element (public member function) Count elements with a specific value (public member function) Return iterator to lower bound (public member function) Return iterator to upper bound (public member function) Get range of equal elements (public member function)
//返回一个反向迭代器,该反向迭代器指向 set 容器中第一个元素之前的理 论元素(被视为其反向端)。
std::set<int>::reverse_iterator itrEnd = m_setValue.rend();
//const 静态的 //std::set<int>::reverse_iterator itcrEnd = m_setValue.crend();
2、接口函数
下面是 SDK 官网的接口,以及其功能简述。
2.1、Iterators(迭代器):
名称
begin end rbegin
rend
描述
Return iterator to beginning (public member function) Return iterator to end (public member function) Return reverse iterator to reverse beginning (public member function) Return reverse iterator to reverse end (public member function)
相关文档
最新文档