我想一次准确地分配一个对象并将其推送到几个列表中。我怎么能用 and 做到这boost::intrusive_ptr
一点boost::intrusive::list
?或者我应该使用另一个容器和引用计数器?
问问题
231 次
1 回答
0
侵入式列表和 intrusive_ptr 不相关。
- 一个来自“Boost Intrusive”库
- 另一个是来自“Boost SmartPtr”库的智能指针
你可以使用任何一个。侵入式容器具有更多的管理功能,但每个容器都需要一个专用的钩子。
intrusive_ptr
更灵活,因为它可以通过使用引用计数来支持无界容器。
此外,请记住,如果您只想对相同的基础数据进行多个索引,通常您可以使用 Boost MultiIndex 并获得两全其美的效果。
侵入性列表
最简单的演示:Live On Compiler Explorer
#include <string>
#include <boost/intrusive/list.hpp>
#include <boost/intrusive/list_hook.hpp>
// for debug output
#include <fmt/ranges.h>
namespace bi = boost::intrusive;
using BaseHook = bi::list_base_hook<>;
using MemberHook = bi::list_member_hook<>;
struct Item : BaseHook {
std::string data;
Item(std::string data) : data(std::move(data)) {}
MemberHook _secondary, _secondary2;
bool operator<(Item const& rhs) const { return data < rhs.data; }
};
template <> struct fmt::formatter<Item> : fmt::formatter<std::string> {
template <typename Ctx>
auto format(Item const&val, Ctx &ctx) { return fmt::format_to(ctx.out(), "'{}'", val.data); }
};
// primary container uses the base hook
using Items = bi::list<Item>;
using Secondary = bi::list<Item, bi::member_hook<Item, MemberHook, &Item::_secondary >>;
using Third = bi::list<Item, bi::member_hook<Item, MemberHook, &Item::_secondary2 >>;
int main() {
Item elements[] { {"one"}, {"two"}, {"three"} };
Items primary(std::begin(elements), std::end(elements));
Secondary idx1(primary.begin(), primary.end());
Third idx2(primary.begin(), primary.end());
idx1.sort();
idx2.reverse();
fmt::print("primary: {}\n", primary);
fmt::print("idx1: {}\n", idx1);
fmt::print("idx2: {}\n", idx2);
}
印刷
primary: {'one', 'two', 'three'}
idx1: {'one', 'three', 'two'}
idx2: {'three', 'two', 'one'}
使用intrusive_ptr
请注意,这是如何更加动态的,但因此成本更高。另请注意,由于容器元素不再是“直接值”,所有操作都因为间接而变得更加复杂:
- 插入需要动态分配 (
new
) - sort 需要一个谓词,以免您对指针进行排序
- 打印需要间接
我在这里使用了各种其他 Boost 助手来节省时间,但请注意,这种复杂性可能会更多地降低您的开发速度,具体取决于您是否了解这些助手位
#include <string>
#include <boost/intrusive_ptr.hpp>
#include <boost/smart_ptr/intrusive_ref_counter.hpp>
#include <list>
// helpers to make handling containers of intrusive pointers easier
#include <boost/range/adaptor/indirected.hpp>
#include <boost/ptr_container/indirect_fun.hpp>
#include <memory>
// for debug output
#include <fmt/ranges.h>
struct Item : boost::intrusive_ref_counter<Item> {
std::string data;
Item(std::string data) : data(std::move(data)) {}
bool operator<(Item const& rhs) const { return data < rhs.data; }
};
template <> struct fmt::formatter<Item> : fmt::formatter<std::string> {
template <typename Ctx>
auto format(Item const&val, Ctx &ctx) { return fmt::format_to(ctx.out(), "'{}'", val.data); }
};
int main() {
using List = std::list<boost::intrusive_ptr<Item> >;
List primary;
for (auto name : {"one","two","three"}) {
primary.emplace_back(new Item(name));
}
List idx1(primary.begin(), primary.end());
List idx2(primary.begin(), primary.end());
idx1.sort(boost::make_indirect_fun(std::less<Item>{}));
idx2.reverse();
fmt::print("primary: {}\n", primary | boost::adaptors::indirected);
fmt::print("idx1: {}\n", idx1 | boost::adaptors::indirected);
fmt::print("idx2: {}\n", idx2 | boost::adaptors::indirected);
}
印刷
primary: {'one', 'two', 'three'}
idx1: {'one', 'three', 'two'}
idx2: {'three', 'two', 'one'}
奖励:多索引容器
你没有要求这个,但给定样本似乎是一个合乎逻辑的解决方案(我意识到样本可能不代表你的问题,当然):
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
// for debug output
#include <fmt/ranges.h>
struct Item {
std::string data;
bool operator<(Item const& rhs) const { return data < rhs.data; }
};
template <> struct fmt::formatter<Item> : fmt::formatter<std::string> {
template <typename Ctx>
auto format(Item const&val, Ctx &ctx) { return fmt::format_to(ctx.out(), "'{}'", val.data); }
};
namespace bmi = boost::multi_index;
using Table = bmi::multi_index_container<Item,
bmi::indexed_by<
bmi::sequenced<>, // primary
bmi::sequenced<bmi::tag<struct Index1> >,
bmi::sequenced<bmi::tag<struct Index2> >
> >;
int main() {
Table primary{ {"one"},{"two"},{"three"} };
auto& idx1 = primary.get<Index1>();
auto& idx2 = primary.get<Index2>();
idx1.sort();
idx2.reverse();
fmt::print("primary: {}\n", primary);
fmt::print("idx1: {}\n", idx1);
fmt::print("idx2: {}\n", idx2);
}
印刷
primary: {'one', 'two', 'three'}
idx1: {'one', 'three', 'two'}
idx2: {'three', 'two', 'one'}
后者具有 - 迄今为止 - 最大的灵活性和最高级别的语义(例如,删除元素适用于所有索引)。您可以拥有关联索引、有序键、复合键等。在这种情况下,它们将自动维护在replace
or上modify
。
当然,如果您的用例不像固定集合的索引,那么您可能需要第一个选项。
于 2020-08-11T14:42:04.317 回答