关于C++里面使用set_union,set_intersections、set_merge、set_difference、set_symmetric_difference等函数的使用总结

set里面有set_intersection(取集合交集)、set_union(取集合并集)、set_difference(取集合差集)、set_symmetric_difference(取集合对称差集)等函数。其中,关于函数的五个参数问题做一下小结:

1、这几个函数的前四个参数一样,只有第五个参数有多重版本。

2、EX1:set_union(A.begin(),A.end(),B.begin(),B.end(),inserter( C1 , C1.begin() ) );前四个参数依次是第一的集合的头尾,第二个集合的头尾。第五个参数的意思是将集合A、B取合集后的结果存入集合C中。

EX2:set_union(A.begin(),A.end(),B.begin(),B.end(),ostream_iterator<int>(cout," “));这里的第五个参数的意思是将A、B取合集后的结果直接输出,(cout," ")双引号里面是输出你想用来间隔集合元素的符号或是空格。

下面是set_union的原型:

template<class InputIterator1, class InputIterator2, class OutputIterator>

OutputIterator set_union(

InputIterator1_First1 ,

InputIterator1_Last1 ,

InputIterator2_First2 ,

InputIterator2_Last2 ,

OutputIterator_Result

);

<span style="font-family:Comic Sans MS;font-size:18px;">/*Description
集合的运算就是用给定的集合去指定新的集合。设A和B是集合,则它们的并差交补集分别定义如下:
A∪B={x|x∈A∨x∈B}
A∩B={x|x∈A∧x∈B}
A-B={x|x∈A∧x不属于 B}
SA ={x|x∈(A∪B)∧x 不属于A}
SB ={x|x∈(A∪B)∧x 不属于B}
<!--[endif]-->
Input
第一行输入一个正整数T,表示总共有T组测试数据。(T<=200)
然后下面有2T行,每一行都有n+1个数字,其中第一个数字是n(0<=n<=100),表示该行后面还有n个数字输入。
Output
对于每组测试数据,首先输出测试数据序号,”Case #.NO”,
接下来输出共7行,每行都是一个集合,
前2行分别输出集合A、B,接下5行来分别输出集合A、B的并(A u B)、交(A n B)、差(A – B)、补。
集合中的元素用“{}”扩起来,且元素之间用“, ”隔开。
Sample Input
1
4 1 2 3 1
0
Sample Output
Case# 1:
A = {1, 2, 3}
B = {}
A u B = {1, 2, 3}
A n B = {}
A - B = {1, 2, 3}
SA = {}
SB = {1, 2, 3}
*/
#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{set<int>A;set<int>B;set<int>C1;set<int>C2;set<int>C3;set<int>C4;set<int>C5;set<int>C6;set<int>::iterator pos;/// 定义迭代器,作用是输出set元素int count=0;int A_i,B_i,n,m;cin>>n;while(n--){count++;cin>>A_i;while(A_i--)///输入集合A{cin>>m;A.insert(m);}cin>>B_i;///输入集合Bwhile(B_i--){cin>>m;B.insert(m);}cout<<"Case# "<<count<<":"<<endl;cout<<"A = {";for(pos=A.begin(); pos!=A.end(); pos++)///迭代器的作用{if(pos!=A.begin())cout<<", ";cout<<*pos;///迭代器的作用,迭代器是一种特殊的指针}cout<<"}"<<endl;cout<<"B = {";for(pos=B.begin(); pos!=B.end(); pos++){if(pos!=B.begin())cout<<", ";cout<<*pos;}cout<<"}"<<endl;set_union(A.begin(),A.end(),B.begin(),B.end(),inserter( C1 , C1.begin() ) );    /*取并集运算*///set_union(A.begin(),A.end(),B.begin(),B.end(),ostream_iterator<int>(cout," "));    /*取并集运算*/ //其中ostream_iterator的头文件是iteratorcout<<"A u B = {";for(pos=C1.begin(); pos!=C1.end(); pos++){if(pos!=C1.begin())cout<<", ";cout<<*pos;}cout<<"}"<<endl;set_intersection(A.begin(),A.end(),B.begin(),B.end(),inserter( C2 , C2.begin() ));    /*取交集运算*/cout<<"A n B = {";for(pos=C2.begin(); pos!=C2.end(); pos++){if(pos!=C2.begin())cout<<", ";cout<<*pos;}cout<<"}"<<endl;set_difference( A.begin(), A.end(),B.begin(), B.end(),inserter( C3, C3.begin() ) );    /*取差集运算*/cout<<"A - B = {";for(pos=C3.begin(); pos!=C3.end(); pos++){if(pos!=C3.begin())cout<<", ";cout<<*pos;}cout<<"}"<<endl;set_difference(C1.begin(),C1.end(), A.begin(), A.end(),inserter( C4, C4.begin() ) );/*取差集运算*/cout<<"SA = {";for(pos=C4.begin(); pos!=C4.end(); pos++){if(pos!=C4.begin())cout<<", ";cout<<*pos;}cout<<"}"<<endl;set_difference(C1.begin(),C1.end(), B.begin(), B.end(),inserter( C5, C5.begin() ) );/*取差集运算*/cout<<"SB = {";for(pos=C5.begin(); pos!=C5.end(); pos++){if(pos!=C5.begin())cout<<", ";cout<<*pos;}cout<<"}"<<endl;set_symmetric_difference(A.begin(),A.end(),B.begin(),B.end(),inserter( C6 , C6.begin() ) );///取 对称差集运算cout<<"A ⊕ B = {";for(pos=C6.begin(); pos!=C6.end(); pos++){if(pos!=C6.begin())cout<<", ";cout<<*pos;}cout<<"}"<<endl;A.clear();B.clear();//各个集合清零,否则下次使用会出错C1.clear();C2.clear();C3.clear();C4.clear();C5.clear();C6.clear();}
}

https://blog.csdn.net/zangker/article/details/22984803 


// set_union example(求并集可用)
#include <iostream> // std::cout
#include <algorithm> // std::set_union, std::sort
#include <vector> // std::vector

int main () {
  int first[] = {5,10,15,20,25};
  int second[] = {50,40,30,20,10};
  std::vector<int> v(10);                      // 0 0 0 0 0 0 0 0 0 0
  std::vector<int>::iterator it;

  std::sort (first,first+5);     // 5 10 15 20 25
  std::sort (second,second+5);   // 10 20 30 40 50

  it=std::set_union (first, first+5, second, second+5, v.begin());    
     // 5 10 15 20 25 30 40 50 0 0      //参数:求并集的两个集合(数组或者set等其他类型)的起止地址,最后一个参数是前两个集合并集的结果需要插入的地方
  v.resize(it-v.begin());                      // 5 10 15 20 25 30 40 50

  std::cout << "The union has " << (v.size()) << " elements:\n";
  for (it=v.begin(); it!=v.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;

}

output

 

The union has 8 elements:5 10 15 20 25 30 40 50

 

以上来自http://www.cplusplus.com/reference/algorithm/set_union/?kw=set_union;

set_union可以对任意类型起作用。

返回值是最后一个插入的位置。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    set<int>x1;
    x1.insert(1);
    x1.insert(2);
    x1.insert(3);
    set<int>x2;
    x2.insert(1);
    x2.insert(4);
    set<int>x;
   set_union(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));    
    for(set<int>::iterator it=x.begin();it!=x.end();it++)
        cout<<*it<<' ';
    cout<<endl;
}

