• STL


     

     

     

     

     

     

      1 #include<set>
      2 #include<iostream>
      3 
      4 using namespace std;
      5 
      6 void printf(set<int>& s)
      7 {
      8     if (s.empty())
      9     {
     10         cout << "这玩意是空的" << endl;
     11     }
     12     else
     13     {
     14         for (auto& a:s)
     15         {
     16             cout << a << " ";
     17         }
     18         cout << endl;
     19     }
     20     
     21 }
     22 
     23 //插入与查找 。不能随机访问,插入后,自动排序,从小到大
     24 void test01()
     25 {
     26     set<int> s1;
     27     s1.insert(19);
     28     s1.insert(20);
     29     s1.insert(14);
     30     s1.insert(2);
     31     s1.insert(8);
     32     printf(s1);
     33     //find
     34     set<int>::iterator itr1 = s1.find(14);
     35     if (itr1 == s1.end())     cout << "can not be found !" << endl;
     36     else                      cout << " be found !" << endl;
     37     //lower_bound
     38     set<int>::iterator itr2 = s1.lower_bound(8);// 返回 >=8 的迭代器
     39     if (itr2 == s1.end())     cout << "can not be found !" << endl;
     40     else                      cout << "lower_bound = " << *itr2 << endl;
     41     //upper_bound
     42     set<int>::iterator itr3 = s1.upper_bound(8);// 返回 > 8 的迭代器
     43     if (itr3 == s1.end())     cout << "can not be found !" << endl;
     44     else                      cout << "upper_bound = " << *itr3 << endl;
     45     //equal_range = lower_bound + upper_bound
     46     typedef set<int>::iterator setItr;
     47     pair<setItr, setItr> itr4 = s1.equal_range(8);
     48     if (itr4.first == s1.end())     cout << "can not be found !" << endl;
     49     else                            cout << "equal_range first = " << *itr4.first << endl;
     50     if (itr4.second == s1.end())    cout << "can not be found !" << endl;
     51     else                            cout << "equal_range second = " << *itr4.second << endl;
     52 }
     53 
     54 void test02()
     55 {
     56     //<1>
     57     pair<string, int> key_value1("shiruiyu", 10);
     58     pair<string, int> key_value2 = make_pair("maozedong", 99);
     59     //<2>
     60 }
     61 
     62 //仿函数,起始是一个类
     63 class MyCampcare
     64 {
     65 public:
     66     bool operator()(int a, int b)
     67     {
     68         return a > b;
     69     }
     70 };
     71 
     72 void test03()
     73 {
     74     set<int, MyCampcare> set1;
     75     set1.insert(22);
     76     set1.insert(1);
     77     set1.insert(10);
     78     set1.insert(12);
     79     //从大到小 打印
     80     for (auto& set_:set1)
     81     {
     82         cout << set_ << " ";
     83     }
     84     cout << endl;
     85 }
     86 
     87 class Person
     88 {
     89 public:
     90     Person(int age, int id) :age_(age), id_(id) {};
     91     ~Person() {};
     92     int age_;
     93     int id_;
     94 };
     95 
     96 //仿函数
     97 class MyCampare_
     98 {
     99 public:
    100     bool operator()(Person p1, Person p2)
    101     {
    102         return p1.id_ > p2.id_;
    103     }
    104 };
    105 
    106 void test04()
    107 {
    108     set<Person, MyCampare_> set1;// 按照id_ 从大到小排序    
    109     Person p1(20, 1), p2(21, 4), p3(23, 2), p4(22, 3);
    110     set1.insert(p1);
    111     set1.insert(p2);
    112     set1.insert(p3);
    113     set1.insert(p4);
    114 }
    115 
    116 int main()
    117 {
    118     test01();
    119     test02();
    120     test03();
    121     test04();
    122     return 1;
    123 }
  • 相关阅读:
    Python语言解析xml文件
    运行manage.py db shell出错
    ImportError: No module named win32com.client
    ImportError: No module named urllib2
    《演讲之禅》 读书笔记
    no such table: django_admin_log
    2 策略模式(2)
    1 简单工厂模式
    2 策略模式(1)
    无言
  • 原文地址:https://www.cnblogs.com/winslam/p/9426466.html
Copyright © 2020-2023  润新知