C++ boost multi_索引-如何使其按插入顺序排列?

C++ boost multi_索引-如何使其按插入顺序排列?,c++,boost,C++,Boost,其定义如下: typedef boost::multi_index_container< X, // the data type stored boost::multi_index::indexed_by< boost::multi_index::hashed_unique< boost::multi_index::composite_key< X, boost::multi_index::member<

其定义如下:

typedef boost::multi_index_container<
  X, // the data type stored
  boost::multi_index::indexed_by< 
    boost::multi_index::hashed_unique<
      boost::multi_index::composite_key<
        X,
        boost::multi_index::member<X,std::string,&X::name>,
        boost::multi_index::member<X,std::string,&X::p1>,
        boost::multi_index::member<X,std::string,&X::p2>
      >
    >
  >
> container;
我使用insert插入新项目

但容器使用字符串键对它们进行排序,而不是存储插入顺序


我需要将其存储。

您需要为要搜索的每个订单的定义添加一个索引

boost::multi_index::sequenced提供类似std::list的访问

boost::multi_index::random_访问提供了类似std::vector的访问

还要注意的是,您定义的容器并没有按任何特定顺序保存内容,它具有std::unordered_集,类似于对其唯一索引的访问

boost::multi_index_container<
  X, // the data type stored
  boost::multi_index::indexed_by< 
    boost::multi_index::random_access,
    boost::multi_index::hashed_unique<
      boost::multi_index::composite_key<
        X,
        boost::multi_index::member<X,std::string,&X::name>,
        boost::multi_index::member<X,std::string,&X::p1>,
        boost::multi_index::member<X,std::string,&X::p2>
      >
    >
  >
> container;

container.index<0>().erase(container.index<0>().begin() + 10) // insertion order
container.index<1>().find(some_value) // unordered

谢谢,但是在这种情况下,我不能使用find来处理这个容器,同时我希望能够使用它,或者更好的方法是删除具有指定索引的项,而不使用循环。@Simus您需要对相关索引进行操作。这就是为什么它被命名为multi_index_container