输出:1 2 3 4

 

set_intersection:(求交集可用)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    set<int>x1;
    x1.insert(1);
    x1.insert(2);
    x1.insert(3);
    set<int>x2;
    x2.insert(1);
    x2.insert(4);
    set<int>x;
    set_intersection(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin())); 
    for(set<int>::iterator it=x.begin();it!=x.end();it++)
        cout<<*it<<' ';
    cout<<endl;
}

输出:1

https://blog.csdn.net/frankax/article/details/54880890


集合A,B。(可以使用数组、list、vector)
头文件:#include<algorithm>
前提:两个集合已经有序。
merge() //归并两个序列,元素总个数不变,只是将两个有序序列归并为一个有序序列。
set_union() //实现求集合A,B的并。
set_difference()//实现求集合A,B的差(即A—B)
set_symmetric_difference()//实现求集合A,B的对称差(即(A-B)并(B-A))
set_intersection()//实现求集合A,B交集。
 
//集合运算
//熊八八
//2013-3-15
 
#include<iostream>
#include<stdio.h>
#include<list>
#include<algorithm>            //set_union求并集
using namespace std;
 
template<class T>
void Print(T List)
{
       class T::iterator iter;
       for(iter=List.begin(); iter!=List.end(); iter++)
              printf("%d ", *iter);
       printf("\n");
}
int main()
{
       list<int> List_A;
       list<int> List_B;
       int temp;
       printf("Enter 5 integers into List_A:\n");
       for(int i=0; i<5; i++)
       {
              scanf("%d", &temp);
              List_A.push_back(temp);
       }
       //printf("Enter some integers into List_B:\n");
       for(int i=0; i<5; i++)
       {
              scanf("%d", &temp);
              List_B.push_back(temp);
       }
      
       List_A.sort();
       List_B.sort();
      
       list<int> List_C(10);
       //不能将操作后的结果重新放入List_A或者List_B.如果非要如此,可以设一中间变量List_C,先将结果存储至List_C,然后List_A = List_C
       //merge(List_A.begin(), List_A.end(), List_B.begin(), List_B.end(), List_C.begin()); //合并
       //set_union(List_A.begin(), List_A.end(), List_B.begin(), List_B.end(), List_C.begin());//并集
       //set_difference(List_A.begin(), List_A.end(), List_B.begin(), List_B.end(), List_C.begin());//差集
       //set_symmetric_difference(List_A.begin(), List_A.end(), List_B.begin(), List_B.end(), List_C.begin());//对称差
       set_intersection(List_A.begin(), List_A.end(), List_B.begin(), List_B.end(), List_C.begin());//交集
      
       printf("List_A contains:\n");
       Print(List_A);
       printf("List_B contains:\n");
       Print(List_B);
       printf("List_C contains:\n");
       Print(List_C);
       system("pause");
       return 0;
}

