【C++编程】容器set 解析

 容器set 解析

1. 例子

#include <boost/intrusive/set.hpp>
#include <utility>
#include <iostream>
#include <string>

using namespace boost::intrusive;

struct vehicle
{
  std::string name;
  int wheels;
  set_member_hook<> set_hook;
  vehicle(std::string n, int w) : name{std::move(n)}, wheels{w} {}
  bool operator<(const vehicle &v) const { return wheels < v.wheels; }
};

int main()
{
  vehicle v1{"Car", 4};
  vehicle v2{"Bike", 2};
  vehicle v3{"Train", 24};
  vehicle v4{"Cycle", 2};
  vehicle v5{"Van", 3};

  typedef member_hook<vehicle, set_member_hook<>, &vehicle::set_hook> hook;
  typedef set<vehicle, hook> vehicle_set;
  vehicle_set vehicles;

  vehicles.insert(v1);
  vehicles.insert(v2);
  vehicles.insert(v3);
  vehicles.insert(v4);
  vehicles.insert(v5);

  for (const vehicle &a : vehicles)
    std::cout << a.name << '\n';
}

 

posted @ 2021-12-24 10:06  苏格拉底的落泪  阅读(114)  评论(0编辑  收藏  举报