admin 管理员组文章数量: 887021
2023年12月25日发(作者:全局变量存放在静态存储区)
C++ STL之Set容器的用法
1 set中的元素类型 ..................................................................................................................... 1
2 set中构造相关函数 ................................................................................................................. 2
3 set中的迭代器 ......................................................................................................................... 3
4 set中的容量相关函数 ............................................................................................................. 3
5 set中元素修改函数 ................................................................................................................. 3
5.1 insert 函数 ................................................................................................................... 3
5.2 erase 函数.................................................................................................................... 4
5.3 clear 函数 .................................................................................................................... 5
5.4 swap 函数 ................................................................................................................... 5
5.5 emplace 函数 .............................................................................................................. 5
5.6 emplace_hint 函数 ..................................................................................................... 5
6 set 中的比较函数体 ................................................................................................................ 6
6.1 key_com 函数 ............................................................................................................. 6
6.2 value_com 函数 ......................................................................................................... 6
7 set的其他操作函数 ................................................................................................................. 7
7.1 find 函数 ..................................................................................................................... 7
7.2 count 函数 ................................................................................................................... 8
7.3 lower_bound 函数 ........................................................................................................ 8
7.4 upper_bound 函数 .................................................................................................... 8
7.5 equal_range 函数 .......................................................................................................... 9
7.6 get_allocator 函数 ....................................................................................................... 10
Set容器是一个关联容器,容器中的元素互不相同,并且容器中的元素按照键值大小进行排序。每当插入或者删除一个元素,容器都会重新排序。Set容器有两大特点,一个是元素排序,另一个就是查询速度快(当然没有vector快)。Set获取元素时通过键值,关联容器都这样。Set是通过二元查找树实现的,再具体点就是红黑树。
1
set中的元素类型
member type definition
key_type The first template parameter (T)
value_type The first template parameter (T)
key_compare
value_compare
allocator_type
notes
reference
const_reference
pointer
defaults
The second template parameter (Compare)
to: less
defaults
The second template parameter (Compare)
to: less
defaults
The third template parameter (Alloc) to:allocator
for the
allocator_type::reference default allocator:value_type&
for the
allocator_type::const_reference default allocator: const
value_type&
for the
allocator_type::pointer default allocator:value_type*
1
const_pointer
iterator
const_iterator
reverse_iterator
const_reverse_iterreverse_iterator
ator
a signed integral type, identical
difference_type to:iterator_traits
an unsigned integral type that can
size_type represent any non-negative value
of difference_type
关于这个无需多言,切记set中的iterator是指向const 元素。
for the
allocator_type::const_pointer default allocator: const
value_type*
convertible
a bidirectional iterator to value_type
to const_iterator
a bidirectional iterator to const value_type
reverse_iterator
usually the same
as ptrdiff_t
usually the same
as size_t
2 set中构造相关函数
explicit set (const key_compare& comp = key_compare(),
empty (1)
const allocator_type& alloc = allocator_type());
explicit set (const allocator_type& alloc);
template
set (InputIterator first, InputIterator last,
range (2)
const key_compare& comp = key_compare(),
const allocator_type& = allocator_type());
set (const set& x);
copy (3)
set (const set& x, const allocator_type& alloc);
set (set&& x);
move (4)
set (set&& x, const allocator_type& alloc);
set (initializer_list
initializer list (5)
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
以上分别为默认构造函数,范围构造函数,复制构造函数,移动构造函数,初始化列表构造函数,其中最后两个是C++11中的版本。关于构造函数,不同的编译器可能提供的形式不一样,但是种类都是这几种。
构造函数示例:
#include
#include
bool fncomp (int lhs, int rhs) {return lhs struct classcomp { bool operator() (const int& lhs, const int& rhs) const {return lhs }; int main () { std::set 2 int myints[]= {10,20,30,40,50}; std::set std::set std::set std::set bool(*fn_pt)(int,int) = fncomp; std::set return 0; } 3 set中的迭代器 begin Return iterator to beginning (public member function ) end Return iterator to end (public member function ) rbegin Return reverse iterator to reverse beginning (public member function ) rend Return reverse iterator to reverse end (public member function ) cbegin Return const_iterator to beginning (public member function ) cend Return const_iterator to end (public member function ) crbegin Return const_reverse_iterator to reverse beginning (public member function ) crend Return const_reverse_iterator to reverse end (public member function ) 关于迭代器也没什么好说的,迭代器和前面的都一样。 4 set中的容量相关函数 empty size max_size Test whether container is empty (public member function ) Return container size (public member function ) Return maximum size (public member function ) 这个和其它容量一样。 5 set中元素修改函数 insert erase swap clear emplace emplace_hint insert 函数 single element (1) 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 ) with hint (2) range (3) pair pair iterator insert (const_iterator position, const value_type& val); iterator insert (const_iterator position, value_type&& val); template void insert (InputIterator first, InputIterator last); 3 initializer list (4) void insert (initializer_list 要注意其中的函数的返回类型,这个在编程中或许会很有用,两种不同的颜色来区分C++11中增加的函数,示例如下: #include #include int main () { std::set std::set std::pair // set some initial values: for (int i=1; i<=5; ++i) (i*10); // set: 10 20 30 40 50 ret = (20); // no new element inserted if (==false) it=; // "it" now points to element 20 (it,25); // max efficiency inserting (it,24); // max efficiency inserting (it,26); // no max efficiency inserting int myints[]= {5,10,15}; // 10 already in set, not inserted (myints,myints+3); std::cout << "myset contains:"; for (it=(); it!=(); ++it) std::cout << ' ' << *it; std::cout << 'n'; return 0; } 结果: myset contains: 5 10 15 20 24 25 26 30 40 50 erase 函数 (1) (2) (3) void erase (iterator position); size_type erase (const value_type& val); void erase (iterator first, iterator last); iterator erase (const_iterator position); size_type erase (const value_type& val); iterator erase (const_iterator first, const_iterator last); (1) (2) (3) 红色表格表示C++98,蓝色表示C++11。函数的返回值,其中第二个函数表示删除的元素的个数,当然在set中其返回值最多是1,在C++11中,其余两个函数皆有返回值为iterator类型的值,其指向删除的最后一个元素,或者指向set的末尾。示例如下: 4 #include #include int main () { std::set std::set // insert some values: for (int i=1; i<10; i++) (i*10); // 10 20 30 40 50 60 70 80 90 it = (); ++it; // "it" points now to 20 (it); (40); it = (60); (it, ()); std::cout << "myset contains:"; for (it=(); it!=(); ++it) std::cout << ' ' << *it; std::cout << 'n'; return 0; } 结果: myset contains: 10 30 50 clear 函数 void clear(); 清空set,容器大小变为0 swap 函数 void swap (set& x); 交换两个set的内容 emplace 函数 template < Args> pair 这个是C++11中的函数,也是插入一个元素 emplace_hint 函数 template < Args> iterator emplace_hint (const_iterator position, Args&&... args); 在一定的位置插入元素,position参数只是用来提高插入的速度,并不一定就是说要在此处插入元素。示例 #include #include #include int main () { std::set 5 auto it = (); e_hint (it,"alpha"); it = e_hint ((),"omega"); it = e_hint (it,"epsilon"); it = e_hint (it,"beta"); std::cout << "myset contains:"; for (const std::string& x: myset) std::cout << ' ' << x; std::cout << 'n'; return 0; } 结果: myset contains: alpha beta epsilon omega 6 set 中的比较函数体 key_comp value_comp Return comparison object (public member function ) Return comparison object (public member function ) 这两个函数都是获取set容器中比较函数 6.1 key_com 函数 key_compare key_comp() const; 函数返回比较函数对象,默认的是升序排列。示例: #include #include int main () { std::set int highest; std::set for (int i=0; i<=5; i++) (i); std::cout << "myset contains:"; highest=*(); std::set do { std::cout << ' ' << *it; } while ( mycomp(*(++it),highest) ); std::cout << 'n'; return 0; } 结果: myset contains: 0 1 2 3 4 6.2 value_com 函数 6 value_compare value_comp()const 函数返回元素比较函数对象,默认的是升序排列,在set中,value_comp函数和key_value函数的作用一模一样。示例: #include #include int main () { std::set std::set for (int i=0; i<=5; i++) (i); std::cout << "myset contains:"; int highest=*(); std::set do { std::cout << ' ' << *it; } while ( mycomp(*(++it),highest) ); std::cout << 'n'; return 0; } 结果: myset contains: 0 1 2 3 4 7 set的其他操作函数 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 ) Get allocator (public member function ) find(const value_type& val) const;(C++98) find(const value_type& val); (C++11) find count lower_bound upper_bound equal_range get_allocator 7.1 find 函数 iterator iterator const_iterator find(const value_type& val) const;(C++11) 函数返回找到元素的iterator,如果找不到就指向set的末尾 #include #include int main () { std::set std::set // set some initial values: for (int i=1; i<=5; i++) (i*10); // set: 10 20 30 40 50 it=(20); (it); ((40)); 7 std::cout << "myset contains:"; for (it=(); it!=(); ++it) std::cout << ' ' << *it; std::cout << 'n'; return 0; } 结果: myset contains: 10 30 50 7.2 count 函数 size_type count (const value_type& val) const; 函数返回值为val的元素的个数,当然在set容器中其最大为1.示例: #include #include int main () { std::set // set some initial values: for (int i=1; i<5; ++i) (i*3); // set: 3 6 9 12 for (int i=0; i<10; ++i) { std::cout << i; if ((i)!=0) std::cout << " is an element of myset.n"; else std::cout << " is not an element of myset.n"; } 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.3 lower_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.4 upper_bound 函数 iterator upper_bound (const value_type& val) const; (C++98) 8 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 std::set for (int i=1; i<10; i++) (i*10); // 10 20 30 40 50 60 70 80 90 itlow=_bound (30); // ^ itup=_bound (60); // ^ (itlow,itup); // 10 20 70 80 90 std::cout << "myset contains:"; for (std::set std::cout << ' ' << *it; std::cout << 'n'; return 0; } 结果: myset contains: 10 20 70 80 90 7.5 equal_range 函数 pair pair pair 函数返回等于set中val的上下界的iterator。示例: #include #include int main () { std::set for (int i=1; i<=5; i++) (i*10); // myset: 10 20 30 40 50 std::pair ret = _range(30); std::cout << "the lower bound points to: " << * << 'n'; std::cout << "the upper bound points to: " << * << 'n'; return 0; } 结果: the lower bound points to: 30 9 the upper bound points to: 40 7.6 get_allocator 函数 allocator_type get_allocator() const; 函数返回set的分配器对象 示例: #include #include int main () { std::set int * p; unsigned int i; // allocate an array of 5 elements using myset's allocator: p=_allocator().allocate(5); // assign some values to array for (i=0; i<5; i++) p[i]=(i+1)*10; std::cout << "The allocated array contains:"; for (i=0; i<5; i++) std::cout << ' ' << p[i]; std::cout << 'n'; _allocator().deallocate(p,5); return 0; } 结果: The allocated array contains: 10 20 30 40 50 10
版权声明:本文标题:C++ STL之set容器使用方法 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1703488015h453508.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论