【C++】Vector判断元素是否存在,去重,求交集,求并集
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判断元素是否存在,去重,求交集,求并集的更多相关文章
- JS数组操作:去重,交集,并集,差集
原文:JS数组操作:去重,交集,并集,差集 1. 数组去重 方法一: function unique(arr) { //定义常量 res,值为一个Map对象实例 const res = new Map ...
- 重学ES系列之Set实现数组去重、交集、并集、差集
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [c++]对vector<T>容器求交集,并集,去重
#include "iostream" #include "vector" #include "algorithm" //sort函数.交并 ...
- python 两个list 求交集,并集,差集
def diff(listA,listB): #求交集的两种方式 retA = [i for i in listA if i in listB] retB = list(set(listA).inte ...
- 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 ...
- java(List或Array数组)求交集、并集、差集, 泛型工具类
业务需要求不同类型的交集.并集.差集为避免代码冗余编写工具类. 注:list 转数组需传入数组,如果将原数组传入将会改变原数组的值,同时泛型数组又不可以实例化,解决方案:Arrays.copyOf(n ...
- STL中set求交集、并集、差集的方法
并集(http://zh.cppreference.com/w/cpp/algorithm/set_union) 交集(http://zh.cppreference.com/w/cpp/algorit ...
- javascript 数组求交集/差集/并集/过滤重复
最近在小一个小程序项目,突然发现 javscript 对数组支持不是很好,连这些基本的功能,都还要自己封装.网上查了下,再结合自己的想法,封装了一下,代码如下. //数组交集 Array.protot ...
- java集合运算:求交集,并集,集合差
今天突然想用Java实现如何用集合实现交集,并集和差集的运算了!主要是看Python语言的时候想起来的. 实现主要使用的Set集合,Set集合的特点是集合内的元素不可重复. 具体代码如何: packa ...
随机推荐
- 【js】Leetcode每日一题-数组异或操作
[js]Leetcode每日一题-数组异或操作 [题目描述] 给你两个整数,n 和 start . 数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == ...
- Wampserver-删除虚拟主机
对hosts操作 到目录C:\Windows\System32\drivers\etc中修改hosts 比如你想删除iwh2.com 选中这2行进行删除,保存退出 对httpd-vhosts操作 到目 ...
- JavaWeb——MySQL约束
内容索引 1. DQL:查询语句 1. 排序查询 2. 聚合函数 3. 分组查询 4. 分页查询 2. 约束 3. 多表之间的关系 4. 范式 5. 数据库的备份和还原 DQL:查询语句 1. 排序查 ...
- opencv——机器视觉检测和计数
引言 在机器视觉中,有时需要对产品进行检测和计数.其难点无非是对于产品的图像分割. 由于之前网购的维生素片,有时候忘了今天有没有吃过,就想对瓶子里的药片计数...在学习opencv以后,希望实现对于维 ...
- 【开源技术分享】无需流媒体服务,让浏览器直接播放rtsp/rtmp的神器:EasyMedia
不同于市面上其他需要各种转发到流媒体服务的中间件来说,EasyMedia不需要依赖任何nginx-rtmp,srs,zlmediakit等等第三方流媒体服务,只需要你有rtsp或者rtmp等等协议的视 ...
- 性能工具 stream 最新版本5.10 The STREAM benchmark
官网下载最新性能工具 stream 最新版本5.10 https://github.com/jeffhammond/STREAM 官网下载最新性能工具 stream 最新版本5.10 http:/ ...
- Linux shell脚本全面学习(一)
1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在 ...
- 文字闪烁效果 CSS + HTML
文字闪烁效果 CSS 写在前面 好好学习,天天向上! 效果图 绝美的效果 实现过程 先给没字体添加一些普通的样式,颜色设置为透明 给文字设置一个动画效果,通过text-shadow属性来实现变亮的效果 ...
- String 是一个奇怪的引用类型
开局两张图,内容全靠刷! 马甲哥看到这样的现象,一开始还是有点懵逼. 这个例子,string是纯粹的引用类型,但是在函数传值时类似于值传递: 我之前给前后示例的内存变化图吧: 根因就是大多数高级语言都 ...
- 【odoo14】【用户侧】权限配置
以下内容仅适用于odoo的客户,不适用于开发人员. 下文介绍中涉及的概念及UI均是在odoo14社区版中进行. 目录 一. odoo中的对象 二. 权限控制 2.1 实现原理 2.2 UI方式实现权限 ...