https://blog.csdn.net/CillyB/article/details/60993193


如上。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441736.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java mouseenter_关于事件mouseover ,mouseout ,mouseenter,mouseleave的区别

最近在做的在线考试和课程商城都遇到这样的问题&#xff1a;就是鼠标滑过的时候出现一个层&#xff0c;当鼠标滑到当前层的话mouseover和mouseout在低版本的浏览器会出现闪动的现象&#xff0c;解决这个现象的办法有许多&#xff0c;不过我觉得有一种是最简单的那就是把mouseov…

【HDU - 1465 】不容易系列之一 (组合数学,错排)

题干&#xff1a; 大家常常感慨&#xff0c;要做好一件事情真的不容易&#xff0c;确实&#xff0c;失败比成功容易多了&#xff01; 做好“一件”事情尚且不易&#xff0c;若想永远成功而总从不失败&#xff0c;那更是难上加难了&#xff0c;就像花钱总是比挣钱容易的道理一…

java gc回收机制种类_JAVA的垃圾回收机制(GC)

1.什么是垃圾回收&#xff1f;垃圾回收(Garbage Collection)是Java虚拟机(JVM)垃圾回收器提供的一种用于在空闲时间不定时回收无任何对象引用的对象占据的内存空间的一种机制。2.什么时候垃圾回收&#xff1f;System.gc()Runtime.getRuntime().gc()上面的方法调用时用于显式通知…

【HDU - 4217 】Data Structure? (线段树求第k小数)

题干&#xff1a; Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently. Today let me introduce a data-structure-like problem for y…

java redis 重连_突破Java面试(23-4) - Redis 复制原理

全是干货的技术号&#xff1a;本文已收录在github&#xff0c;欢迎 star/fork&#xff1a;在Redis复制的基础上(不包括Redis Cluster或Redis Sentinel作为附加层提供的高可用功能)&#xff0c;使用和配置主从复制非常简单&#xff0c;能使得从 Redis 服务器(下文称 slave)能精确…

【HDU - 4006】The kth great number (优先队列,求第k大的数)

题干&#xff1a; Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too much, Xiao Bao is feeling giddy…

java获取u盘_实例分享java监听u盘的方法

package org.load.u;import java.io.File;import java.util.LinkedHashMap;import java.util.Map;// U盘检测public class CheckU {// 存放磁盘状态private static Map map new LinkedHashMap();// 定义磁盘private static final String[] arr new String[] {"C", …

