首页 > 编程知识 正文

set含义,safe

时间:2023-05-04 13:53:12 阅读:39330 作者:2962

【C STL】Set和多点

1、结构

set和multiset根据特定的排序策略对元素进行排序。 两者的区别在于,multisets允许元素重复,而set不允许元素重复。

如果是assignable、copyable、comparable (基于某个排序标准)类型t,则是set或multisets的元素。 如果没有特殊的排序策略,operator使用默认的less比较了元素并完成了排序。

排序标准必须遵循以下原则:

必须是“反对称”。 对于操作者来说,如果x y为真,则yx为假。

必须是“可传递的”。 对操作者来说,如果xy为真,yz为真,则xz也为真。

必须“非自反”,对操作者来说xx永远是赝品。

2、能力

与所有标准相关容器一样,sets和multisets通常用平衡二叉树完成。

自动排序的主要优点是使二叉树的搜索元素具有优良的性能,使其搜索函数算法具有对数复杂度。 但是,自动排序也有限制,不能直接更改元素的值。 这将打乱原始顺序,因此要更改元素的值,必须删除旧元素,然后插入新元素。 因此,sets和multisets具有以下特征:

没有用于直接访问元素的操作元素

通过迭代器访问元素。

3、操作函数

3.1结构、拷贝和析构函数

操作

效果

set c

生成不包含元素的空set/multiset

setc(op )是

使用op作为排序标准生成空的set/multiset

SETC1(C2 )。

创建set/multiset的副本并复制所有元素

setc(Beg,end ) )。

在区间[beg,end内的所有要素中生成一个set/multiset

setc(Beg,end,op ) )。

以op为排序基准,区间[beg,end内的要素生成set/multiset

c.~set () ) )。

销毁所有元素并释放内存

设置

生成set,以(操作者)为排序标准

setElem,0p

生成以op为排序标准的set

3.2非波动操作

操作

效果

c.size () )。

返回当前元素的数量

c.empty () ) )。

判断大小是否为零与0==size ()相同,效率更高

c.max_size (

返回可容纳的最大元素数

c1==c2

判断c1是否等于c2

c1!=c2

判断c1是否不等于c2 ()相等! (c1==c2 ) )

c1 c2

判断c1是否小于c2

c1 c2

判断c1是否大于c2

c1=c2

判断c1是否在c2以下()同等! (c2c1) )

c1=c2

判断c1是否在c2以上()同等! (c1c2 ) )

3.3特殊搜索函数

sets和multisets针对元素的快速搜索进行了优化,并提供了特殊的搜索函数。 因此,必须优先使用这些搜索函数来获得对数复杂度而不是STL的线性复杂度。 例如,在搜索1000个要素时,对数复杂度平均为10次,但线性复杂度平均需要500次。

操作

效果

计数(elem ) )。

返回元素值为elem的个数

查找(elem ) )。

返回元素值为elem的第一个元素。 end (如果未返回

lower _bound(elem )

返回元素值为elem的第一个可插入位置,即元素值=elem的第一个元素位置

upper _bound (elem )

返回元素值为elem的最后一个可插入位置,即元素值为elem的第一个元素位置

equal_range (elem )

返回elem可以插入的第一个和最后一个位置,即元素值==elem的区间

3.4赋值

操作

效果

c1=c2

将c2的元素全部给予c1

C1.swap(C2 ) )。

调换c1和c2的元素

swap(C1,c2 ) )。

同上,全局函数

3.5迭代器关联函数

sets和multisets迭代程序是双向迭代程序,迭代程序操作将所有元素视为常量,无法调用变动算法,因为它们会人为更改元素的值,以避免打乱默认顺序,如remove ()。

可恶

效果

c.begin()

返回一个随机存取迭代器,指向第一个元素

c.end()

返回一个随机存取迭代器,指向最后一个元素的下一个位置

c.rbegin()

返回一个逆向迭代器,指向逆向迭代的第一个元素

c.rend()

