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

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

[复制链接]

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

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

admin 楼主

2018-4-3 07:56:54

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

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

x

9 a( O" N) g3 Y" K: j0 T  p两个vector 去重复,相交,合并的函数分享
; C( F, |; g- D* y
1 ]' i" O& [$ m( ][mw_shl_code=c,true]//容器vector中元素的去重  
6 w) I- C  [0 f! Mvector<int> unique_element_in_vector(vector<int> v){  1 J& M4 u! r0 l* \
    vector<int>::iterator vector_iterator;  # }2 y0 ?' Y& R! Y$ f+ [
    sort(v.begin(),v.end());  
& Z5 {9 n, B' m1 f- G7 U) b    vector_iterator = unique(v.begin(),v.end());  0 A: v3 k0 I! V5 C
    if(vector_iterator != v.end()){  8 l8 ?+ @- n+ |5 n3 n" N; D9 z
        v.erase(vector_iterator,v.end());  . F$ H7 x$ q8 [3 C$ L
    }  
. _1 W8 O1 m; L- ], {    return v;  8 s0 K, z* x" m
}  
% W+ |4 h! ]5 ?5 |+ p! J  # Z" O6 X8 `7 B/ M4 i. \5 g
//两个vector求交集  
: p* M. r$ c# G+ Y! gvector<int> vectors_intersection(vector<int> v1,vector<int> v2){  % T. b2 ?4 i( d# U* S8 k
    vector<int> v;  - m& G9 k# D0 h5 K; ?9 ^$ t
    sort(v1.begin(),v1.end());     
) W2 j, P" s! B' x    sort(v2.begin(),v2.end());     ( D; {7 @4 y4 l# T* E& b
    set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集   0 Z0 s) M1 q# T4 ]0 w! h/ U6 v
    return v;  
( g! u1 L8 i, y( t  q6 z}  $ b# r2 L& x! L# S, ~
  
' j( g8 `3 t; U' h& b  d& [7 ^5 C' K//两个vector求并集  4 N# q. d- u6 Q+ E9 S; D3 S
vector<int> vectors_set_union(vector<int> v1,vector<int> v2){  - g# ]3 G. I) [
    vector<int> v;  0 b( q4 |' {- ]7 g
    sort(v1.begin(),v1.end());     $ m9 q/ l& |; R* p  R1 J
    sort(v2.begin(),v2.end());     6 Q5 L6 I8 J" T3 a
    set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集   * }+ }. r; N) b( E
    return v;  ) c- P. N  J: i& K1 D& Q
}  $ l( Y$ a1 {' }9 L0 }( X% ^" u
  - e2 [& @% _+ x+ A$ M" z- [4 n
//判断vector的某一元素是否存在  
# ^" u% [" g! v* y8 i- n( ^bool is_element_in_vector(vector<int> v,int element){  4 |# {  B; ^, j! U% D3 m. }  M7 {
    vector<int>::iterator it;  2 a; s+ X/ |' A' p$ R
    it=find(v.begin(),v.end(),element);    v' U9 I3 h8 G% Z5 v
    if (it!=v.end()){  * `7 W% Y/ f/ p+ Q! \  q
        return true;  
% \) G- o0 _- e1 h0 N3 U, G. q) j    }  
8 h% Y/ f6 {% l) K" l  k6 _. g9 [    else{    w( R) h& ?9 I! n* f6 K
        return false;  # L1 K9 j& t3 t  a
    }  ; }6 V  l5 h! d  a8 D. Y6 C
}  - ?0 l7 k* F. x& I7 J. j9 c3 N
  [/mw_shl_code]8 e+ U) K" l$ ^# m2 o9 J' \- s9 j5 V
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了