1 #include <iostream>
2 #include <vector>
3 #include <algorithm> //sort函数、交并补函数
4 #include <iterator> //求交并补使用到的迭代器
5 using namespace std;
6
7 //打印容器vector
8 void print_vector(vector<int> v)
9 {
10 if(v.size()>0)
11 {
12 cout<<"{";
13 for(int i=0;i<int(v.size());i++)
14 {
15 cout<<v[i]<<",";
16 }
17 cout<<"\b}";
18 }
19 else{
20 cout<<"{}";
21 }
22 }
23
24 //容器vector中元素的去重
25 vector<int> unique_element_in_vector(vector<int> v)
26 {
27 vector<int>::iterator vector_iterator;
28 sort(v.begin(),v.end());
29 vector_iterator = unique(v.begin(),v.end());
30 if(vector_iterator != v.end())
31 {
32 v.erase(vector_iterator,v.end());
33 }
34 return v;
35 }
36
37 //两个vector求交集
38 vector<int> vectors_intersection(vector<int> v1,vector<int> v2)
39 {
40 vector<int> v;
41 sort(v1.begin(),v1.end());
42 sort(v2.begin(),v2.end());
43 set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
44 return v;
45 }
46
47 //两个vector求并集
48 vector<int> vectors_set_union(vector<int> v1,vector<int> v2)
49 {
50 vector<int> v;
51 sort(v1.begin(),v1.end());
52 sort(v2.begin(),v2.end());
53 set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
54 return v;
55 }
56
57 //判断vector的某一元素是否存在
58 bool is_element_in_vector(vector<int> v,int element)
59 {
60 vector<int>::iterator it;
61 it=find(v.begin(),v.end(),element);
62 if (it!=v.end())
63 {
64 return true;
65 }
66 else{
67 return false;
68 }
69 }
70
71 int main()
72 {
73 vector<int> v1,v2,v;
74 v1.push_back(22);v1.push_back(22);v1.push_back(23);v2.push_back(23);v2.push_back(24);
75 cout<<"v1是否存在1这个元素?"<<is_element_in_vector(v1,1)<<endl;
76 cout<<"对v1去重:";
77 v1=unique_element_in_vector(v1);
78 print_vector(v1);
79 cout<<endl;
80 cout<<"求v1与v2的交集:";
81 v=vectors_intersection(v1,v2);
82 print_vector(v);
83 cout<<endl;
84 cout<<"求v1与v2的并集:";
85 v=vectors_set_union(v1,v2);
86 print_vector(v);
87 return 0;
88 }

转自:https://www.cnblogs.com/mayouyou/p/8921598.html 感谢博主!

【C++】Vector判断元素是否存在,去重,求交集,求并集的更多相关文章

  1. JS数组操作:去重,交集,并集,差集

    原文:JS数组操作:去重,交集,并集,差集 1. 数组去重 方法一: function unique(arr) { //定义常量 res,值为一个Map对象实例 const res = new Map ...

  2. 重学ES系列之Set实现数组去重、交集、并集、差集

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. [c++]对vector<T>容器求交集,并集,去重

    #include "iostream" #include "vector" #include "algorithm" //sort函数.交并 ...

  4. python 两个list 求交集,并集,差集

    def diff(listA,listB): #求交集的两种方式 retA = [i for i in listA if i in listB] retB = list(set(listA).inte ...

  5. Linux 两个文件求交集、并集、差集

    一.交集 sort a.txt b.txt | uniq -d 二.并集 sort a.txt b.txt | uniq 三.差集 a.txt-b.txt: sort a.txt b.txt b.tx ...

  6. java(List或Array数组)求交集、并集、差集, 泛型工具类

    业务需要求不同类型的交集.并集.差集为避免代码冗余编写工具类. 注:list 转数组需传入数组,如果将原数组传入将会改变原数组的值,同时泛型数组又不可以实例化,解决方案:Arrays.copyOf(n ...

  7. STL中set求交集、并集、差集的方法

    并集(http://zh.cppreference.com/w/cpp/algorithm/set_union) 交集(http://zh.cppreference.com/w/cpp/algorit ...

  8. javascript 数组求交集/差集/并集/过滤重复

    最近在小一个小程序项目,突然发现 javscript 对数组支持不是很好,连这些基本的功能,都还要自己封装.网上查了下,再结合自己的想法,封装了一下,代码如下. //数组交集 Array.protot ...

  9. java集合运算:求交集,并集,集合差

    今天突然想用Java实现如何用集合实现交集,并集和差集的运算了!主要是看Python语言的时候想起来的. 实现主要使用的Set集合,Set集合的特点是集合内的元素不可重复. 具体代码如何: packa ...

随机推荐

  1. 百度地图api逆地址解析 PHP

    一.说明:逆地址查询就是根据经纬度信息获取地址位置信息 二.参数:$lat:纬度值 ,$lng:经度值 ,$ak = 自己的AK:(百度地图开放平台对应ak链接:http://lbsyun.baidu ...

  2. 【实用小技巧】freemarker模板中文乱码问题解决

    freemarker简单模板如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content- ...

  3. CVPR2021| TimeSformer-视频理解的时空注意模型

    前言: transformer在视频理解方向的应用主要有如下几种实现方式:Joint Space-Time Attention,Sparse Local Global Attention 和Axial ...

  4. 微信小程序中的常见弹框

    显示加载中的提示框 wx.showLoading() 当我们正在在进行网络请求时,常常就需要这个提示框 手动调用wx.hideLoading()方法才能够关闭这个提示框,通常在数据请求完毕时就应该关闭 ...

  5. ipmi配置方法-20200328

    ipmi配置错误-20200328[root@localhost home]# ipmitool lan set 1 ipsrc staticCould not open device at /dev ...

  6. 云计算OpenStack共享组件---信息队列rabbitmq(2)

    一.MQ 全称为 Message Queue, 消息队列( MQ ) 是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们. 消息传 ...

  7. 那些天,shell脚本中曾经踩过的坑

    前些天,需要实现一个需求,用脚本轮流kill服务器上的进程,观察内存变化情况,并写日志.脚本逻辑不难,但shell脚本好久不用,看过书里的语法都忘得差不多了,中间踩了不少的坑,特此记录一下,留作后续参 ...

  8. visual studio code 快捷键-(转自 浅笑千寻)

    Visual Studio Code之常备快捷键 官方快捷键大全:https://code.visualstudio.com/docs/customization/keybindings Visual ...

  9. Centos7.4 file '/grub/i386-pc/normal.mod' not found,实际为/boot下所有文件丢失

    注:如果服务器特别重要,此方案慎用.如果没有其他方案解决,可以使用该方案 事件:搭建在云计算管理平台CAS上的 Centos7.4 虚拟机在一次断电后,启动虚拟机出现file '/grub/i386- ...

  10. 常见判断错误 (Day_30)

    写给自己的话: 这是一个卡了我小半天的BUG,也是一个很低端的BUG,写篇博客吧,以后回来看看,会发现曾经的自己是如何的菜. 同样,以此记录我的进步 步入正题,这是我实现多条件分页时遇到的一个BUG, ...