返回一个逆向迭代器,指向逆向迭代的最后一个元素的下一个位置

3.6 安插和删除元素
  必须保证参数有效,迭代器必须指向有效位置,序列起点不能位于终点之后,不能从空容器删除元素。

操作

效果

c.insert(elem)

插入一个elem副本,返回新元素位置,无论插入成功与否。

c.insert(pos, elem)

安插一个elem元素副本,返回新元素位置,pos为收索起点,提升插入速度。

c.insert(beg,end)

将区间[beg,end)所有的元素安插到c,无返回值。

c.erase(elem)

删除与elem相等的所有元素,返回被移除的元素个数。

c.erase(pos)

移除迭代器pos所指位置元素,无返回值。

c.erase(beg,end)

移除区间[beg,end)所有元素,无返回值。

c.clear()

移除所有元素,将容器清空

4、示例代码
4.1 set
复制代码
// cont/set1.cpp

#include <iostream>#include <set>using namespace std;int main(){ /*type of the collection: *-no duplicates *-elements are integral values *-descending order */ typedef set<int,greater<int> > IntSet; IntSet coll1; // empty set container //insert elements in random order coll1.insert(4); coll1.insert(3); coll1.insert(5); coll1.insert(1); coll1.insert(6); coll1.insert(2); coll1.insert(5); //iterate over all elements and print them IntSet::iterator pos; for (pos = coll1.begin(); pos != coll1.end(); ++pos) { cout << *pos << ' '; } cout << endl; //insert 4 again and process return value pair<IntSet::iterator,bool> status = coll1.insert(4); if (status.second) { cout << "4 inserted as element " << distance (coll1.begin(),status. first) + 1 << endl; } else { cout << "4 already exists" << endl; } //assign elements to another set with ascending order set<int> coll2(coll1.begin(), coll1.end()); //print all elements of the copy copy (coll2.begin(), coll2.end(), ostream_iterator<int>(cout," ")); cout << endl; //remove all elements up to element with value 3 coll2.erase (coll2.begin(), coll2.find(3)); //remove all elements with value 5 int num; num = coll2.erase (5); cout << num << " element(s) removed" << endl; //print all elements copy (coll2.begin(), coll2.end(), ostream_iterator<int>(cout," ")); cout << endl;}

复制代码
输出:

6 5 4 3 2 1
4 already exists
1 2 3 4 5 6
1 element(s) removed
3 4 6
4.2 multiset
复制代码
// cont/mset1.cpp

#include
#include
using namespace std;

int main()
{

/*type of the collection: *-duplicates allowed *-elements are integral values *-descending order */ typedef multiset<int,greater<int> > IntSet; IntSet coll1, // empty multiset container //insert elements in random order coll1.insert(4); coll1.insert(3); coll1.insert(5); coll1.insert(l); coll1.insert(6); coll1.insert(2); coll1.insert(5); //iterate over all elements and print them IntSet::iterator pos; for (pos = coll1.begin(); pos != coll1.end(); ++pos) { cout << *pos << ' '; } cout << endl; //insert 4 again and process return value IntSet::iterator ipos = coll1.insert(4); cout << "4 inserted as element " << distance (coll1.begin(),ipos) + 1 << endl; //assign elements to another multiset with ascending order multiset<int> coll2(coll1.begin(), coll1.end()); //print all elements of the copy copy (coll2.begin(), coll2.end(), ostream_iterator<int>(cout," ")); cout << endl; //remove all elements up to element with value 3 coll2.erase (coll2.begin(), coll2.find(3)); //remove all elements with value 5 int num; num = coll2.erase (5); cout << num << " element(s) removed" << endl; //print all elements copy (coll2.begin(), coll2.end(), ostream_iterator<int>(cout," ")); cout << endl;

}
复制代码
输出:

6 5 5 4 3 2 1
4 inserted as element 5
1 2 3 4 4 5 5 6
2 element(s) removed
3 4 4 6

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。