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

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

[复制链接]

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

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

admin 楼主

2018-7-2 19:41:23

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

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

x
2 t3 p0 B! G( N! o: y2 _( z( @- ]

; v7 D2 s7 f3 G, fC++虽然对vector封装了大量的函数,比如判断元素是否在vector中find、去重的unique,还有在algorithm类中对两个vector求交集、并集的函数,但是,这些函数是不可以直接vector.XX来使用的,如同在《【C++】容器类》中提到过如何删除vector的其中元素一样,需要搭上迭代器iterator对其进行遍历才能完成这个动作。这是不同于java与c#的,习惯就好。具体如下代码所示:: w6 T3 w; n: x- |- d
[mw_shl_code=cpp,true]#include "iostream", h' X* _' m2 q; G$ z7 A/ y
#include "vector"
* f% B; K) m2 M2 O#include "algorithm" //sort函数、交并补函数
6 d5 ?  S4 O* n& Z2 [8 n#include "iterator" //求交并补使用到的迭代器
) ~: {8 F0 B' f5 _9 Q+ |using namespace std;
# T0 E* \8 _9 |! W 0 g8 \( {, G4 W6 E, e3 b
//打印容器vector
% j3 J6 T  M- K9 ^. f; Qvoid print_vector(vector<int> v){# @' q' u0 F  G( X3 d
        if(v.size()>0){
8 J$ O; P1 Q1 `6 _! B5 N                cout<<"{";  - ^2 Z1 q# ]( J: X5 L2 r* b% O1 F0 _: w
                for(int i=0;i<int(v.size());i++){  - y( C# f" H( y! V, z4 H$ [
                        cout<<v<<",";  
  u/ w) X+ i' u                }  
, B1 J; Z9 O* h: ~* Q                cout<<"\b}";  % h/ M, u! ^3 g
        }7 z9 C% F% y3 E% j
        else{. L' x( k) L0 X6 [% E
                cout<<"{}";0 C: `; R  W2 V, K+ U  J) z4 T
        }
- C# E( }! F- i+ m3 Y}
! c/ F+ Q+ a" L& }; d* B( b
6 N# R' `4 }. u" c//容器vector中元素的去重$ y  x, f/ H3 V( Q: L# W
vector<int> unique_element_in_vector(vector<int> v){: R6 F" d: o& ]
        vector<int>::iterator vector_iterator;
. z4 L6 f. {1 g9 m/ P' f$ e        sort(v.begin(),v.end());# _$ y0 J* R$ J0 ?' _" Z+ z0 P
        vector_iterator = unique(v.begin(),v.end());! J/ u7 L+ A, G+ \* S2 U
        if(vector_iterator != v.end()){# U7 H8 {9 G0 T6 N) C5 u$ l4 N* K
                v.erase(vector_iterator,v.end());
4 ]* S- k+ ]$ y        }
/ k; Q/ l: B: _' {5 ^        return v;, B# a2 _9 ?% v3 O) Q$ Y
}( O4 m* g) Z( }! F1 K

3 D6 ?+ Q9 _7 L; U. K  ~//两个vector求交集
7 m; E( I7 O, l) ^( o3 i2 x4 qvector<int> vectors_intersection(vector<int> v1,vector<int> v2){
/ H, r  t6 w2 m" e# j1 A        vector<int> v;0 ^& u: j- x2 p% [  f1 l  e
        sort(v1.begin(),v1.end());   
0 B+ I4 Y* Q" ?        sort(v2.begin(),v2.end());   ! e, P4 b9 s% \2 @6 S( s  _6 c
        set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
3 u$ k* x; I/ g9 N! j, _        return v;
" y5 \2 k! F7 v- Y* q}( x  G+ \" `' C+ I; |

" b7 z, U; N+ r% K* j//两个vector求并集7 S' }8 A6 _5 [5 b0 e# c
vector<int> vectors_set_union(vector<int> v1,vector<int> v2){
* F: A# L4 N8 D- e9 r        vector<int> v;. Z& G" w8 w' N' y# S+ A8 {
        sort(v1.begin(),v1.end());   
# y  S% X  g- u1 R* @- ?        sort(v2.begin(),v2.end());   3 ?# V- e7 f( T3 j* T( o
        set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
" E$ \: G/ g9 [& A# y" O2 e6 L0 ]& ~        return v;1 f4 _: U; v* D) B4 J$ [) p
}
! q' t+ C' D- h$ D1 C
/ s5 |% ^% i8 ?! ~' W' ^//判断vector的某一元素是否存在2 [4 q. [5 ]1 ?0 ?
bool is_element_in_vector(vector<int> v,int element){
" |/ n. b. o8 ?3 ~2 ^4 L        vector<int>::iterator it;+ u" ~) y+ f  ^7 K. ^8 c+ `$ L: |9 a
        it=find(v.begin(),v.end(),element);
2 ~$ }& S& u3 \* t0 F5 M        if (it!=v.end()){
3 S2 @/ \: l& ?4 G4 {) B7 [                return true;
1 O. O% p( m/ U5 t1 }6 E* Q        }; U' e" Q1 B  R; y& e
        else{% R! x" ~% Q; x8 x
                return false;( W: ^2 V$ `2 q) k: A+ u. l
        }
4 ~9 Z; \6 b8 n9 T3 f7 _}
# `+ h* j7 i7 ~- g, @
+ G' N8 t" Y0 S# s* Fint main(){; j& l+ B# _9 M" R# d' l
        vector<int> v1,v2,v;' M4 `2 d+ s# ^! n4 d% T/ W, n9 J
        v1.push_back(22);v1.push_back(22);v1.push_back(23);v2.push_back(23);v2.push_back(24);
( b( F* f, D3 U% M/ k2 C+ f        cout<<"v1是否存在1这个元素?"<<is_element_in_vector(v1,1)<<endl;
$ o- v7 |1 C! \* j. P6 \+ W9 v        cout<<"对v1去重:";
9 ]) |3 K, [1 t! |4 z        v1=unique_element_in_vector(v1);1 w/ _: |& i( Q& h/ D8 V
        print_vector(v1);
9 z7 Z# B2 m! w4 _! @        cout<<endl;
/ }  Z' n% m( K# ~* f( p2 l        cout<<"求v1与v2的交集:";
1 W6 {) s3 Z$ k  |        v=vectors_intersection(v1,v2);9 U& t, {  [+ N
        print_vector(v);
* G" Q3 K( y+ e$ r. N* ~        cout<<endl;
$ t# A- R+ }! n% }) P- x  r        cout<<"求v1与v2的并集:";
4 E. z9 b0 W4 @( u% X# z% Y        v=vectors_set_union(v1,v2);
! n- o# X% Q1 z7 Z- _- I        print_vector(v);
. @/ p9 p: c- m/ h2 b( @        return 0;
/ ?" J. `1 G! ^- N. m: @8 t}[/mw_shl_code]
0 o* r: N$ b5 `' T$ |( D
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了