|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
; \7 G8 O3 `( k( T+ h两个vector 去重复,相交,合并的函数分享
- _) k& [' b4 e! C
; \0 j% S" r: g1 d/ K3 b. ~" r[mw_shl_code=c,true]//容器vector中元素的去重 # }( M$ \& x5 |4 j
vector<int> unique_element_in_vector(vector<int> v){ 1 e- I, {0 H, V7 i
vector<int>::iterator vector_iterator; 0 T9 p6 L7 R* R' w9 o
sort(v.begin(),v.end()); # ^( h/ ?0 ? I2 f% Q
vector_iterator = unique(v.begin(),v.end());
! A9 @( W0 s/ Z- G* O6 O1 I if(vector_iterator != v.end()){ ( e) U/ E& D- c" }8 [) z( E \7 N
v.erase(vector_iterator,v.end());
% L, H; ?+ N- g$ D1 U: K4 O } ) L. J8 L* F/ t6 Z3 d
return v; . z3 _* U3 Q+ B
}
@1 @1 v; N; s1 w6 W- x% P ( t/ S' j6 I) e# a! v4 {
//两个vector求交集 1 g9 L7 z; i& W" p* F
vector<int> vectors_intersection(vector<int> v1,vector<int> v2){
; Z- {# M; j$ L7 [1 V vector<int> v; ) b* m0 m% Z" q4 b) a' ^; a
sort(v1.begin(),v1.end()); ! F/ b6 Q/ d5 z7 a8 V( C/ o
sort(v2.begin(),v2.end()); * l4 J& L2 R* u7 @
set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集 6 G+ \5 ?/ A: m5 B5 o9 n4 T* s
return v;
9 V) r, @3 \" c+ E, {} 3 O3 y0 c& D1 z# ?$ d: `
! [( Z; m5 X ^//两个vector求并集
3 M) j. J2 o: i8 z$ G% {' c) ?) Rvector<int> vectors_set_union(vector<int> v1,vector<int> v2){
" P- r, f4 T8 U ~- h vector<int> v; 8 {9 i2 S- V- B4 V# n
sort(v1.begin(),v1.end()); ( N, `/ C* x( }! A9 x% ^
sort(v2.begin(),v2.end());
4 n2 L2 O3 ^2 m6 ^ set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集 " I6 k5 d' l; p
return v; 9 p- R7 m0 s. v8 l3 f
}
& ~; s p4 b( b" v9 Y$ x 8 p. X) E- }1 @) k( o
//判断vector的某一元素是否存在
/ w: ^" F! L; J* Jbool is_element_in_vector(vector<int> v,int element){ 6 Y8 Y- z: A5 p) F7 V4 v6 w
vector<int>::iterator it;
4 c7 f& J/ v0 d7 c6 |% {8 j) T( ^7 [ it=find(v.begin(),v.end(),element); w% X# Z5 ]) I9 Y1 l
if (it!=v.end()){ 9 o6 @: C: z' x2 H& i
return true; 9 T5 `. e8 t5 s+ F3 k D
} % ^) U: X' v2 y6 \7 n1 f% f
else{
# O, S, h, p# d return false; 4 i$ a5 w, x# T2 U9 Y5 z
} # u" p. d* X' B
} 1 Z2 ?* P" G( G- Y$ F: `+ A
[/mw_shl_code]* ^- o' y& r- J8 w8 ] |
|
|