|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
) b e; }$ X, r& r- _两个vector 去重复,相交,合并的函数分享' ?2 f# [; T" k9 ]6 z
/ c" t5 O( o$ a ]1 l[mw_shl_code=c,true]//容器vector中元素的去重
0 s8 [" x1 B7 u5 s1 S1 Lvector<int> unique_element_in_vector(vector<int> v){
' r4 |7 Y/ _( l$ N, s7 p: @ vector<int>::iterator vector_iterator; 8 Y% w$ q H% G
sort(v.begin(),v.end()); : }( B1 J/ O, b3 P+ X6 a( D& h
vector_iterator = unique(v.begin(),v.end());
4 x- y- }# A5 y7 V9 @3 h# {- U if(vector_iterator != v.end()){
' G' ?2 G) U8 {8 ^) M v.erase(vector_iterator,v.end()); / {: j* K9 }2 v* C- S" a( X
}
2 V `% u3 v0 x5 W return v; . V( M9 c3 H& D6 k9 J/ d: Y
} 8 s @3 `/ W( {
4 ~. I. u' t3 B( N//两个vector求交集
9 p. {! g) Q! hvector<int> vectors_intersection(vector<int> v1,vector<int> v2){
_+ {% H" `; y- N7 ?5 }/ M vector<int> v; / M# Z- x" D! J# C6 n6 M5 Y% u
sort(v1.begin(),v1.end());
W! u6 U- a( x. V% |+ ? sort(v2.begin(),v2.end());
4 T* t6 n% r I2 @' ^" q. d set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集 7 {; r- @/ `; m: T- P' h4 b
return v;
9 h; x$ g6 _9 X2 A} ! c7 ~0 f' t: A4 o0 h
; m J4 |0 F( o: ^, a
//两个vector求并集 * ^8 z! B+ G w/ u8 H" i
vector<int> vectors_set_union(vector<int> v1,vector<int> v2){
: F9 J% j- j, `1 B C+ X7 R vector<int> v;
0 D8 E- ]. Q9 Y1 W, | sort(v1.begin(),v1.end());
/ W$ X( C W5 y3 J8 y1 _$ p sort(v2.begin(),v2.end()); ! I9 I5 H' q- [
set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集 0 `4 p, I' E6 f* `* l8 j2 P
return v;
- G p1 }2 {& }} 1 Y6 B; W8 D" P3 _* w9 L) ~4 o
- P; c* c# b( S% D% W; ]: ]( o# }//判断vector的某一元素是否存在 ! o* m- i8 s2 m7 a
bool is_element_in_vector(vector<int> v,int element){ ; w1 S% e0 k( r! i2 _. x
vector<int>::iterator it;
% ~9 }. b0 n' J8 K* D1 h it=find(v.begin(),v.end(),element); 1 _, ~- i) a$ q+ }$ |6 L
if (it!=v.end()){ ( ^5 q* f: \% Y/ }4 J9 q2 e
return true; 8 Y( W* ]3 B; z$ ^) V
}
+ P0 S! v( |# n, [0 ? else{
5 h( ?) l% o" @2 H2 C1 \ return false; l4 n6 D: X3 H5 c$ i d
}
- K& d0 _" G5 S! V: z}
3 A% `7 R5 C( R( J& E0 b( |& Z' H [/mw_shl_code]$ e* y, L5 [- ~ M. V
|
|