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

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

[复制链接]

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

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

admin 楼主

2018-7-2 19:41:23

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

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

x

7 P, @0 c$ o" p- I6 T' y
% W8 I, N1 w  {& z& y7 _C++虽然对vector封装了大量的函数,比如判断元素是否在vector中find、去重的unique,还有在algorithm类中对两个vector求交集、并集的函数,但是,这些函数是不可以直接vector.XX来使用的,如同在《【C++】容器类》中提到过如何删除vector的其中元素一样,需要搭上迭代器iterator对其进行遍历才能完成这个动作。这是不同于java与c#的,习惯就好。具体如下代码所示:
4 D  L) _! d9 g# r# _" V: w7 z[mw_shl_code=cpp,true]#include "iostream"
! [2 ]9 @$ D$ u, G9 b  n3 u) ?#include "vector"
( v$ C7 r0 j, |2 x#include "algorithm" //sort函数、交并补函数
3 I  c+ e1 L2 x' n5 y& M#include "iterator" //求交并补使用到的迭代器
9 p5 Y0 Z% ]7 I/ U0 y7 d- _using namespace std;
0 b0 U- O) w) G$ j( C% y
0 ?! C4 l) d' f7 b//打印容器vector
3 D+ C8 f$ J8 I! o5 c0 Xvoid print_vector(vector<int> v){
; a7 s: f* _$ [        if(v.size()>0){
6 O" U  ?; M5 F8 d+ m0 e. @                cout<<"{";  ) P) Z5 }2 w. R+ Q
                for(int i=0;i<int(v.size());i++){  
# T1 S% T% o' b: C' S; O                        cout<<v<<",";  
4 z1 P3 z/ t+ U% d8 c, Y2 a( G$ M                }  " f1 j$ T4 _5 _+ b
                cout<<"\b}";  
6 H5 F# C9 m  O" W- {0 ?  T        }
" D# K) Q0 H( c9 _! [5 N        else{
- ?4 S% w2 ?' h* H6 h, D! O                cout<<"{}";
7 U1 M! J- W3 g7 ~! f6 [, a        }$ M0 Y5 u' A( O6 _, x7 T) t# @
}% V/ O8 j5 D/ x
: Y* I* {+ ?& I! Q# Z! A
//容器vector中元素的去重
0 y5 Q. h1 n9 R8 o/ Svector<int> unique_element_in_vector(vector<int> v){* D/ G2 Q/ l7 n- C2 p  R
        vector<int>::iterator vector_iterator;' W5 ^0 K1 U. C" d. ~' {- @
        sort(v.begin(),v.end());% J/ T8 U$ Q3 Z& L) q3 j& ?
        vector_iterator = unique(v.begin(),v.end());/ D! @" z5 J& \5 A& B1 t
        if(vector_iterator != v.end()){
2 j) w; u$ c7 D* v                v.erase(vector_iterator,v.end());) W) Z/ Z) r( f% F+ o
        }
2 J% v( I4 f7 u3 E% m0 ^* i+ h        return v;
; d: n) v* n$ b' p; n+ B2 S}4 @' U" r1 I+ H- W3 E$ k" n0 g# }- i
6 V. {3 w9 F7 v3 [2 j0 J
//两个vector求交集2 g0 j; W* ]$ }# S8 g
vector<int> vectors_intersection(vector<int> v1,vector<int> v2){
% W( @6 z6 M# t2 T9 H" O        vector<int> v;; z3 I- V( y6 s5 ?- G6 P. ?
        sort(v1.begin(),v1.end());   # R5 [* Z: i. P2 x
        sort(v2.begin(),v2.end());   
6 l$ i/ G7 v  t5 Z5 v% Y& w# Q8 r        set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
1 b$ |( y/ w- _, R' Q        return v;
" V& P& y1 `( ?9 \, U* e; I}; K: `$ l3 Y% v1 w( ~

! R, ^7 h5 Y; `& I* X9 i//两个vector求并集
3 l0 Q% B  n3 o/ V" n' cvector<int> vectors_set_union(vector<int> v1,vector<int> v2){& \2 ]" e3 L9 g8 c+ X3 D
        vector<int> v;
* b' v  A- I" k5 G; o' y, L( P) e6 J        sort(v1.begin(),v1.end());   9 _. @5 H- k. \, r8 ?" N
        sort(v2.begin(),v2.end());   ; X% f1 c$ Q4 i7 o9 U4 `" n' r
        set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集 % H9 v7 ~) d: G$ W4 w# M' R
        return v;
6 u/ L. v$ F3 q" A4 o% t6 Q}' e9 i3 M: [' f* t4 q
8 m5 O, e3 M7 K% \" L7 x
//判断vector的某一元素是否存在
) d7 \: ^0 d! ?% J% ebool is_element_in_vector(vector<int> v,int element){
0 D9 `5 l! G# D4 E. W# W6 D* g1 `: N        vector<int>::iterator it;$ C2 X* I, C/ W7 r4 T
        it=find(v.begin(),v.end(),element);
/ y/ c3 s" g& @# `4 ^5 d        if (it!=v.end()){% u6 B% M6 C6 C* c* \
                return true;
4 _- y2 n) n3 U2 r* \        }$ L4 I" h& A7 T" P
        else{' v( v' d( S' Y2 `/ s2 r' O' D; F
                return false;8 c0 |9 P5 g2 |
        }
! ^: d% _7 S. Q1 G* ]  ~. ]}9 v- |+ M9 u5 }0 [7 p! [7 Q

; d* S4 j. ]3 c' Uint main(){$ S2 r0 A! O$ D6 F4 }
        vector<int> v1,v2,v;
2 J  ]- e+ e; P# k. Z        v1.push_back(22);v1.push_back(22);v1.push_back(23);v2.push_back(23);v2.push_back(24);' `4 K; h, q& ^% h5 X: ?1 L
        cout<<"v1是否存在1这个元素?"<<is_element_in_vector(v1,1)<<endl;
" F) u0 }3 G. T. Y% O        cout<<"对v1去重:";
  b/ G* k2 c6 U' K" R; w        v1=unique_element_in_vector(v1);
; o8 d/ I, S0 b" ]- E        print_vector(v1);
. W6 j# ~. `2 e9 ]7 h) ?9 I8 ~        cout<<endl;+ B7 [5 w$ L2 {+ e+ D  A! C
        cout<<"求v1与v2的交集:";
, M) W; }. X; Z7 K2 ?        v=vectors_intersection(v1,v2);+ ~" ]$ Q$ Y; l: r
        print_vector(v);
4 G, ~5 U9 v1 E0 s) h        cout<<endl;0 D3 @  P: d7 Q" \7 i
        cout<<"求v1与v2的并集:";
' o1 W5 k" n" E# G  k/ T5 q        v=vectors_set_union(v1,v2);' w& c+ H) V* {
        print_vector(v);
& Q5 F3 G% x7 K        return 0;
9 ?- t9 t" K  g( ~& ?- h}[/mw_shl_code]1 c2 M, h) c( a, p. y
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了