|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
t: l% f N" ^" L
两个vector 去重复,相交,合并的函数分享
" N: c; ?8 [+ x9 U, w# O C* ^5 w, V0 \$ t# E& x0 M
[mw_shl_code=c,true]//容器vector中元素的去重
. i" i7 w0 C8 S/ Q8 svector<int> unique_element_in_vector(vector<int> v){
4 }3 R, d+ q$ J, ?, `- V vector<int>::iterator vector_iterator; y! @1 H3 G- S9 Q4 ~4 ^3 }
sort(v.begin(),v.end()); $ }* p* o; n* \
vector_iterator = unique(v.begin(),v.end()); 1 T+ P9 q. R9 L% x
if(vector_iterator != v.end()){ # H! }! p* [' D$ y
v.erase(vector_iterator,v.end()); ( Z4 k( U2 c7 `. T# j8 x s
} ; ]+ q$ c( I$ N$ a. a) c* Q1 u
return v;
/ o9 ~- e# n1 A1 Q" w4 c. ?}
0 U; F( w9 S! F
. V1 j5 U% k/ d//两个vector求交集 . F6 x' \2 w7 t; k8 Q
vector<int> vectors_intersection(vector<int> v1,vector<int> v2){
/ e8 T9 m7 G) k2 U* b3 Z/ P vector<int> v; ) O. g/ C, c/ T4 N- y C( n, Z
sort(v1.begin(),v1.end()); ! S- z( y8 x1 p
sort(v2.begin(),v2.end());
1 G' G2 _7 ?6 V+ O1 _0 S set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集 # f+ Y" y# p5 }+ Q% Z
return v; + P$ c! ?6 Z6 _+ c
}
3 x$ T0 n) V( x, [* W
* r' A$ W8 f; K! b; j+ J//两个vector求并集
# F7 y# a9 V. x- qvector<int> vectors_set_union(vector<int> v1,vector<int> v2){ & a3 ?2 A9 i% v1 \4 \
vector<int> v;
' I: ]5 I' ^8 Z! Z sort(v1.begin(),v1.end()); 8 c" F7 q4 I- c( N. w
sort(v2.begin(),v2.end());
0 S! F" H. t3 P3 M: A set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
_5 ^- J3 g: {6 C& s) ~9 e m& S return v;
: S3 p( m# E; U" e} 4 z# K# i( w2 e$ g- q4 E
. @9 ?# c; v, V/ j
//判断vector的某一元素是否存在 4 K! E8 q, X0 Z) U% c" k
bool is_element_in_vector(vector<int> v,int element){ 9 s: q( S$ u3 D, i
vector<int>::iterator it;
' J: i: P& h& E' b0 K* ~& K it=find(v.begin(),v.end(),element);
& V) p7 O6 q, H, m+ F& a6 w if (it!=v.end()){
, p/ e5 J: I. U return true; ) ~% X5 j) s! }" x+ `$ k% ?
}
- T6 o) ^: G1 s+ F* f- I, \ else{ ! b1 L; Y5 ]8 Y" \' g+ m
return false;
% f, r: Z3 y$ r7 f& L1 @. _& t } ( o$ J# ?) p' f, @1 u- Z: n+ q0 n
}
x& H; Y. y9 i) } [/mw_shl_code]; X2 P8 }2 L+ G# ~( S) t s
|
|