PLM之家PLMHome-工业软件与AI结合践行者

[资料分享] 两个vector 去重复,相交,合并的函数分享

[复制链接]

2018-4-3 07:56:54 3239 0

admin 发表于 2018-4-3 07:56:54 |阅读模式

admin 楼主

2018-4-3 07:56:54

请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!

您需要 登录 才可以下载或查看,没有账号?注册

x

0 |' c  Z: t) H, h5 M; K两个vector 去重复,相交,合并的函数分享' h; j6 \/ `+ Y8 F/ m% b3 G8 j

) L: y' {7 Z# |7 W% @, W[mw_shl_code=c,true]//容器vector中元素的去重  
) T6 ]; N4 z2 F& z5 J, Evector<int> unique_element_in_vector(vector<int> v){  3 s$ B. z7 H5 E" y
    vector<int>::iterator vector_iterator;  8 y) L5 {$ |0 I5 r2 F% e  o7 H
    sort(v.begin(),v.end());  
) w/ a5 `4 \0 Y3 l6 }8 F0 b    vector_iterator = unique(v.begin(),v.end());  
9 d6 U" F- D) w    if(vector_iterator != v.end()){  
* }/ l8 N3 u1 N3 t& z        v.erase(vector_iterator,v.end());  6 h- ?, Y: `+ F' A; [$ s
    }  : G+ f9 \8 J  |4 I8 z9 s
    return v;  
4 r! G) M; N! U8 W! {: q}  
* w$ G5 U* q0 X6 G2 D4 x4 G% C1 d  
  u9 k# x* i# q( P//两个vector求交集  
+ T! W5 x# g7 V( n! E% pvector<int> vectors_intersection(vector<int> v1,vector<int> v2){  ) }3 H% t& l1 U
    vector<int> v;  , U8 g" K& a2 n; V# V
    sort(v1.begin(),v1.end());     5 Q" O( O* x) T& x6 o9 F+ E) I
    sort(v2.begin(),v2.end());     
! R* y8 n( J1 u2 B2 @. b* o$ `    set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集   7 O5 i7 s9 @; {* a$ q( k
    return v;  
5 N) v0 C4 `0 ~! N5 D}  ; T" i# s5 t1 i3 C  C
  $ @8 e6 }" o- s
//两个vector求并集  ' c% t' c' n  \+ n
vector<int> vectors_set_union(vector<int> v1,vector<int> v2){  
( d# ~8 v4 p5 ]/ G( g. @6 c; m$ u    vector<int> v;  % o/ h' m. u- E
    sort(v1.begin(),v1.end());     4 Q6 D& I( g" g- p5 X
    sort(v2.begin(),v2.end());     - B$ n: v, U9 R+ f
    set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集   . w5 y; H( G# b# T
    return v;  
1 Y3 a: D' y* A6 g* `/ o) J}  
' I, Z, d% Z$ Q& s- r1 r2 k6 V- d$ K  4 o% x, v5 y# |. J
//判断vector的某一元素是否存在  
! T' L+ X( B0 k& @bool is_element_in_vector(vector<int> v,int element){  
- u5 W" m' X" E+ U/ Q0 b8 Q! q    vector<int>::iterator it;  
' N, n. W6 Z1 x! U7 O    it=find(v.begin(),v.end(),element);  
* I  V) d5 t, S4 x( I. o2 R4 Z4 s/ W    if (it!=v.end()){  & h' J+ y: L9 S
        return true;  
1 N% V  F: Y- F/ g    }  / p, P" \! ~7 g- w3 @# V( z/ _5 ^7 K
    else{  
% M- o  v) g/ {        return false;  
; m) K  K0 Q2 h    }  : {. M' E0 K) L6 N
}  
3 g, i9 [7 D# X/ c) g7 @" K3 {  [/mw_shl_code]
- h! h$ K: h! J' |0 @" f2 G) A7 q
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 doTeam.tech
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 注册

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了