zoukankan      html  css  js  c++  java
  • set的使用

     1 // constructing sets
     2 #include <iostream>
     3 #include <set>
     4 
     5 bool fncomp (int lhs, int rhs) {return lhs<rhs;}
     6 
     7 struct classcomp {
     8     bool operator() (const int& lhs, const int& rhs) const
     9     {return lhs<rhs;}
    10 };
    11 
    12 int main ()
    13 {
    14     std::set<int> first;                           // empty set of ints
    15 
    16     std::cout << "first size : " << first.size() << std::endl;
    17     
    18     int myints[]= {10,20,30,40,50};
    19     std::set<int> second (myints,myints+5);        // range
    20     std::cout << "second size : " << second.size() << std::endl;
    21 
    22     std::set<int> third (second);                  // a copy of second
    23     std::cout << "third size : " << third.size() << std::endl;
    24     
    25     std::set<int> fourth (second.begin(), second.end());  // iterator ctor.
    26     std::cout << "fourth size : " << fourth.size() << std::endl;
    27 
    28     std::set<int,classcomp> fifth;                 // class as Compare
    29     std::cout << "fifth size : " << fifth.size() << std::endl;
    30 
    31     bool(*fn_pt)(int,int) = fncomp;
    32     std::set<int,bool(*)(int,int)> sixth (fn_pt);  // function pointer as Compare
    33     std::cout << "sixth size : " << sixth.size() << std::endl;
    34     
    35     return 0;
    36 }

    运行结果:

  • 相关阅读:
    Golang判断文件/文件夹是否存在
    Golang将一个文件中的内容写入到另一个文件里
    Golang打开已存在的文件并覆盖其内容
    Golang打开文件并写入
    【正则】day01
    【正则】day02
    【day09】PHP
    【day08】PHP
    转换函数
    变量作用域
  • 原文地址:https://www.cnblogs.com/wanmeishenghuo/p/13551236.html
Copyright © 2011-2022 走看看