|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
q7 A$ L" e1 y; S- V+ a# p两个vector 去重复,相交,合并的函数分享" }+ [6 Y' N3 M; D5 B
; T/ a3 s( |0 L! R4 C; P
[mw_shl_code=c,true]//容器vector中元素的去重
# m+ ]* U: C+ b* bvector<int> unique_element_in_vector(vector<int> v){
- Z; C0 z9 l, g" v, C, j$ |4 S vector<int>::iterator vector_iterator;
1 H4 L% q) N8 _ sort(v.begin(),v.end());
: r% E# ~5 m5 ^0 N6 B b- x& } vector_iterator = unique(v.begin(),v.end());
/ F v4 T, a) d& b9 W F if(vector_iterator != v.end()){ 9 J# `4 @ ?# K- Q8 h+ R- U: {
v.erase(vector_iterator,v.end()); # e7 p6 y7 Q+ r/ p" H
} * I! |7 s& @( z/ r
return v; 5 B7 Y& t% n6 d9 n0 n* ~, U i) ?7 f
}
( b) A6 f. F9 m2 J " N3 B% l. f0 I% R4 Y( a1 B1 O1 R2 W
//两个vector求交集
9 f2 b) H7 ]/ h& I3 Evector<int> vectors_intersection(vector<int> v1,vector<int> v2){ ! V H( R, Z9 F# i( t' ^( _
vector<int> v;
; _9 Q% T& I. F3 i$ y0 G& r l sort(v1.begin(),v1.end());
9 f( u. U% Q. O- o f5 r sort(v2.begin(),v2.end()); ! k# _/ X# E) n! n V
set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
% \5 d% O7 e1 O7 Y; _6 e' L. @+ a return v; * |2 p/ {- W! K. i
}
3 u; S/ Z0 e B+ n
8 [( \7 \, t- M. j5 J9 u$ f+ l, H//两个vector求并集 & }2 K @+ T8 D2 J
vector<int> vectors_set_union(vector<int> v1,vector<int> v2){
, K, F/ R) ] z! y vector<int> v; 9 m4 D3 h0 t7 [) m: C- V+ k8 n
sort(v1.begin(),v1.end()); % A, ]% ]8 L8 y1 h/ l
sort(v2.begin(),v2.end()); , \5 {1 J6 r. ]( Z: n7 R7 i
set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集 9 o5 R3 I3 `, U9 S1 I$ g
return v;
2 S9 l- F/ n- e. o/ v M}
" F$ d* r0 k6 B. q6 h # i6 e7 M3 s: n* h) |
//判断vector的某一元素是否存在 * G4 S D% v$ b1 x# q8 ]% }3 R
bool is_element_in_vector(vector<int> v,int element){ 4 E3 W9 O' l- s$ |7 K! @
vector<int>::iterator it;
4 x5 g' H" f% V' M* Y, [# }# H+ G+ u it=find(v.begin(),v.end(),element);
1 X8 @' w: d3 t if (it!=v.end()){
$ _7 B, d: p0 R u' x return true; 0 d! X C2 M0 X9 x( Y
}
& x9 d6 N- c/ O# j/ H else{
% ~1 a5 `( A- ~2 G6 P' x; c return false;
: m: P; y; w, l7 l' [/ T0 B }
2 Q: @* f0 } I/ Q! ^$ g}
. \3 O3 I& L0 s0 [3 k) e! y- s# F3 | [/mw_shl_code]" i/ W& T6 T5 Y$ H& Z) p/ p6 e
|
|