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

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

[复制链接]

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

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

admin 楼主

2018-7-2 19:41:23

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

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

x

) T' z  S, H8 Y5 P# {" F4 U! R4 b0 k* O
C++虽然对vector封装了大量的函数,比如判断元素是否在vector中find、去重的unique,还有在algorithm类中对两个vector求交集、并集的函数,但是,这些函数是不可以直接vector.XX来使用的,如同在《【C++】容器类》中提到过如何删除vector的其中元素一样,需要搭上迭代器iterator对其进行遍历才能完成这个动作。这是不同于java与c#的,习惯就好。具体如下代码所示:
( a4 [6 k. J7 j1 @3 I' h2 F9 S! @+ _: B[mw_shl_code=cpp,true]#include "iostream"7 C/ |5 M, W4 r& D
#include "vector"
9 U3 x' Q3 V$ F8 a1 r9 ?#include "algorithm" //sort函数、交并补函数1 i5 g2 K5 z# |  y$ e0 s5 E: S
#include "iterator" //求交并补使用到的迭代器+ q" q, o( A5 V7 R1 g. a3 o1 y" ?
using namespace std;
7 P; D: T& k+ S+ d' W : F9 Q- I9 w/ o: K, k! v6 u
//打印容器vector9 u+ L3 q. f0 G  R0 x' W
void print_vector(vector<int> v){1 V* [/ ?$ Y; `
        if(v.size()>0){
5 \% X! E- `( C8 ^9 E( I' U                cout<<"{";  
$ i0 n" a  S2 w1 t$ ~- p                for(int i=0;i<int(v.size());i++){  . M. S$ Z+ G# i1 W4 ~! E$ ~8 e
                        cout<<v<<",";  0 T. V2 f# g6 M* S0 f5 p  F
                }  
& P, N3 k7 Z; h4 x                cout<<"\b}";  
4 I* {1 c; a. f  y9 X, d        }
0 f) z5 Y6 o; P7 M9 [        else{1 Q  \- S8 O9 }# k! m3 w) j
                cout<<"{}";! f/ k+ l: R  e! Z- {
        }
+ w- O1 o  r* M) T0 G}
; H: n9 \0 H' u) W" W; m
. v6 w) N- Y  _( j9 T: e0 @//容器vector中元素的去重
( J# }4 M5 Q( J& ~" Kvector<int> unique_element_in_vector(vector<int> v){" W: S4 }. o/ y1 s% B+ [9 c
        vector<int>::iterator vector_iterator;0 f+ {$ d0 s4 a
        sort(v.begin(),v.end());7 x0 @" f& r/ \+ C
        vector_iterator = unique(v.begin(),v.end());
: j. b% t) U7 \, x! x7 V7 `        if(vector_iterator != v.end()){4 W/ h; A6 b8 ]. S3 k- v
                v.erase(vector_iterator,v.end());
& q1 |0 h$ n" \' Y% X) `        }
- }2 ?8 z) m! o6 `        return v;4 w5 [9 x/ ]' g4 r# G5 D, P9 T( g0 \
}& E: h& m1 ~8 \( I1 B8 g
; G! [6 V! ^8 }  {" O; U
//两个vector求交集
& P$ T- P1 j5 v& ~9 L1 yvector<int> vectors_intersection(vector<int> v1,vector<int> v2){+ I+ m) M3 b7 V$ n" `
        vector<int> v;
% |' p: l3 z2 b+ J& _9 q        sort(v1.begin(),v1.end());   ) s+ X' b$ e% Q
        sort(v2.begin(),v2.end());   2 s* a1 {! w2 l! D8 r. L
        set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
1 {' j4 |3 H' w" \% z& d        return v;
# d/ U2 [1 W; ~& b}
* t& P6 ~: @& R3 o/ g: d
/ H6 q( ^' V: L3 K5 `/ e//两个vector求并集
: H6 P# t3 w' `3 B8 X  b7 [vector<int> vectors_set_union(vector<int> v1,vector<int> v2){
) Q# c; _+ L' d" d% U& e+ ^        vector<int> v;
* h5 ^* X: K9 M' G        sort(v1.begin(),v1.end());   
: K  Y3 P% i& r        sort(v2.begin(),v2.end());   ) d. V6 c. B& o" N: ~6 N% `
        set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v));//求交集
2 Q5 j, I2 G! \$ i4 C: E! ^9 p        return v;
, a! p+ x9 }3 k5 R; ~. T}
3 D0 i6 w2 ^9 E1 D5 |& E ! R/ I: J3 e1 w8 Z% e
//判断vector的某一元素是否存在
/ u6 Y* J  v4 Q, K3 o6 lbool is_element_in_vector(vector<int> v,int element){
( e" b" e$ U: a        vector<int>::iterator it;
# q1 I2 R1 }# h        it=find(v.begin(),v.end(),element);. s( ^+ S1 `. }/ F0 i
        if (it!=v.end()){2 x2 t- N" e3 I& t0 I; g. M7 _
                return true;
6 z' r' d1 |% z: X9 R        }+ g' x7 l1 f' f" T
        else{
2 g! |: ]  @, P! K: ]                return false;" H1 {& B7 Q1 J7 e$ u) {
        }. U* ~- ]: u2 T3 t; S3 A
}9 S% x* L0 K) y+ T
8 W' d( y6 R0 W9 i
int main(){4 X* f! Q8 w# R) ~) \( e4 {% g
        vector<int> v1,v2,v;
2 s- P# B" p5 ?. Q  ^! q3 W7 S8 H        v1.push_back(22);v1.push_back(22);v1.push_back(23);v2.push_back(23);v2.push_back(24);- n4 d& [  V/ s$ ~3 I) w
        cout<<"v1是否存在1这个元素?"<<is_element_in_vector(v1,1)<<endl;
  f! N+ u8 ]3 d$ l8 E$ H        cout<<"对v1去重:";
3 q" G4 \9 X; e8 x, J" _$ _. X( T        v1=unique_element_in_vector(v1);; w) ~/ R- A: k6 N* T0 z2 U5 {
        print_vector(v1);
4 g0 B- o7 S  Z0 F        cout<<endl;
9 }. \" ?6 M& ~: k/ S: P) ~- v% H: l        cout<<"求v1与v2的交集:";
. v- U3 |* g% e        v=vectors_intersection(v1,v2);
; A' `3 M8 G% o4 @4 x) |% P  s        print_vector(v);$ b: Z; b; {6 o/ r3 V8 ~
        cout<<endl;
- G) h" n1 h. K* P        cout<<"求v1与v2的并集:";
, ^* k# N1 ?0 I& b8 @) J+ E% g% i0 g        v=vectors_set_union(v1,v2);' J* M  D, E# u6 U, P* t# Q6 l
        print_vector(v);2 y  A4 Z3 c+ K' r0 S
        return 0;
1 }  m6 O& i( u, j- E, A}[/mw_shl_code]9 F, V+ r& X1 s0 N# Y( L
上海点团信息科技有限公司,承接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二次开发专题模块培训报名开始啦

    我知道了