PLM之家PLMHome-国产软件践行者

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

[复制链接]

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

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

admin 楼主

2018-4-3 07:56:54

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

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

x

9 s# j5 T' _& v# ^两个vector 去重复,相交,合并的函数分享
9 T7 S7 m2 e8 \) e" _
" J0 O/ D  I' I) Y$ E' f[mw_shl_code=c,true]//容器vector中元素的去重  
7 C+ I1 @& v! R( \9 Yvector<int> unique_element_in_vector(vector<int> v){  
& x( {8 R8 z8 V& U2 y4 J    vector<int>::iterator vector_iterator;  
7 k$ r. U4 B1 Y. r& J8 T  g7 `3 ?    sort(v.begin(),v.end());  
6 n, p0 x* v+ u1 S    vector_iterator = unique(v.begin(),v.end());  . }5 C$ N& f1 _+ s4 W
    if(vector_iterator != v.end()){  ' H; l: x! I7 b# u0 B- o
        v.erase(vector_iterator,v.end());  ) g& t! p4 {5 n9 Y- [
    }  
( g6 F1 ^5 b" ~7 S7 D    return v;  
; b. J# b7 a7 L% f  r% B}  ; x4 |9 h5 q2 u. O
  ) e; x. Z. T7 G1 [
//两个vector求交集  0 \, K, P& {/ t
vector<int> vectors_intersection(vector<int> v1,vector<int> v2){  : U1 `/ v4 x3 A
    vector<int> v;  . v( D6 d0 S9 ], s& j1 ~! e# |
    sort(v1.begin(),v1.end());     8 ?; K6 D5 w- N+ d/ ~
    sort(v2.begin(),v2.end());     
/ ~3 H* b; K* Y4 d/ t0 v4 h0 J; }    set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集   2 A. ]2 i# h% Y. L/ m  _
    return v;  
' [% j2 S4 {5 Y3 X6 n}  + f+ C' d7 h' w" d
  + l" M6 \! m: Q& S1 `0 y9 J% Q
//两个vector求并集  
+ Y6 ?8 ?# `, Y& A/ u: |& Lvector<int> vectors_set_union(vector<int> v1,vector<int> v2){  
9 Y1 t7 b, D) Q9 T( X5 `* q    vector<int> v;  0 u( g. `& Q+ C( v
    sort(v1.begin(),v1.end());     . y: X: _/ \! H8 s" B/ v! v
    sort(v2.begin(),v2.end());     
# t) c* Z2 U, A% r6 p- v0 V' P/ x    set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集   
% I8 |1 ?! Z, U, m+ O    return v;  . y5 ]" _! p6 L3 S/ C- n
}  
! Z8 @" z$ y& {& Q  
. q+ I7 K$ J( Z& f//判断vector的某一元素是否存在  2 @5 j* E+ I5 J5 k) ?  M% M7 w
bool is_element_in_vector(vector<int> v,int element){  & N6 @( S, F1 Z+ S+ o! C
    vector<int>::iterator it;  - g2 `% p6 D6 S" E
    it=find(v.begin(),v.end(),element);  
- l- x9 R  A* E- a& \4 u    if (it!=v.end()){  
1 P4 M( x; n6 F        return true;  
* ]# _7 j: C; N2 d7 {    }  7 `- y; c+ O8 F& x/ x+ a
    else{  % O$ M$ F! A; v; P; u
        return false;  ) J: s8 V% g1 P0 Y, m
    }  
) ^0 A6 M; ^/ Z}  
" L+ ]0 v; k0 Q, S  [/mw_shl_code]
; q* i6 r' D: Z( `0 f
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了