C++
Set容器是一个关联容器,容器中的元素互不相同,并且容器中的元素按照键值大小进行排序。每当插入或者删除一个元素,容器都会重新排序。Set容器有两大特点,一个是元素排序,另一个就是查询速度快(当然没有vector快)。Set获取元素时通过键值,关联容器都这样。Set是通过二元查找树实现的,再具体点就是红黑树。
1
member type | definition | notes |
key_type | Thefirst template parameter (T) | |
value_type | Thefirst template parameter (T) | |
key_compare | Thesecond template parameter (Compare) | defaults to: |
value_compare | Thesecond template parameter (Compare) | defaults to: |
allocator_type | Thethird template parameter (Alloc) | defaults to:allocator |
reference | allocator_type::reference | forthe default |
const_reference | allocator_type::const_reference | forthe default |
pointer | allocator_type::pointer | forthe default |
const_pointer | allocator_type::const_pointer | forthe default |
iterator | a | convertible to |
const_iterator | a | |
reverse_iterator | | |
const_reverse_iterator | | |
difference_type | asigned integral type, identical to:iterator_traits::difference_type | usually the same as |
size_type | anunsigned integral type that can represent any non-negative valueof | usually the same as |
关于这个无需多言,切记set中的iterator是指向const元素。
2
empty (1) | explicit set (const key_compare& comp =key_compare(), explicit set (const allocator_type&alloc); |
range (2) | template |
copy (3) | set (const set& x); set (const set& x, const allocator_type&alloc); |
move (4) | set (set&& x); set (set&& x, const allocator_type&alloc); |
initializer list (5) | set (initializer_list il, |
以上分别为默认构造函数,范围构造函数,复制构造函数,移动构造函数,初始化列表构造函数,其中最后两个是C++11中的版本。关于构造函数,不同的编译器可能提供的形式不一样,但是种类都是这几种。
构造函数示例:
#include
#include
boolfncomp (
intlhs,
intrhs) {
returnlhs
struct classcomp {
bool
operator
() (
constint
& lhs,
constint
& rhs)
const
return
{ lhs
};
int main ()
{
> first;
std::set<</span>int // empty set of ints
int
myints[]= {10,20,30,40,50};
> second (myints,myints+5);
std::set<</span>int // range
> third (second);
std::set<</span>int // a copy of second
std::set<</span>int
> fourth (second.begin(), second.end()); //iterator ctor.
,classcomp> fifth;
std::set<</span>int // class as Compare
bool
(*fn_pt)(
int,
int) = fncomp;
,bool
std::set<</span>int (*)(
int,
int)> sixth (fn_pt);
// function pointer as Compare
return
0;
}
3
begin
end
rbegin
rend
cbegin
cend
crbegin
crend
关于迭代器也没什么好说的,迭代器和前面的都一样。
4
empty
size
max_size
这个和其它容量一样。
5
insert
erase
swap
clear
emplace
emplace_hint
5.1
single element (1) | pair insert (const value_type&val); pair insert (value_type&& val); |
with hint (2) | iterator insert (const_iterator position, constvalue_type& val); iterator insert (const_iterator position,value_type&& val); |
range (3) | template |
initializer list (4) | void insert (initializer_list il); |
要注意其中的函数的返回类型,这个在编程中或许会很有用,两种不同的颜色来区分C++11中增加的函数,示例如下:
#include
#include
int main ()
{
std::set<</span>int
> myset;
std::set<</span>int
>::iterator it;
int
std::pair >::iterator,
bool> ret;
// set some initial values:
for
(
inti=1; i<=5; ++i) myset.insert(i*10);
// set: 10 20 30 40 50
ret = myset.insert(20);
// no new element inserted
if
(ret.second==
false) it=ret.first;
// "it" now points to element 20
myset.insert (it,25);
// max efficiency inserting
myset.insert (it,24);
// max efficiency inserting
myset.insert (it,26);
// no max efficiency inserting
int
myints[]= {5,10,15};
// 10 already in set, not inserted
myset.insert (myints,myints+3);
"myset contains:"
std::cout << ;
for
(it=myset.begin(); it!=myset.end(); ++it)
' '
std::cout << << *it;
'\n'
std::cout << ;
return
0;
}
结果:
myset contains: 5 10 15 20 24 25 26 30 40 50
5.2erase 函数
(1) | |
(2) | size_type erase (const value_type&val); |
(3) | |
(1) | iterator |
(2) | size_type erase (const value_type&val); |
(3) | iterator |
红色表格表示C++98,蓝色表示C++11。函数的返回值,其中第二个函数表示删除的元素的个数,当然在set中其返回值最多是1,在C++11中,其余两个函数皆有返回值为iterator类型的值,其指向删除的最后一个元素,或者指向set的末尾。示例如下:
#include
#include
int main ()
{
std::set<</span>int
> myset;
std::set<</span>int
>::iterator it;
// insert some values:
for
(
inti=1; i<10; i++) myset.insert(i*10);
// 10 20 30 40 50 60 70 80 90
it = myset.begin();
++it;
// "it" points now to 20
myset.erase (it);
myset.erase (40);
it = myset.find (60);
myset.erase (it, myset.end());
"myset contains:"
std::cout << ;
for
(it=myset.begin(); it!=myset.end(); ++it)
' '
std::cout << << *it;
'\n'
std::cout << ;
return
0;
}
结果:
myset contains: 10 30 50
5.3
void clear();
清空set,容器大小变为0
5.4
void swap (set& x);
交换两个set的内容
5.5
template
pair emplace (Args&&... args);
这个是C++11中的函数,也是插入一个元素
template
iterator emplace_hint (const_iterator position, Args&&... args);
在一定的位置插入元素,position参数只是用来提高插入的速度,并不一定就是说要在此处插入元素。示例
#include
#include
#include
int main ()
{
std::set myset;
auto
it = myset.cbegin();
"alpha"
myset.emplace_hint (it, );
"omega"
it = myset.emplace_hint (myset.cend(), );
"epsilon"
it = myset.emplace_hint (it, );
"beta"
it = myset.emplace_hint (it, );
"myset contains:"
std::cout << ;
for
(
conststd::string& x: myset)
' '
std::cout << << x;
'\n'
std::cout << ;
return
0;
}
结果:
myset contains: alpha beta epsilon omega
6
key_comp
value_comp
这两个函数都是获取set容器中比较函数
6.1
key_compare key_comp() const;
函数返回比较函数对象,默认的是升序排列。示例:
#include
#include
int main ()
{
std::set<</span>int
> myset;
int
highest;
std::set<</span>int
>::key_compare mycomp = myset.key_comp();
for
(
inti=0; i<=5; i++) myset.insert(i);
"myset contains:"
std::cout << ;
highest=*myset.rbegin();
std::set<</span>int
>::iterator it=myset.begin();
do
{
' '
std::cout << << *it;
while
} ( mycomp(*(++it),highest) );
'\n'
std::cout << ;
return
0;
}
结果:
myset contains: 0 1 2 3 4
6.2value_com 函数
value_compare value_comp()const
函数返回元素比较函数对象,默认的是升序排列,在set中,value_comp函数和key_value函数的作用一模一样。示例:
#include
#include
int main ()
{
std::set<</span>int
> myset;
std::set<</span>int
>::value_compare mycomp = myset.value_comp();
for
(
inti=0; i<=5; i++) myset.insert(i);
"myset contains:"
std::cout << ;
int
highest=*myset.rbegin();
std::set<</span>int
>::iterator it=myset.begin();
do
{
' '
std::cout << << *it;
while
} ( mycomp(*(++it),highest) );
'\n'
std::cout << ;
return
0;
}
结果:
myset contains: 0 1 2 3 4
7set的其他操作函数
find
count
lower_bound
upper_bound
equal_range
get_allocator
7.1find 函数
iterator
| find(const value_type& val) const;(C++98) |
const_iterator
| find(const value_type& val) const;(C++11) |
iterator
| find(const value_type& val); |
函数返回找到元素的iterator,如果找不到就指向set的末尾
#include
#include
int main ()
{
std::set<</span>int
> myset;
std::set<</span>int
>::iterator it;
// set some initial values:
for
(
inti=1; i<=5; i++) myset.insert(i*10);
// set: 10 20 30 40 50
it=myset.find(20);
myset.erase (it);
myset.erase (myset.find(40));
"myset contains:"
std::cout << ;
for
(it=myset.begin(); it!=myset.end(); ++it)
' '
std::cout << << *it;
'\n'
std::cout << ;
return
0;
}
结果:
myset contains: 10 30 50
7.2count 函数
size_type count (const value_type& val) const;
函数返回值为val的元素的个数,当然在set容器中其最大为1.示例:
#include
#include
int main ()
{
std::set<</span>int
> myset;
// set some initial values:
for
(
inti=1; i<5; ++i) myset.insert(i*3);
// set: 3 6 9 12
for
(
inti=0; i<10; ++i)
{
std::cout << i;
if
(myset.count(i)!=0)
" is an element of myset.\n"
std::cout << ;
else
" is not an element of myset.\n"
std::cout << ;
}
return
0;
}
结果:
0 is not an element of myset.
1 is not an element of myset.
2 is not an element of myset.
3 is an element of myset.
4 is not an element of myset.
5 is not an element of myset.
6 is an element of myset.
7 is not an element of myset.
8 is not an element of myset.
9 is an element of myset.
7.3lower_bound 函数
iterator lower_bound (const value_type& val) const; (C++98)
iterator lower_bound (const value_type& val);(C++11)
const_iterator lower_bound (const value_type& val) const;(C++11)
函数返回set中第一个小于或者等于val的元素的iterator。
7.4upper_bound 函数
iterator upper_bound (const value_type& val) const; (C++98)
iterator upper_bound (const value_type& val);(C++11)
const_iterator upper_bound (const value_type& val) const;(C++11)
函数返回set中第一个大于或者等于val的元素的iterator。示例
#include
#include
int main ()
{
std::set<</span>int
> myset;
std::set<</span>int
>::iterator itlow,itup;
for
(
inti=1; i<10; i++) myset.insert(i*10);
// 10 20 30 40 50 60 70 80 90
//
itlow=myset.lower_bound (30); ^
//
itup=myset.upper_bound (60); ^
myset.erase(itlow,itup);
// 10 20 70 80 90
"myset contains:"
std::cout << ;
for
(std::set<</span>int
>::iterator it=myset.begin(); it!=myset.end(); ++it)
' '
std::cout << << *it;
'\n'
std::cout << ;
return
0;
}
结果:
myset contains: 10 20 70 80 90
7.5equal_range 函数
pair equal_range (const value_type& val) const; (C++98)
pair equal_range (const value_type& val) const;(C++11)
pair equal_range (const value_type& val);(C++11)
函数返回等于set中val的上下界的iterator。示例:
#include
#include
int main ()
{
std::set<</span>int
> myset;
for
(
inti=1; i<=5; i++) myset.insert(i*10);
// myset: 10 20 30 40 50
int
std::pair >::const_iterator,std::set<</span>int
>::const_iterator> ret;
ret = myset.equal_range(30);
"the lower bound points to: "
std::cout << << *ret.first <<
'\n';
"the upper bound points to: "
std::cout << << *ret.second <<
'\n';
return
0;
}
结果:
the lower bound points to: 30
the upper bound points to: 40
7.6get_allocator 函数
allocator_type get_allocator() const;
函数返回set的分配器对象 示例:
#include
#include
int main ()
{
std::set<</span>int
> myset;
int
* p;
unsigned
int
i;
// allocate an array of 5 elements using myset's allocator:
p=myset.get_allocator().allocate(5);
// assign some values to array
for
(i=0; i<5; i++) p[i]=(i+1)*10;
"The allocated array contains:"
std::cout << ;
for
(i=0; i<5; i++) std::cout <<
' '<< p[i];
'\n'
std::cout << ;
myset.get_allocator().deallocate(p,5);
return
0;
}
结果:
The allocated array contains: 10 20 30 40 50