【POJ - 1562】Oil Deposits (dfs搜索,连通块问题)

题干&#xff1a; The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It …

python列表浅复制_Python列表深浅复制详解

转自&#xff1a;https://www.cnblogs.com/blaomao/p/7239203.html在文章《Python 数据类型》里边介绍了列表的用法&#xff0c;其中列表有个 copy() 方法&#xff0c;意思是复制一个相同的列表。例如1 names ["小明", "小红", "小黑", "小…

【HYSBZ - 1192】鬼谷子的钱袋(水题,二进制)

题干&#xff1a; 鬼谷子非常聪明&#xff0c;正因为这样&#xff0c;他非常繁忙&#xff0c;经常有各诸侯车的特派员前来向他咨询时政。有一天&#xff0c;他在咸阳游历的时候&#xff0c;朋友告诉他在咸阳最大的拍卖行&#xff08;聚宝商行&#xff09;将要举行一场拍卖会&a…

【CodeForces - 735B】Urbanization (找规律,思维)

题干&#xff1a; Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are n people who plan to move to the cities. The wealth of the i of them is equal to a…

java 文本查找_Java基于正则表达式实现查找匹配的文本功能【经典实例】

本文实例讲述了Java基于正则表达式实现查找匹配的文本功能。分享给大家供大家参考&#xff0c;具体如下&#xff1a;REMatch.java&#xff1a;package reMatch;import java.util.regex.Matcher;import java.util.regex.Pattern;/*** Created by Frank*/public class REMatch {p…

【51nod - 1073】约瑟夫环问题模板

题干&#xff1a; N个人坐成一个圆环&#xff08;编号为1 - N&#xff09;&#xff0c;从第1个人开始报数&#xff0c;数到K的人出列&#xff0c;后面的人重新从1开始报数。问最后剩下的人的编号。 例如&#xff1a;N 3&#xff0c;K 2。2号先出列&#xff0c;然后是1号&am…

java oracle database user dsn_跨会话序列化数据库连接

我正在开发一个web应用程序&#xff0c;它需要使用最终用户提供的凭据登录到数据库&#xff1b;应用程序本身没有登录到数据库。在问题是如何为每个用户会话创建一个连接。在一种方法是&#xff1a;请求用户凭据检查针对数据库后端的凭据是否有效在会话级变量中存储凭据这种方法…

【POJ - 3125 】Printer Queue(模拟,队列+优先队列,STL)

题干&#xff1a; The only printer in the computer science students union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer queue and you may have to wait for hours to get a single page of output. Because some …

java循环单链表类构造函数_C++实现双向循环链表

本文实例为大家分享了C实现双向循环链表的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下一、概念1.在双链表中的每个结点应有两个链接指针&#xff1a;lLink -> 指向前驱结点 (前驱指针或者左链指针)rLink->指向后继结点(后驱指针或者右链指针)2.双链表常采用…

*【CodeForces - 799C】Fountains (线段树 或 树状数组,类似二元组问题)

题干&#xff1a; Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are n available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cos…

java 前后的区别_java中前后++的区别

java中前后的区别发布时间&#xff1a;2020-06-22 14:38:22来源&#xff1a;亿速云阅读&#xff1a;134作者&#xff1a;Leah这篇文章将为大家详细讲解有关java中前后的区别&#xff0c;小编觉得挺实用的&#xff0c;因此分享给大家做个参考&#xff0c;希望大家阅读完这篇文章…

【CodeForces - 735A 】Ostap and Grasshopper (水题,模拟)

题干&#xff1a; On the way to Rio de Janeiro Ostap kills time playing with a grasshopper he took with him in a special box. Ostap builds a line of length n such that some cells of this line are empty and some contain obstacles. Then, he places his grassh…

java私有成员的访问_java – 使用私有成员或公共访问器的方法

我意识到这可能无法回答,但我正在寻找是否有关于是否直接使用私有成员或类方法中的公共访问器的某种指导.例如,考虑以下代码(在Java中,但在C中看起来非常相似)&#xff1a;public class Matrix {// Private Membersprivate int[][] e;private int numRows;private int numCols;…