gpt4 book ai didi

c++ - boost::multi_index 随机访问唯一索引

转载 作者:行者123 更新时间:2023-11-30 00:45:29 25 4
gpt4 key购买 nike

我的应用程序中有一个 boost::multi_index 容器,它包含对象的共享指针 (std::shared_ptr)。正如我从文档中了解到的那样,它创建了具有对唯一索引的双向访问的树结构。是否可以告诉容器有一个数组结构,可以随机访问唯一索引(类似于 boost::flat_set)?

最佳答案

没有多索引

当然,您可以完全按照您的描述进行操作:

Live On Coliru

#include <boost/container/flat_set.hpp>
#include <memory>
#include <vector>
#include <iostream>

template <typename V, typename... Init>
auto make_shared_flat_set(Init&&... values) {
return boost::container::flat_set<std::shared_ptr<V> > {
std::make_shared<V>(std::forward<Init>(values))...
};
}

int main() {
using sptr = std::shared_ptr<std::string>;
auto const set = make_shared_flat_set<std::string>("one", "two", "three", "four", "Hungary");

// the set-like interface: (ordered_unique)
std::cout << "set: ";
for (auto& e : set) std::cout << *e << " ";

{
// let's make a random access copy:
std::vector<sptr> random_access(set.begin(), set.end());
std::cout << "\ncopy: ";
for (auto& e : random_access) std::cout << *e << " ";
}

{
// let's make a random access view:
using spref = std::reference_wrapper<sptr const>;
std::vector<spref> random_access(set.begin(), set.end());
std::cout << "\nview: ";
for (sptr const& e : random_access) std::cout << *e << " ";
}
}

打印

set: one two three four Hungary 
copy: one two three four Hungary
view: one two three four Hungary

使用多索引

Live On Coliru

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <memory>
#include <iostream>

namespace bmi = boost::multi_index;

template <typename V> using Table = bmi::multi_index_container<std::shared_ptr<V>,
bmi::indexed_by<
bmi::random_access<bmi::tag<struct ra_idx> >,
bmi::ordered_unique<bmi::tag<struct set_idx>, bmi::identity<V> >
>
>;

template <typename V, typename... Init>
auto make_shared_flat_set(Init&&... values) {
return Table<std::string> {
std::make_shared<V>(std::forward<Init>(values))...
};
}

int main() {
auto const set = make_shared_flat_set<std::string>("one", "two", "three", "four", "Hungary");

// the set-like interface: (ordered_unique)
auto& as_set = set.get<set_idx>();

std::cout << "set: ";
for (auto& e : as_set) std::cout << *e << " ";

// the random access interface:
std::cout << "\nrandom access: ";
auto& random_access = set.get<ra_idx>();
for (auto& e : random_access) std::cout << *e << " ";
}

打印:

set: Hungary four one three two 
random access: one two three four Hungary

奖励:

要将 random_access 索引放入与集合相同的顺序,请使用重新排列:

// put the random access in the same order as the set:
random_access.rearrange(make_ro_view(as_set).begin());

std::cout << "\nrandom access (rearranged): ";
for (auto& e : random_access) std::cout << *e << " ";

Live On Coliru

打印

set: Hungary four one three two 
random access: one two three four Hungary
random access (rearranged): Hungary four one three two

关于c++ - boost::multi_index 随机访问唯一索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43191685/

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