|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
×
) r* i! \& c k7 w1 z1 J9 v* y
两个vector 去重复,相交,合并的函数分享( ?; J5 p+ s4 S( U8 U
_, Q0 i; }/ C# ~! s. P. F[mw_shl_code=c,true]//容器vector中元素的去重 ) z/ k. G, q' k, x1 A4 a! k( r) b
vector<int> unique_element_in_vector(vector<int> v){ . @2 P) U$ F! v; k3 r( C3 Z, m
vector<int>::iterator vector_iterator; , T% \$ \+ B# t* J" F+ D
sort(v.begin(),v.end()); _% y% Z1 m2 L2 e4 [) a
vector_iterator = unique(v.begin(),v.end()); [$ o8 `% D' [7 ~2 l+ H- v
if(vector_iterator != v.end()){
2 o) O* V5 H) B& Z; J v.erase(vector_iterator,v.end());
0 x* B. T: L2 b- U, t }
& r4 X1 H: H8 h return v;
% `- p d& l2 t' d( U}
9 p- Z9 g+ s3 @
. `, n! s; S% _. m. L. l, v//两个vector求交集 % V0 X# |& v% S* J, B6 w5 H, J/ R
vector<int> vectors_intersection(vector<int> v1,vector<int> v2){
1 e* @4 H2 L& j vector<int> v;
. A2 E' S( \( ]/ R% A1 `# A) p `2 P. ? sort(v1.begin(),v1.end()); 1 F1 A6 I' L. e6 P [) q6 {! j) v
sort(v2.begin(),v2.end()); 2 T) G8 k K# B; Q( b6 W
set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集 : q* h/ P& U: u W( Z( |9 x: w
return v;
: D. L! V* e% S$ o}
! v( ?2 l$ r/ K0 |6 A0 g2 G2 `8 _ % N! D( t# Y* u: ]$ ]6 z
//两个vector求并集 7 J8 O0 u: h" h, v
vector<int> vectors_set_union(vector<int> v1,vector<int> v2){
* A2 w! ^5 H( O7 n vector<int> v;
% k9 j0 K6 C) b! i: i sort(v1.begin(),v1.end());
7 O4 u- {- ]8 z T( r& Y4 c sort(v2.begin(),v2.end());
, q* D- E4 U) ~* h/ r' H" _" U s set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集 7 H, ~1 k2 p* V- S
return v;
2 e# r. m/ Z& S/ M; V} + h' K9 f6 f0 h- q/ E
( ^9 P+ r( o% v5 t( Y! ]/ R//判断vector的某一元素是否存在 % C5 N% z" v/ `; g9 H
bool is_element_in_vector(vector<int> v,int element){
' G7 i6 A" a4 Z; _7 @8 T; _& i vector<int>::iterator it; 4 k" C0 h2 t, h$ j( m- [
it=find(v.begin(),v.end(),element); ; e' R. S& r0 s, v8 _: L
if (it!=v.end()){
: c% s( u6 l9 H9 D return true;
1 X$ v& U$ n, }# W) t: C0 g2 D7 s }
3 C6 h% A* r ?% J1 m e& z else{
2 y- E0 ~/ X0 v, C return false;
$ r7 `+ U0 I& b6 L& _/ ]$ } }
3 y) G, `1 w0 g} % g( ?5 t! A5 s) a4 s4 ]& E
[/mw_shl_code]$ i; w; W1 T) r/ N
|
|