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

[资料] 元素是否在vector中,对vector去重,两个vector求交集、并集

[复制链接]

2018-7-2 19:41:23 2498 0

admin 发表于 2018-7-2 19:41:23 |阅读模式

admin 楼主

2018-7-2 19:41:23

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

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

x
5 L- G4 B+ e* c% x: `

; c- X3 U3 [: W3 lC++虽然对vector封装了大量的函数,比如判断元素是否在vector中find、去重的unique,还有在algorithm类中对两个vector求交集、并集的函数,但是,这些函数是不可以直接vector.XX来使用的,如同在《【C++】容器类》中提到过如何删除vector的其中元素一样,需要搭上迭代器iterator对其进行遍历才能完成这个动作。这是不同于java与c#的,习惯就好。具体如下代码所示:: s' }/ V/ n# A9 [' o  m6 _
[mw_shl_code=cpp,true]#include "iostream"
; j7 d$ F% u# f8 z1 M8 E8 {#include "vector"   d* f, N/ \/ y5 x
#include "algorithm" //sort函数、交并补函数
1 ~8 f2 O5 D6 }& @#include "iterator" //求交并补使用到的迭代器
5 |3 R! d- g$ n+ t3 m, h9 G/ qusing namespace std;! B: p4 v0 U5 K

$ _/ d8 Y, ~% k. {+ ]//打印容器vector8 {& f- e8 i2 N2 b
void print_vector(vector<int> v){
+ A! j4 |; j$ o* A9 l        if(v.size()>0){  B0 P+ e2 p$ A8 \  S: Y
                cout<<"{";  
- m1 `: f, g6 h# [+ n) T# j                for(int i=0;i<int(v.size());i++){  * A2 H# O8 |# \) m( j1 @
                        cout<<v<<",";  2 E3 i0 ^$ j( `0 ^/ V# H0 @% D5 H# s6 \. b
                }  
# n9 u) b# ]% P3 ~4 C9 N                cout<<"\b}";  ' r, B; @0 s6 v+ ^& H+ v
        }8 j8 g% g6 `, \( r  f  h( B# g
        else{
+ ]3 A9 F8 O6 J                cout<<"{}";
# r$ Y3 M8 e4 q8 O        }: u$ i/ X+ Y4 J- j0 \7 @6 E
}
5 D/ u* ^4 F- r9 i
  V& U4 ^/ Z# X4 C! W6 O5 p% s//容器vector中元素的去重
* {6 H$ r' C# {vector<int> unique_element_in_vector(vector<int> v){
- Y! P- W6 X5 @/ f# {5 j, ?        vector<int>::iterator vector_iterator;
; p$ R& j' D& ?; \/ l' u) i        sort(v.begin(),v.end());
5 K% _2 n4 W9 A; @9 x" F! F. P  S        vector_iterator = unique(v.begin(),v.end());
7 ]+ P- k; r1 I9 e( ~# d% ~        if(vector_iterator != v.end()){
5 H( v3 {9 f8 X) q8 _% t, L: t, I                v.erase(vector_iterator,v.end());* J7 i  z; a  L# }
        }5 x' w( b- e* N) M
        return v;
) O7 `0 p" x# D( V$ Q  o6 y}
8 }# ~% `1 i# D- f- e0 x$ E6 d' ^
  Q2 d* A' }+ c# g+ ~; P$ G9 G% t//两个vector求交集
% D5 P0 u. v' Y6 s# p* Qvector<int> vectors_intersection(vector<int> v1,vector<int> v2){( H" C$ e  N* e. K  F5 ~1 b% e! N. ^
        vector<int> v;
  e0 _! Y- {' e$ I, g        sort(v1.begin(),v1.end());   
2 I1 {" F" {+ @, w) q; V; ]        sort(v2.begin(),v2.end());   * l% ]6 n/ \& @1 M
        set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
, X0 J, G8 v# O+ G- n& M# B        return v;8 n2 M1 L' N% |: V; Z3 l: B
}
+ f! t0 F8 y! C) u; O6 o
/ J5 [4 O" A8 \/ g5 Z5 i//两个vector求并集. \2 }3 [# E7 }, l7 n
vector<int> vectors_set_union(vector<int> v1,vector<int> v2){
  }+ w) _6 y: u% ~' M        vector<int> v;
. H- `- S4 M2 G8 x% ?4 J+ T        sort(v1.begin(),v1.end());   4 J, I( |" [+ a+ K5 d% F( W
        sort(v2.begin(),v2.end());   
/ H$ B9 Y0 X9 o2 W2 l$ D  |5 m2 N        set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
  H( o3 r  H# _$ I        return v;
- v) a; Q6 H* @5 S' _0 V" Q6 T/ u. ]}
) ?4 _4 Y1 u( ^2 X  D: F & @: ?& k% h% C
//判断vector的某一元素是否存在4 \6 P) ?" Z( K: J  L
bool is_element_in_vector(vector<int> v,int element){/ ?2 f8 i1 `# J, ~; \2 \; z
        vector<int>::iterator it;
/ W8 ]* X, [  L, d. \& ^        it=find(v.begin(),v.end(),element);
3 P+ q. j6 a, P8 d# B        if (it!=v.end()){+ {3 d1 e- A5 ~9 q8 b5 z/ B+ `& L: }
                return true;7 U0 a6 I8 A. X6 b+ {( ?+ Y) p
        }
  X) l  S: y: U) T        else{
" }9 [; _5 F# E0 z, L5 M                return false;
# U) h( A! F; k! M, @0 u0 B  ~; r        }4 U4 y) D+ R7 k  b  ^
}& {  _, X# }2 M: C, B& {" f% Q6 ^
# N8 @' w0 j. \2 g  f4 B& `3 b
int main(){/ _$ }* |, }" g% [1 |/ q
        vector<int> v1,v2,v;
2 J) S& P; Z0 |( A8 \: |        v1.push_back(22);v1.push_back(22);v1.push_back(23);v2.push_back(23);v2.push_back(24);/ B/ C; ^$ e# a
        cout<<"v1是否存在1这个元素?"<<is_element_in_vector(v1,1)<<endl;
( w1 _4 |6 g: d, m        cout<<"对v1去重:";
' p& b" L% I4 W1 \        v1=unique_element_in_vector(v1);
3 o6 G( n' H9 K+ q! s9 J        print_vector(v1);
* x5 O  J1 s4 Q- L0 N        cout<<endl;
# ?, }3 L  ]6 @( b        cout<<"求v1与v2的交集:";, B! @2 I2 K+ u- ^+ k* t) U: n
        v=vectors_intersection(v1,v2);
! b* P3 K; _7 U) \3 q        print_vector(v);
: `5 D4 F7 o6 r" @/ N( A1 S9 i4 E        cout<<endl;
$ @, k3 K& F5 t2 T% f9 q* H        cout<<"求v1与v2的并集:";0 u2 {- h/ L* w3 H
        v=vectors_set_union(v1,v2);$ C& |) G2 H; b$ l( T0 |
        print_vector(v);
5 d, g9 D. `' j# D2 f2 E1 Y        return 0;( U+ {9 I3 x+ O0 z% K! J/ G+ O
}[/mw_shl_code]/ a% b( @$ h5 n5 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二次开发专题模块培训报名开始啦

    我知道了