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 }
运行结果: