• STL


    参考:http://www.cplusplus.com/reference/set/set/

    一、set 是按特定顺序存储唯一元素的容器

    实现是一种非常高效的平衡检索二叉树:红黑树(Red-Black Tree)。

    二、set 的特性

    1、set中的元素都是排好序的(与lower_bound()等结合使用能起到找前驱、后继的作用)

    2、set集合中没有重复的元素(常常用于去重)

    三、set 的成员函数

    begin() 返回指向第一个元素的迭代器
    end() 返回指向最后一个元素的迭代器
     1 // set::begin/end
     2 #include <iostream>
     3 #include <set>
     4 
     5 int main ()
     6 {
     7   int myints[] = {75,23,65,42,13};
     8   std::set<int> myset (myints,myints+5);
     9 
    10   std::cout << "myset contains:";
    11   for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    12     std::cout << ' ' << *it;
    13 
    14   std::cout << '
    ';
    15 
    16   return 0;
    17 }
    View Code
    empty () 如果集合为空,返回true
    size () 集合中元素的数目
    max_size() 返回集合能容纳的元素的最大限值
     1 // set::empty
     2 #include <iostream>
     3 #include <set>
     4 
     5 int main ()
     6 {
     7   std::set<int> myset;
     8 
     9   myset.insert(20);
    10   myset.insert(30);
    11   myset.insert(10);
    12 
    13   std::cout << "myset contains:";
    14   while (!myset.empty())
    15   {
    16      std::cout << ' ' << *myset.begin();
    17      myset.erase(myset.begin());
    18   }
    19   std::cout << '
    ';
    20 
    21   return 0;
    22 }
    23 
    24 
    25 // set::size
    26 #include <iostream>
    27 #include <set>
    28 
    29 int main ()
    30 {
    31   std::set<int> myints;
    32   std::cout << "0. size: " << myints.size() << '
    ';
    33 
    34   for (int i=0; i<10; ++i) myints.insert(i);
    35   std::cout << "1. size: " << myints.size() << '
    ';
    36 
    37   myints.insert (100);
    38   std::cout << "2. size: " << myints.size() << '
    ';
    39 
    40   myints.erase(5);
    41   std::cout << "3. size: " << myints.size() << '
    ';
    42 
    43   return 0;
    44 }
    45 
    46 // set::max_size
    47 #include <iostream>
    48 #include <set>
    49 
    50 int main ()
    51 {
    52   int i;
    53   std::set<int> myset;
    54 
    55   if (myset.max_size()>1000)
    56   {
    57     for (i=0; i<1000; i++) myset.insert(i);
    58     std::cout << "The set contains 1000 elements.
    ";
    59   }
    60   else std::cout << "The set could not hold 1000 elements.
    ";
    61 
    62   return 0;
    63 }
    View Code
    insert() 在集合中插入元素
    erase() 删除集合中的元素
    swap() 交换两个集合变量
    clear() 清除所有元素

     

     

     

     

      1 // set::insert (C++98)
      2 #include <iostream>
      3 #include <set>
      4 
      5 int main ()
      6 {
      7   std::set<int> myset;
      8   std::set<int>::iterator it;
      9   std::pair<std::set<int>::iterator,bool> ret;
     10 
     11   // set some initial values:
     12   for (int i=1; i<=5; ++i) myset.insert(i*10);    // set: 10 20 30 40 50
     13 
     14   ret = myset.insert(20);               // no new element inserted
     15 
     16   if (ret.second==false) it=ret.first;  // "it" now points to element 20
     17 
     18   myset.insert (it,25);                 // max efficiency inserting
     19   myset.insert (it,24);                 // max efficiency inserting
     20   myset.insert (it,26);                 // no max efficiency inserting
     21 
     22   int myints[]= {5,10,15};              // 10 already in set, not inserted
     23   myset.insert (myints,myints+3);
     24 
     25   std::cout << "myset contains:";
     26   for (it=myset.begin(); it!=myset.end(); ++it)
     27     std::cout << ' ' << *it;
     28   std::cout << '
    ';
     29 
     30   return 0;
     31 }
     32 
     33 
     34 // erasing from set
     35 #include <iostream>
     36 #include <set>
     37 
     38 int main ()
     39 {
     40   std::set<int> myset;
     41   std::set<int>::iterator it;
     42 
     43   // insert some values:
     44   for (int i=1; i<10; i++) myset.insert(i*10);  // 10 20 30 40 50 60 70 80 90
     45 
     46   it = myset.begin();
     47   ++it;                                         // "it" points now to 20
     48 
     49   myset.erase (it);
     50 
     51   myset.erase (40);
     52 
     53   it = myset.find (60);
     54   myset.erase (it, myset.end());
     55 
     56   std::cout << "myset contains:";
     57   for (it=myset.begin(); it!=myset.end(); ++it)
     58     std::cout << ' ' << *it;
     59   std::cout << '
    ';
     60 
     61   return 0;
     62 }
     63 
     64 
     65 // swap sets
     66 #include <iostream>
     67 #include <set>
     68 
     69 main ()
     70 {
     71   int myints[]={12,75,10,32,20,25};
     72   std::set<int> first (myints,myints+3);     // 10,12,75
     73   std::set<int> second (myints+3,myints+6);  // 20,25,32
     74 
     75   first.swap(second);
     76 
     77   std::cout << "first contains:";
     78   for (std::set<int>::iterator it=first.begin(); it!=first.end(); ++it)
     79     std::cout << ' ' << *it;
     80   std::cout << '
    ';
     81 
     82   std::cout << "second contains:";
     83   for (std::set<int>::iterator it=second.begin(); it!=second.end(); ++it)
     84     std::cout << ' ' << *it;
     85   std::cout << '
    ';
     86 
     87   return 0;
     88 }
     89 
     90 
     91 // set::clear
     92 #include <iostream>
     93 #include <set>
     94 
     95 int main ()
     96 {
     97   std::set<int> myset;
     98 
     99   myset.insert (100);
    100   myset.insert (200);
    101   myset.insert (300);
    102 
    103   std::cout << "myset contains:";
    104   for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    105     std::cout << ' ' << *it;
    106   std::cout << '
    ';
    107 
    108   myset.clear();
    109   myset.insert (1101);
    110   myset.insert (2202);
    111 
    112   std::cout << "myset contains:";
    113   for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    114     std::cout << ' ' << *it;
    115   std::cout << '
    ';
    116 
    117   return 0;
    118 }
    View Code
    find() 返回一个指向被查找到元素的迭代器
    count() 返回某个值元素的个数
    lower_bound() 返回指向大于(或等于)某值的第一个元素的迭代器
    upper_bound() 返回大于某个值元素的迭代器
     1 // set::find
     2 #include <iostream>
     3 #include <set>
     4 
     5 int main ()
     6 {
     7   std::set<int> myset;
     8   std::set<int>::iterator it;
     9 
    10   // set some initial values:
    11   for (int i=1; i<=5; i++) myset.insert(i*10);    // set: 10 20 30 40 50
    12 
    13   it=myset.find(20);
    14   myset.erase (it);
    15   myset.erase (myset.find(40));
    16 
    17   std::cout << "myset contains:";
    18   for (it=myset.begin(); it!=myset.end(); ++it)
    19     std::cout << ' ' << *it;
    20   std::cout << '
    ';
    21 
    22   return 0;
    23 }
    24 
    25 
    26 // set::count
    27 #include <iostream>
    28 #include <set>
    29 
    30 int main ()
    31 {
    32   std::set<int> myset;
    33 
    34   // set some initial values:
    35   for (int i=1; i<5; ++i) myset.insert(i*3);    // set: 3 6 9 12
    36 
    37   for (int i=0; i<10; ++i)
    38   {
    39     std::cout << i;
    40     if (myset.count(i)!=0)
    41       std::cout << " is an element of myset.
    ";
    42     else
    43       std::cout << " is not an element of myset.
    ";
    44   }
    45 
    46   return 0;
    47 }
    48 
    49 
    50 // set::lower_bound/upper_bound
    51 #include <iostream>
    52 #include <set>
    53 
    54 int main ()
    55 {
    56   std::set<int> myset;
    57   std::set<int>::iterator itlow,itup;
    58 
    59   for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
    60 
    61   itlow=myset.lower_bound (30);                //       ^
    62   itup=myset.upper_bound (60);                 //                   ^
    63 
    64   myset.erase(itlow,itup);                     // 10 20 70 80 90
    65 
    66   std::cout << "myset contains:";
    67   for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    68     std::cout << ' ' << *it;
    69   std::cout << '
    ';
    70 
    71   return 0;
    72 }
    View Code

    例题:http://poj.org/problem?id=3050

    Hopscotch

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5441   Accepted: 3582

    Description

    The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of numbered boxes into which to hop, the cows create a 5x5 rectilinear grid of digits parallel to the x and y axes. 

    They then adroitly hop onto any digit in the grid and hop forward, backward, right, or left (never diagonally) to another digit in the grid. They hop again (same rules) to a digit (potentially a digit already visited). 

    With a total of five intra-grid hops, their hops create a six-digit integer (which might have leading zeroes like 000201). 

    Determine the count of the number of distinct integers that can be created in this manner.

    Input

    * Lines 1..5: The grid, five integers per line

    Output

    * Line 1: The number of distinct integers that can be constructed

    Sample Input

    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 2 1
    1 1 1 1 1

    Sample Output

    15

    Hint

    OUTPUT DETAILS: 
    111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, and 212121 can be constructed. No other values are possible.

    Source

     

    题目大意:

    有一个 5*5 矩阵,你可以在矩阵里朝上下左右行走五步,起点任意。问能走出多少种不同的序列。

    大概思路:

    DFS + set去重

     

    AC code(492K 32MS):

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <set>
     5 #define INF 0x3f3f3f3f
     6 using namespace std;
     7 
     8 int mp[6][6];
     9 set<int> S;
    10 int ans;
    11 
    12 void dfs(int x, int y, int step, int sum)
    13 {
    14     if(step == 6)
    15     {
    16         sum = sum*10 + mp[x][y];
    17         S.insert(sum);
    18         //ans = S.size();
    19         //printf("%d
    ", ans);
    20         return;
    21     }
    22     else
    23     {
    24         sum = sum*10 + mp[x][y];
    25         if(x-1 >= 1) dfs(x-1, y, step+1, sum);
    26         if(x+1 <= 5) dfs(x+1, y, step+1, sum);
    27         if(y-1 >= 1) dfs(x, y-1, step+1, sum);
    28         if(y+1 <= 5) dfs(x, y+1, step+1, sum);
    29     }
    30     return;
    31 }
    32 
    33 int main()
    34 {
    35     for(int i = 1; i <= 5; i++)
    36     for(int j = 1; j <= 5; j++)
    37     {
    38         scanf("%d", &mp[i][j]);
    39     }
    40 
    41     for(int i = 1; i <= 5; i++)
    42     for(int j = 1; j <= 5; j++)
    43     {
    44         dfs(i, j, 1, 0);
    45     }
    46     ans = S.size();
    47     printf("%d
    ", ans);
    48     return 0;
    49 }
    View Code

    例题:https://www.lydsy.com/JudgeOnline/problem.php?id=1588

    1588: [HNOI2002]营业额统计

    Time Limit: 5 Sec  Memory Limit: 162 MB
    Submit: 19175  Solved: 8093

    Description

    营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。 Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况: 该天的最小波动值 当最小波动值越大时,就说明营业情况越不稳定。 而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。 第一天的最小波动值为第一天的营业额。  输入输出要求

    Input

    第一行为正整数 ,表示该公司从成立一直到现在的天数,接下来的n行每行有一个整数(有可能有负数) ,表示第i
    天公司的营业额。
    天数n<=32767,
    每天的营业额ai <= 1,000,000。
    最后结果T<=2^31

    Output

    输出文件仅有一个正整数,即Sigma(每天最小的波动值) 。结果小于2^31 。

    Sample Input

    6
    5
    1
    2
    5
    4
    6

    Sample Output

    12

    HINT

    结果说明:5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12

    题目大意:显而易见

    大概思路:set + lower_bound() 求前驱或者后继

     

    AC code:

     1 #include <bits/stdc++.h>
     2 #define INF 0x3f3f3f3f
     3 #define ll long long int
     4 using namespace std;
     5 
     6 set<ll> T;
     7 int N;
     8 
     9 int main()
    10 {
    11     scanf("%d", &N);
    12     set<ll>::iterator it;
    13     if(N == 0)
    14     {
    15         printf("0
    ");
    16         return 0;
    17     }
    18     ll res = 0;
    19     ll p = 0;
    20     N--;
    21     scanf("%lld", &p);
    22     res+=p;
    23     T.insert(p);
    24 
    25     while(N--)
    26     {   ll t = INF;
    27         scanf("%lld", &p);
    28         it = T.lower_bound(p);
    29         if(it != T.end())
    30         {
    31             t = min(t, abs((*it)-p));
    32         }
    33         if(it != T.begin())
    34         {
    35             t = min(t, abs((*--it)-p));
    36         }
    37         res+=t;
    38         T.insert(p);
    39     }
    40     printf("%lld
    ", res);
    41 
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    Django环境配置
    python pip使用报错: Fatal error in launcher: Unable to create process using '"c:python27python.exe" "C:Python27Scriptspip.exe" '
    我的第100篇博客
    测试代码(测试函数)
    python 存储数据
    python 异常之进阶操作
    从实战的角度谈微服务(三):基于Feign的熔断
    从实战的角度谈微服务(三):基于Feign服务间的调用
    从实战的角度谈微服务(二):服务注册与发现(Eureka服务端)
    从实战的角度谈微服务(一):初识微服务
  • 原文地址:https://www.cnblogs.com/ymzjj/p/9401355.html
Copyright © 2020-2023  润新知