
set的基本 *** 作:
clear() 清除所有元素
count() 返回某个值元素的个数
empty() 如果集合为空,返回true
end() 返回指向最后一个元素的迭代器
equal_range() 返回集合中与给定值相等的上下限的两个迭代器
erase() 删除集合中的元素
find() 返回一个指向被查找到元素的迭代器
get_allocator() 返回集合的分配器
insert() 在集合中插入元素
lower_bound() 返回指向大于(或等于)某值的第一个元素的迭代器
key_comp() 返回一个用于元素间值比较的函数
max_size() 返回集合能容纳的元素的最大限值
rbegin() 返回指向集合中最后一个元素的反向迭代器
rend() 返回指向集合中第一个元素的反向迭代器
size() 集合中元素的数目
swap() 交换两个集合变量
upper_bound() 返回大于某个值元素的迭代器
value_comp() 返回一个用于比较元素间的值的函数
5,自定义比较函数:
For example:
#include<iostream>#include<set>
using namespace std
typedef struct {
int a,b
char s
}newtype
struct compare //there is no ().
{
bool operator()(const newtype &a, const newtype &b) const
{
return a.s<b.s
}
}//the “ ” is here
set<newtype,compare>element
int main()
{
newtype a,b,c,d,t
a.a=1 a.s='b'
b.a=2 b.s='c'
c.a=4 c.s='d'
d.a=3 d.s='a'
element.insert(a)
element.insert(b)
element.insert(c)
element.insert(d)
set<newtype,compare>::iterator it
for(it=element.begin() it!=element.end()it++)
cout<<(*it).a<<" "
cout<<endl
for(it=element.begin() it!=element.end()it++)
cout<<(*it).s<<" "
}
element自动排序是按照char s的大小排序的;
6.其他的set构造方法;
#include <iostream>#include <set>
using namespace std
bool fncomp (int lhs, int rhs) {return lhs<rhs}
struct classcomp {
bool operator() (const int& lhs, const int& rhs) const
{return lhs<rhs}
}
int main ()
{
set<int> first // empty set of ints
int myints[]= {10,20,30,40,50}
set<int> second (myints,myints+5) // pointers used as iterators
set<int> third (second) // a copy of second
set<int> fourth (second.begin(), second.end()) // iterator ctor.
set<int,classcomp> fifth // class as Compare
bool(*fn_pt)(int,int) = fncomp
set<int,bool(*)(int,int)> sixth (fn_pt) // function pointer as Compare
return 0
}
创建set对象为了管理set的二叉树链表数据,先用set容器的构造函数,创建一个set对象
(1) set()
用默认的less<T>函数对象和内存分配器,创建一个没有任何数据元素的set对象。
set<int>s //创建了空的set对象s,元素类型为整型int
(2) set(const key_compare&comp)
指定一个比较函数对象 comp 来创建set对象,内存分配器为默认值。
//定义字符串比较函数对象 strLess
struct strLess {
bool operatro() (const char *s1, const char *s2) const
{
return strcmp(s1, s2) <0
}
}
//创建set容器对象s
set<const char*, strLess>s(strLess())
(3)set(const set&)
set拷贝构造函数,通过红黑树的拷贝构造函数,实现两个set容器的元素、头结点和节点个数的拷贝。
//set<int>s1
set<int>s2 (s1)
(4)set(InputIterator first, InputIterator last)
用区间迭代器[first, last)所指的元素,创建一个set对象。
int iArray = { 13, 32,19 }
set<int>s(iArray, iArray+3)
(5)set(InputIterator first, InputIterator last, const key_compare&comp)
用区间迭代器[first, last)所指的元素和comp函数对象,创建一个set对象。
const char* szArray = {"hello", "dog", "bird" }
set<const char* , strLess>s(szArray, szArray+3, strLess() )
元素的插入
set没有尾部插入函数push_back(),元素的插入一般使用insert进行动态检索插入。
(1)pair<iterator, bool>insert(const value_type&v)
将元素v插入set容器,要求v值不与set容器的任何元素重复,否则插入失败。返回一个pair配对对象,提供所插入元素的迭代器位置和true/false插入成功标志。
(2)iterator insert(iterator position, const value_type&v)
将元素v插入set容器,参数position提示可在position位置之前插入v,所返回的插入位置视实际情况而定,不一定能在position位置之前插入。
(3)void insert(inputIterator fist, InputIterator last)
将某迭代器区间[first, last)所指的数据作为元素,插入到set容器。
#include <iostream>#include <set>
using namespace std
void main()
{
set<int>s1//空set集合
set<int>::iterator it
int a[]= {1,2,3,4,5}
set<int>s2 (a,a+5)//利用指针对象初始化set集合
s2.insert(0) //插入方法1
s2.insert(s2.begin(),7) //插入方法2
int b[] ={10,11,12,13,14,15}
s2.insert(b,b+3) //插入方法3
cout<<"集合s2中的元素:"<<endl
for(it=s2.begin()it!=s2.end()it++) //遍历
cout<<" "<<*it<<endl
system("pause")
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)