- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序中有一个 boost::multi_index
容器,它包含对象的共享指针 (std::shared_ptr
)。正如我从文档中了解到的那样,它创建了具有对唯一索引的双向访问的树结构。是否可以告诉容器有一个数组结构,可以随机访问唯一索引(类似于 boost::flat_set
)?
最佳答案
当然,您可以完全按照您的描述进行操作:
#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
#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 << " ";
打印
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/
这是我的 multi_index 代码: struct tag_type {}; typedef boost::multi_index_container, b
class ObjectStorage { private: std::string objName; int zIndex; // Refer
2个问题: 1. 我真的需要通过成员将参数传递给 modify/modify_key 吗? 2. 为什么会出现这个编译错误 要查看包含错误的完整代码,您可以查看http://coliru.stacke
我有一个包含多个 hashed_unique 索引(A、B、C)的 multi_index。 并非所有对哈希的插入都包含所有索引的信息。一些索引可能是空的。有些可能包含所有条目。 我将它们定义为 ha
为了避免 msvc2010 编译器错误,我在 composite_key 中使用用户定义的 key 提取器,如下所示: enum NodeType { TypeOne = 0, T
首先我想展示工作代码,然后解释我想如何改变。这是简单的 boost multi_index 示例: //main.cpp #include #include #inc
我有一个带有多个索引的 boost multi_index 容器。如何使用迭代时指定的自定义比较来迭代元素。 例如,假设 Element::name 和 Element::index 由 multi_
所以。我正在使用 igraph 对象,我想以特定顺序迭代顶点。顺序由称为“值”的顶点属性确定,我想按从高到低的顺序进行操作。 igraph 可以按顶点 ID 顺序将所有值作为 igraph_vecto
我想按顺序搜索 boost::multi_index 容器并按顺序获取下一个元素。 下面的代码存储了四个具有不同索引(顺序和有序)的 float 。 最后一个 if 语句是问题所在。我不知道如何编辑以
我的应用程序中有一个 boost::multi_index 容器,它包含对象的共享指针 (std::shared_ptr)。正如我从文档中了解到的那样,它创建了具有对唯一索引的双向访问的树结构。是否可
阅读 boost::multi_index 引用文献,我发现 iterator_to 方法具有恒定顺序。这怎么可能?我的意思是,如果迭代器是一个不同于它所代表的 value_type 的对象,容器怎么
其定义如下: typedef boost::multi_index_container, boost::multi_index::member, boost::mult
我正在尝试制作一个 boost::multi_index 容器,它使用带参数的成员函数作为键。 class Data { public: std::string get(const std::s
我有一个 boost::multi_index 容器。谁能告诉我如何根据某个键检索一系列迭代器?经过几个小时的搜索,我想到 lower_bound 或 upper_bound 应该可以解决问题,但我仍
这个问题在这里已经有了答案: Where and why do I have to put the "template" and "typename" keywords? (8 个答案) 关闭 5
我正在应用 boost multi_index 示例的示例 6。 https://www.boost.org/doc/libs/1_71_0/libs/multi_index/doc/examples
我有以下(简化的)代码: #include #include namespace bmi = boost::multi_index; #include #include #include u
我想在内部实现 boost 多索引两组具有相同搜索条件但不同驱逐条件的键。假设我有两组具有相同搜索条件的数据,但一组需要 100 的 MRU(最近使用)列表,另一组需要 200 的 MRU。假设条目是
我有这个结构: struct myData { unsigned long id; int age; int phone; myData(){}; myDa
假设我有一个对象,可以通过来自不同 namespace 的不同名称来标识: enum Namespace { nspaceA, nspaceB }; struct Object {
我是一名优秀的程序员,十分优秀!