#include "iostream"
#include "vector"
#include "algorithm" //sort函数、交并补函数
#include "iterator" //求交并补使用到的迭代器 using namespace std; //打印容器vector
void print_vector(vector<string> v) {
if (v.size() > 0) {
cout << "{";
for (int i = 0; i < int(v.size()); i++) {
cout << v[i] << ",";
}
cout << "\b}";
} else {
cout << "{}";
}
} //容器vector中元素的去重
vector<string> unique_element_in_vector(vector<string> v) {
vector<string>::iterator vector_iterator;
sort(v.begin(), v.end());
vector_iterator = unique(v.begin(), v.end());
if (vector_iterator != v.end()) {
v.erase(vector_iterator, v.end());
}
return v;
} //两个vector求交集
vector<string> vectors_intersection(vector<string> v1, vector<string> v2) {
vector<string> v;
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v));//求交集
return v;
} //两个vector求并集
vector<string> vectors_set_union(vector<string> v1, vector<string> v2) {
vector<string> v;
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v));//求交集
return v;
} //判断vector的某一元素是否存在
bool is_element_in_vector(vector<string> v, string element) {
vector<string>::iterator it;
it = find(v.begin(), v.end(), element);
if (it != v.end()) {
return true;
} else {
return false;
}
} int trim_z(std::string &s) {
if (s.empty()) {
return 0;
}
s.erase(0, s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1); return 1;
} string trim(string &s) {
string str;
if (!s.empty()) {
s.erase(0, s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
}
str = s;
return str;
} void test() {
Json::Value qr_result; //"猴子尾巴", "猴子尾巴", "松鼠尾巴"
//"松鼠尾巴", "猴子尾巴", "猴子尾巴"
vector<string> qr_text;
string s1 = "松鼠尾巴";
string s2 = "猴子尾巴";
string s3 = "猴子尾巴 ";
string text = " 7ter 09, jdhfd iere*- ddw jjdjjdj ";
// trim_z(text); s3 = trim(s3);
// cout << "text==>>" << text << endl;
cout << "s3==>>" << s3 << endl; qr_text.emplace_back(s1);
qr_text.emplace_back(s2);
qr_text.emplace_back(s3); const int s = qr_text.size();
int tempTimes = 0;
Json::Value items;
string name;
for (int j = 0; j < s; ++j) {
string item = qr_text[j];
if (item != "二维码识别失败") {
int times = count(qr_text.begin(), qr_text.end(), item);
cout << "times==>" << times << endl;
if (times > tempTimes) {
tempTimes = times;
name = item;
tempTimes++;
}
}
items.append(item);
}
//封装返回的json信息
qr_result["name"] = name;
qr_result["nname"] = items;
qr_result["score"] = items.size();
qr_result["function"] = "QRcoderecognition";
cout << "json==>>" << qr_result.toStyledString() << endl;
} int main() {
// jiaoji();
// test();
vector<string> vc1{"猴子尾巴", "猴子尾巴", "猴子耳朵", "松鼠脸"};
vector<string> vc2{"猴子尾巴", "猴子耳朵", "松鼠尾巴"};
vector<string> vec; cout << "求v1与v2的交集:";
vec = vectors_intersection(vc1, vc2);
print_vector(vec);
cout << endl;
cout << "求v1与v2的并集:";
vec = vectors_set_union(vc1, vc2);
print_vector(vec);
return 0;
}

[c++]对vector<T>容器求交集,并集,去重的更多相关文章

  1. SQL求 交集 并集 差集

    故事是这样的….. 故事情节: 表 tb_test 有两列, colA , colB; 求 colA , colB 的并交差集… -- 计算并集 SELECT DISTINCT colB FROM t ...

  2. stl set求交集 并集 差集

    #include <iostream>#include <set> using namespace std; typedef struct tagStudentInfo{  i ...

  3. 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 ...

  4. 如何求ArrayList集合的交集 并集 差集 去重复并集

    需要用到List接口中定义的几个方法: addAll(Collection<? extends E> c) :按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾 ...

  5. 【C++】Vector判断元素是否存在,去重,求交集,求并集

    1 #include <iostream> 2 #include <vector> 3 #include <algorithm> //sort函数.交并补函数 4 ...

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

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

  7. Python 求两个文本文件以行为单位的交集 并集 差集

    Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...

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

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

  9. js求对象数组的交集/并集/差集/去重

    1.求交集 var arr1 = [{name:'name1',id:1},{name:'name2',id:2},{name:'name3',id:3}]; var arr1Id = [1,2,3] ...

随机推荐

  1. js 如何全部替代一个子串为另一个子串

    更多描述: 假设有一个字符串 `hello. hello. hello. ` 需要替换为 `AAA`,即把 `hello. ` 替换为 `A` 如果需要全量替换字符串,可以使用 String.prot ...

  2. [web安全] 利用pearcmd.php从LFI到getshell

    有一段时间没写blog了,主要是事多,加上学的有些迷茫,所以内耗比较大.害,沉下心好好学吧. 漏洞利用背景: 允许文件包含,但session等各种文件包含都已经被过滤了.ctf题中可以关注regist ...

  3. android Paint 详解

    /**     * Paint类介绍 * * Paint即画笔,在绘图过程中起到了极其重要的作用,画笔主要保存了颜色, * 样式等绘制信息,指定了如何绘制文本和图形,画笔对象有很多设置方法, * 大体 ...

  4. RAC中常见的高级用法-过滤

    filter      过滤信号,使用它可以获取满足条件的信号. - (void)filter { //只有当我们文本框内容长度大于5才想要获取文本框的内容 [[_passWord.rac_textS ...

  5. 找出1小时内占用cpu最多的10个进程的shell脚本

    cpu时间是一项重要的资源,有时,我们需要跟踪某个时间内占用cpu周期最多的进程.在普通的桌面系统或膝上系统中,cpu处于高负荷状态也许不会引发什么问题.但对于需要处理大量请求的服务器来讲,cpu是极 ...

  6. 学习 27 门编程语言的长处,提升你的 Python 代码水平

    Python猫注:Python 语言诞生 30 年了,如今的发展势头可谓如火如荼,这很大程度上得益于其易学易用的优秀设计,而不可否认的是,Python 从其它语言中偷师了不少.本文作者是一名资深的核心 ...

  7. CPU中的上下文

    目录 一.简介 二.进程切换 三.线程切换 四.中断切换 五.中断检测和查看 六.模拟 一.简介 Linux是多任务操作系统,cpu划分固定时间片,分给每个进程,当前进程时间片执行完毕,将挂起,运行下 ...

  8. inode节点

    目录 一.简介 二.信息 inode的内容 inode的大小 3.inode号码 三.目录文件 四.硬连接 五.软链接 六.inode的特殊作用 一.简介 理解inode,要从文件储存说起. 文件储存 ...

  9. h5文件下载

    // type1 await getFile(fileUrl).then((res) => { console.log('download',res); let bFile = window.U ...

  10. 在JSP页面里,时间控件的JS位置在下面然后就显示不出来

    在JSP页面里,时间空间的JS位置在下面然后就显示不出来,放到前面然后就显示出来了, 情何以堪啊,开始还以为是什么错误.