|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
# O, o1 n" p: k2 o
两个vector 去重复,相交,合并的函数分享
0 v: b! u. S$ G1 f* e
# J( B" j; C# d# n2 @# h[mw_shl_code=c,true]//容器vector中元素的去重
/ i) ~" q3 Y4 Tvector<int> unique_element_in_vector(vector<int> v){
% n7 J7 k7 M" E0 G( v# P1 w vector<int>::iterator vector_iterator;
( J7 V9 k$ [2 ~1 B; l, {/ \ sort(v.begin(),v.end());
; I2 s0 X" ^5 F& J vector_iterator = unique(v.begin(),v.end());
6 ?# U2 W4 X: m, K if(vector_iterator != v.end()){ 4 I0 D: x3 A: s$ D
v.erase(vector_iterator,v.end());
" [; ~* C* \9 t; ^ } + A- H6 V3 s, O* `
return v; & E d$ |. ]0 I* v( Q
}
/ x3 P% j& J/ {8 b& a9 G
: Q R* S& ^) h$ I" w6 ]//两个vector求交集 : S5 D. Z7 Z% K4 I
vector<int> vectors_intersection(vector<int> v1,vector<int> v2){ ! Z) c* o+ l, F; J. |
vector<int> v; 1 O- S7 a7 w$ r* l
sort(v1.begin(),v1.end());
" ~( H. Y! d4 E+ ?7 d$ t: v sort(v2.begin(),v2.end()); N+ E- x1 f/ w8 u K; @8 y
set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
4 q; r# k0 L* x- y return v;
' \" V* X# d' s} ! v3 a4 ^8 p9 S$ Y9 E+ }
# c0 w; d3 t" Z3 i1 Q' T
//两个vector求并集 : r$ _- i5 i9 Q! e
vector<int> vectors_set_union(vector<int> v1,vector<int> v2){ ( H; |9 }" W) j# l% M. P0 Y
vector<int> v;
% }; P9 h4 J5 V1 {6 k2 T sort(v1.begin(),v1.end());
/ J- _: V1 h2 _ sort(v2.begin(),v2.end());
8 w) u; q0 T8 @, Q set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
9 k7 F$ ]5 \8 K2 Z& ]0 F8 y" h return v; [8 c3 E* S8 U3 y
}
/ X" G1 B* }5 |6 w ! v% l0 {- J; O+ p
//判断vector的某一元素是否存在 2 z! `. K; \( t9 F% F$ z: h" E2 r7 V5 t
bool is_element_in_vector(vector<int> v,int element){ + t1 D! @: q1 h+ s
vector<int>::iterator it;
4 D7 z2 J1 S& z6 G+ B5 y( @ c it=find(v.begin(),v.end(),element);
+ q6 A9 f/ `! g% H! f if (it!=v.end()){ 4 H4 l- A+ @% q9 f2 `% S6 A' M9 \1 b
return true; ' k6 F3 t5 Y+ a0 R' S
}
: u5 w6 B" c& b7 l6 B* j4 Z5 P9 A7 a else{ # J2 q! L6 U* z; H7 _+ s
return false; ; F1 L7 w; _% b6 c7 I
}
9 `* B a" C: a+ k" L} F7 [3 O$ L: f: @7 e' Y0 J' z" \
[/mw_shl_code]" q3 e7 T' \2 u9 A. A* @
|
|