容器set 解析
1. 例子
1 #include <boost/intrusive/set.hpp> 2 #include <utility> 3 #include <iostream> 4 #include <string> 5 6 using namespace boost::intrusive; 7 8 struct vehicle 9 { 10 std::string name; 11 int wheels; 12 set_member_hook<> set_hook; 13 vehicle(std::string n, int w) : name{std::move(n)}, wheels{w} {} 14 bool operator<(const vehicle &v) const { return wheels < v.wheels; } 15 }; 16 17 int main() 18 { 19 vehicle v1{"Car", 4}; 20 vehicle v2{"Bike", 2}; 21 vehicle v3{"Train", 24}; 22 vehicle v4{"Cycle", 2}; 23 vehicle v5{"Van", 3}; 24 25 typedef member_hook<vehicle, set_member_hook<>, &vehicle::set_hook> hook; 26 typedef set<vehicle, hook> vehicle_set; 27 vehicle_set vehicles; 28 29 vehicles.insert(v1); 30 vehicles.insert(v2); 31 vehicles.insert(v3); 32 vehicles.insert(v4); 33 vehicles.insert(v5); 34 35 for (const vehicle &a : vehicles) 36 std::cout << a.name << '\n'; 37 }