|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
4 Y! D- t# d: Z" o1 Q& l7 v两个vector 去重复,相交,合并的函数分享( y4 P% L9 h% G: l6 g
" ?4 O+ K6 p" U9 s. U9 O, z& [
[mw_shl_code=c,true]//容器vector中元素的去重
9 P9 f. N U) m+ T+ B) g6 rvector<int> unique_element_in_vector(vector<int> v){
5 a8 T$ w+ C; t- M8 a vector<int>::iterator vector_iterator;
1 H& e" e) t9 ^7 e sort(v.begin(),v.end());
* W1 J& N- k( e vector_iterator = unique(v.begin(),v.end());
. V6 A2 }2 P$ F- `' h2 M5 k if(vector_iterator != v.end()){ 8 r4 d) ?/ ?4 m0 y5 O8 x
v.erase(vector_iterator,v.end()); Y- U8 W# ^& Y6 x9 E. K# N
}
; f; l7 A6 f' L& { return v; . g# l, A+ r c! j5 ], r' x
} # U7 u, @. t t% V' [. P% D
6 ], z& ~; @) G2 k) H1 N# b//两个vector求交集
, ^& k+ b2 g# h9 X/ T: mvector<int> vectors_intersection(vector<int> v1,vector<int> v2){ 9 {: w2 w9 `# z8 G3 O) Z8 @7 _
vector<int> v; $ i& L A J- T. Q0 c) p: ~" o K
sort(v1.begin(),v1.end());
0 p7 e5 Q- X; ~2 E7 `# u0 i/ @ sort(v2.begin(),v2.end());
8 @. C: l8 K% z% p* {. _ set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集 + t' f2 }$ p+ w, N
return v; ; B1 W1 ~+ m0 d1 C% b2 {4 I' Z
}
! o5 v. [' i2 B" `7 D8 h i 0 }7 Q. X% t$ R
//两个vector求并集 - w* \7 H, q( u* f4 M7 i# D
vector<int> vectors_set_union(vector<int> v1,vector<int> v2){
- j, b( i& a0 r& d+ R% w vector<int> v;
* M. C9 m& ~5 z M sort(v1.begin(),v1.end());
, ?$ Z% S7 v9 F7 L+ |- q3 w1 q! y sort(v2.begin(),v2.end()); , g6 Q; |% p( F/ f4 _) j# d, x, y& C
set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
# z1 Q6 M0 u. G F8 ?% c1 M return v;
: u" t" n' u& Q) d; j}
5 T: A; l. W" M: c6 Y( E0 R - g) i) A3 n- m9 ^/ K- M0 f# v
//判断vector的某一元素是否存在 - q( ?7 T% T- y k, Y# u
bool is_element_in_vector(vector<int> v,int element){
! Q- G; ^* Y! ?: u3 k/ u vector<int>::iterator it;
" z4 w% J7 K9 `4 f it=find(v.begin(),v.end(),element);
$ E# Y; T, X" p6 U2 k if (it!=v.end()){ ' k X7 Y) ^8 @9 W
return true;
0 F/ v% b& I- K# }, B: Z0 B }
- y! _8 T* m" y# [ else{ , P7 o: T; ~& P' N5 [" J) _: W
return false; 0 V; B5 i9 N2 C* a: V. ]' a
}
0 O! |3 F1 y$ s2 t/ R} 7 P# [! C" i" x$ }: E) W3 m
[/mw_shl_code]! h; R( F9 M: l. l/ U# c( R
|
|