gpt4 book ai didi

c++ - boost multi_index - 如何让它按插入排序?

转载 作者:行者123 更新时间:2023-11-28 04:39:48 25 4
gpt4 key购买 nike

其定义如下:

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_access 提供类似 std::vector 的访问权限

另请注意,您定义的容器 不会以任何特定顺序保存内容,它具有类似 std::unordered_set 的访问权限只有索引。

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

关于c++ - boost multi_index - 如何让它按插入排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50488037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com