zoukankan      html  css  js  c++  java
  • STL_算法_05_集合算法

    ◆ 常用的集合算法: 

    1、

    1.1、第6讲 PPT.40

    ◆ set_union() :  构造一个有序序列,包含两个有序序列的并集。

    1.2、第6讲 PPT.40

    ◆ set_intersection() :  构造一个有序序列,包含两个有序序列的交集。

    1.3、第6讲 PPT.40

    ◆ set_difference() :  构造一个有序序列,该序列保留第一个有序序列中存在而第二个有序序列中不存在的元素。

    ZC: VC6 测试代码:

     1 #ifdef WIN32
     2 #pragma warning (disable: 4786)
     3 #endif
     4 
     5 #include <string>
     6 #include <vector>
     7 #include <set>
     8 
     9 #include <algorithm>    // 算法
    10 #include <numeric>    // 算法
    11 #include <functional>    // 算法
    12 
    13 using namespace std;
    14 
    15 void main()
    16 {
    17     vector<int> vecIntA;
    18     vecIntA.push_back(1);
    19     vecIntA.push_back(3);
    20     vecIntA.push_back(5);
    21     vecIntA.push_back(7);
    22     vecIntA.push_back(9);
    23 
    24     vector<int> vecIntB;
    25     vecIntB.push_back(1);
    26     vecIntB.push_back(3);
    27     vecIntB.push_back(5);
    28     vecIntB.push_back(6);
    29     vecIntB.push_back(8);
    30 
    31     vector<int> vecIntC;
    32     vecIntC.resize(10); // ZC: 没有这句,VC6编译的exe执行时会崩溃
    33     //vecIntC.resize(5);    // ZC: 若这里设置的大小 小于7(∵这里结合实际情况,vecIntC的元素最多为7个),则VC6编译的exe在执行过程中:
    34                             // ZC:    Debug会报"Debug Error !"==>"DAMAGE: after Normal block (#64) at 0x00332EE8";
    35                             // ZC:    Release什么错误都没有,但是由于vecIntC中元素个数较少,放不下的那些数据就被丢弃了。
    36 
    37     //并集
    38     set_union(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin());    //vecIntC : {1,3,5,6,7,8,9,0,0,0}
    39 
    40             int iIdx = 0;
    41             vector<int>::iterator itC = vecIntC.begin();
    42             while (itC != vecIntC.end())
    43             {
    44                 printf("[%02d] ==> %d
    ", iIdx, *itC);
    45                 itC ++;
    46                 iIdx ++;
    47             }
    48             printf("
    ");
    49 
    50     //交集
    51     fill(vecIntC.begin(), vecIntC.end(), 0);
    52     set_intersection(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin());    //vecIntC: {1,3,5,0,0,0,0,0,0,0}
    53 
    54             iIdx = 0;
    55             itC = vecIntC.begin();
    56             while (itC != vecIntC.end())
    57             {
    58                 printf("[%02d] ==> %d
    ", iIdx, *itC);
    59                 itC ++;
    60                 iIdx ++;
    61             }
    62             printf("
    ");
    63 
    64     //差集
    65     fill(vecIntC.begin(), vecIntC.end(), 0);
    66     set_difference(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin());    //vecIntC: {7,9,0,0,0,0,0,0,0,0}
    67 
    68             iIdx = 0;
    69             itC = vecIntC.begin();
    70             while (itC != vecIntC.end())
    71             {
    72                 printf("[%02d] ==> %d
    ", iIdx, *itC);
    73                 itC ++;
    74                 iIdx ++;
    75             }
    76 }

    ZC:控制台输出:

     1 [00] ==> 1
     2 [01] ==> 3
     3 [02] ==> 5
     4 [03] ==> 6
     5 [04] ==> 7
     6 [05] ==> 8
     7 [06] ==> 9
     8 [07] ==> 0
     9 [08] ==> 0
    10 [09] ==> 0
    11 
    12 [00] ==> 1
    13 [01] ==> 3
    14 [02] ==> 5
    15 [03] ==> 0
    16 [04] ==> 0
    17 [05] ==> 0
    18 [06] ==> 0
    19 [07] ==> 0
    20 [08] ==> 0
    21 [09] ==> 0
    22 
    23 [00] ==> 7
    24 [01] ==> 9
    25 [02] ==> 0
    26 [03] ==> 0
    27 [04] ==> 0
    28 [05] ==> 0
    29 [06] ==> 0
    30 [07] ==> 0
    31 [08] ==> 0
    32 [09] ==> 0
    33 Press any key to continue

    ?.?、第6讲 PPT.?

    ◆ 

    ZC: VC6 测试代码:

    ZC:控制台输出:

    X

  • 相关阅读:
    Reaper自定义模板
    c#3.0 特性
    C#中下载文件出现410错误。
    使用Create task with ContentType创建任务的时候,必须先在task list中加上该ContentType
    tsmmc.msc 远程桌面
    工作流的ReplicatorActivity
    关于Windows2003的远程桌面链接数量。
    【手绘】A old painting ,drawed in middle school ,grade 8
    【Notepad++】Notepad ++ plugin Compare
    【资讯】Fight for this goal ,and better than this~
  • 原文地址:https://www.cnblogs.com/cppskill/p/5241872.html
Copyright © 2011-2022 走看看