[c++]对vector<T>容器求交集,并集,去重
#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>容器求交集,并集,去重的更多相关文章
- SQL求 交集 并集 差集
故事是这样的….. 故事情节: 表 tb_test 有两列, colA , colB; 求 colA , colB 的并交差集… -- 计算并集 SELECT DISTINCT colB FROM t ...
- stl set求交集 并集 差集
#include <iostream>#include <set> using namespace std; typedef struct tagStudentInfo{ i ...
- 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 ...
- 如何求ArrayList集合的交集 并集 差集 去重复并集
需要用到List接口中定义的几个方法: addAll(Collection<? extends E> c) :按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾 ...
- 【C++】Vector判断元素是否存在,去重,求交集,求并集
1 #include <iostream> 2 #include <vector> 3 #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 ...
- Python 求两个文本文件以行为单位的交集 并集 差集
Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...
- java(List或Array数组)求交集、并集、差集, 泛型工具类
业务需要求不同类型的交集.并集.差集为避免代码冗余编写工具类. 注:list 转数组需传入数组,如果将原数组传入将会改变原数组的值,同时泛型数组又不可以实例化,解决方案:Arrays.copyOf(n ...
- js求对象数组的交集/并集/差集/去重
1.求交集 var arr1 = [{name:'name1',id:1},{name:'name2',id:2},{name:'name3',id:3}]; var arr1Id = [1,2,3] ...
随机推荐
- Spark On Yarn的各种Bug
今天将代码以Spark On Yarn Cluster的方式提交,遇到了很多很多问题.特地记录一下. 代码通过--master yarn-client提交是没有问题的,但是通过--master yar ...
- 从面试官的角度,聊聊java面试流程
在这篇回答里,就讲以我常规的面试流程为例,说下java方面大致会问什么问题,以及如何确认候选人达到招聘要求. 先说面试前准备,可能有些面试官是拿到简历直接问,而且是在候选人自我介绍时再草草浏览简历,但 ...
- 优化if else嵌套代码
写在前面 不知大家有没遇到过像"横放着的金字塔"一样的if else嵌套: if (true) { if (true) { if (true) { if (true) { if ( ...
- Linux学习 - 权限管理命令
一.chmod(change the permissions mode of a file) 1 功能 改变文件或目录权限 root 与 所有者 可进行此操作 2 语法 chmod [(ugoa) ...
- Linux基础命令---ab测试apache性能
ab ab指令是apache的性能测试工具,它可以测试当前apache服务器的运行性能,显示每秒中可以处理多少个http请求. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.F ...
- Oracle bulk collect into 的几种用法
bulk collect 和 forall 联合应用写起来显得有些啰嗦,不过为了速度,多写两句又何妨 建立两个临时表 create table T_TEST ( TESTID NUMBER(19) n ...
- Playing with Destructors in C++
Predict the output of the below code snippet. 1 #include <iostream> 2 using namespace std; 3 4 ...
- OpenStack之一:初始化环境
初始化环境必须在左右节点执行 #:注意node节点要使用7.2 #: 关闭NetworkManager [root@localhost ~]# systemctl stop NetworkManage ...
- Maven配置大全
maven项目打jar包(带依赖) <build> <plugins> <plugin> <artifactId>maven-assembly-plug ...
- 1.RabbitMQ
1.RabbitMq是什么? MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced Message Queue 高级消息队 ...