浏览本文之前,事先告知,本文由两个部分,如果你只是忘记了<set>的某些常用的用法,那只要看前半部分《STL_<set>快速养成》,如果你是初次接触set并且想要做一番深究的话,可以把整篇都看完,相信大家或多或少都会有所收获
一。《STL_<set>快速养成》
1.<set>的特性
<set>中所有的元素都互不相同,并且是有序的(默认从小到大),在内部是通过二叉查找树实现,与map不同的是其关键词(key)和值(value)相等
2.如何创建
-创建一个空的set
set<int> set_empty
-创建一个带大于比较器的set,默认是小于比较器less<int>
set<int,greater<int>> set_greater
-用数组初始化一个set
int array[3]={1,2,3};
set<int> set_array(array,array+3);
-用拷贝构造函数初始化set
set<int> set_1;
set<int> set_2(set_1);
-区间初始化set
set<int> set_1;
set<int> set_2(set_1.begin(),set_1.end())
-自定义比较器
以类为比较器
struct classcmp
{
bool operator()(const int& lhs, const int& rhs)
{
return lhs < rhs ;
}
};
int main(void)
{
set<int, classcmp> s5
return 0;
}
以函数指针为比较器
bool fncmp(int lhs, int rhs)
{
return lhs < rhs ;
}
int main(void)
{
bool(*fn_pt)(int, int) = fncmp ;
set<int, bool(*)(int, int)> s1(fn_pt) ;
system("pause") ;
return 0 ;
}
3.如何遍历
-正向遍历
使用while()
int a[3] = {1, 2, 3} ;
set<int> s(a, a + 3) ;
set<int>::const_iterator itor ;
itor = s.begin() ;
while (itor != s.end())
{
cout << *itor << endl ;
++itor ;
}
使用for()
int a[3] = {1, 2, 3} ;
set<int> s(a, a + 3) ;
set<int>::const_iterator itor ;
for (itor = s.begin(); itor != s.end(); ++itor)
{
cout << *itor << endl ;
}
-反向遍历
使用while()
int a[3] = {1, 2, 3} ;
set<int> s(a, a + 3) ;
set<int>::const_reverse_iterator ritor ;
ritor = s.rbegin() ;
while (ritor != s.rend())
{
cout << *ritor << endl ;
++ritor ;
}
使用for()
int a[3] = {1, 2, 3} ;
set<int> s(a, a + 3) ;
set<int>::const_reverse_iterator ritor ;
for (ritor = s.rbegin(); ritor != s.rend(); ++ritor)
{
cout << *ritor << endl ;
}
4.如何插入
-插入单个值
set<int> s;
s.insert(value);
-插入整个数组
int a[3] = {1, 2, 3} ;
set<int> s ;
s.insert(a, a + 3) ;
-插入其他set的值
int a[3] = {1, 2, 3} ;
set<int> s(a, a + 3) ;
set<int> s1 ;
s1.insert(s.begin(), s.end()) ;
5.如何删除
set<int> s ;
for (int i = 1; i <= 5; ++i)
s.insert(i) ;
set<int>::const_iterator citor ;
citor = s.begin() ;
++citor ; // citor now point to 2
// 删除单个元素
s.erase(citor) ; // erase 2 ;
//批量删除
citor = s.find(3) ; // itor now point to 3
s.erase(citor, s.end()) ; // erase 3, 4, 5
//删除所有元素
s.erase(s.begin(), s.end()) ;// erase all elements, same as s.clear()
6.如何查找
-find()
set<int> s ;
for (int i = 1; i <= 5; ++i)
s.insert(i) ;
set<int>::iterator itor ;
itor = s.find(4) ;
if(itor != s.end()) // itor point to s.end() if not found
cout << "found" ;
else
cout << "not found" ;
-count()
set<int> s ;
for (int i = 1; i <= 5; ++i)
s.insert(i) ;
set<int>::iterator itor ;
if(s.count(4) == 1) // return 1 if s contains 4, else 0
cout << "s contains 4" ;
else
cout << "s does not contains 4" ;
OK,速成到这里就结束了,想必大家已经可以照葫芦画瓢写出代码了,接下来是对其内部的解释,当然不是我写的,是一篇类似于API的文章
C++ STL之Set容器的用法
Set容器是一个关联容器,容器中的元素互不相同,并且容器中的元素按照键值大小进行排序。每当插入或者删除一个元素,容器都会重新排序。Set容器有两大特点,一个是元素排序,另一个就是查询速度快(当然没有vector快)。Set获取元素时通过键值,关联容器都这样。Set是通过二元查找树实现的,再具体点就是红黑树。
1 set中的元素类型
member type |
definition |
notes |
key_type |
The first template parameter (T) |
|
value_type |
The first template parameter (T) |
|
key_compare |
The second template parameter (Compare) |
defaults to: |