5

day04 C++集合操作 | HBUACM

 2 years ago
source link: https://hbuacm.github.io/2022/07/07/%E6%AF%8F%E6%97%A5%E5%88%86%E4%BA%AB2022day04%EF%BC%9Ac++%E9%9B%86%E5%90%88%E6%93%8D%E4%BD%9C/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

HBUACM

day04 C++集合操作
发表于 2022-07-07| 更新于 2022-07-08|2022程序设计训练营每日分享
阅读量:1

今日份知识分享:C++集合操作

set介绍

C++ STL之所以得到广泛赞誉,也被很多人使用,不只是提供了像vector,string,list等方便的容器,更重要的是STL封装了许多复杂和大量常用的数据结构操作。

set是一种关联式容器,set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也称为RB树(Red- Black Tree)。RB树的统计性能要好于一般平衡二叉树,所以被multimap内部采用的就是一种非常 高效的平衡检索二叉树:红黑树,也成为RB树(Red- Black Tree)。RB树的统计性能要好于一般平衡二叉树,所以被STL选择作为了关联容器的内部结构。

begin:返回set容器的第一个元素

end:返回set容器最后一个元素

clear:删除set容器所有元素

empty:判断set是否为空

max_size:返回set容器可能包含的元素最大个数

size:返回当前set容器元素个数

除了一般方法外,set集合常用操作便为求交集、并集、差集、对称差集

(beg1、end1为容器1的开始、结束迭代器,beg2、end2为容器2的开始、结束迭代器)

  • 交集set_intersection()

    • 函数原型:set_intersection(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest)
    • 作用:将容器1和容器2的交集存到目标容器,dest为目标容器的起始迭代器。
  • 求并集set_union()

    • 函数原型:set_union(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest)

    • 作用:将容器1和容器2的并集存到目标容器,dest为目标容器的起始迭代器。

  • 求差集set_difference()

    • 函数原型:set_difference(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest)

    • 作用:将容器1和容器2的差集存到目标容器,dest为目标容器的起始迭代器。

  • 求对称差集set_symmetric_difference()

    • 函数原型:set_symmetric_difference(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest)

    • 作用:将容器1和容器2的对称差集存到目标容器,dest为目标容器的起始迭代器。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK