听不懂multi_index
Can't understand multi_index
class ObjectStorage
{
private:
std::string objName;
int zIndex;
// Reference for the Object interface
boost::shared_ptr<Object> mCppObject;
// Reference for the Python interface
boost::python::object mPythonObject;
public:
ObjectStorage(const std::string &name, int zIndex_, boost::shared_ptr<Object> cpp, boost::python::object python)
: objName(name), zIndex(zIndex_),
mCppObject(cpp), mPythonObject(python) {}
std::string getName() const { return objName; };
int getZIndex() const { return zIndex; }
boost::shared_ptr<Object> getCppObject() const { return mCppObject; }
boost::python::object getPythonObject() const { return mPythonObject; }
};
// Tagging for multi_index container
struct tag_zindex {};
struct tag_name {};
struct tag_cpp {};
struct tag_py {};
typedef boost::multi_index_container<ObjectStorage,
bmi::indexed_by<
// ZIndex
bmi::ordered_non_unique<
bmi::tag<tag_zindex>,
bmi::const_mem_fun<ObjectStorage, int, &ObjectStorage::getZIndex>
>,
// Name
bmi::ordered_unique<
bmi::tag<tag_name>,
bmi::const_mem_fun<ObjectStorage, std::string, &ObjectStorage::getName>
>,
// CPP reference
bmi::ordered_non_unique<
bmi::tag<tag_cpp>,
bmi::const_mem_fun<ObjectStorage, boost::shared_ptr<Object>, &ObjectStorage::getCppObject>
>,
// Python reference
bmi::ordered_unique<
bmi::tag<tag_py>,
bmi::const_mem_fun<ObjectStorage, boost::python::object, &ObjectStorage::getPythonObject>
>
>
> ObjectWrapperSet;
如果multi_index
中的第一个索引是正确的:对容器内的对象进行排序是指ZIndex
值,我不确定另一个索引。我需要这样的功能:按 ZIndex 排序,但在迭代时返回getCppObject
。是否可以不仅可以设置排序,还可以设置访问时的结果?
另外,例如tag_py
我想遍历所有getPythonObject
,而不是ObjectStorage
.这真的可以用multi_index
吗?
在您的情况下,multi_index_container
包含ObjectStorage
对象的实例。因此,您可以按任何顺序对其进行迭代,并调用ObjectStorage
类的任何函数。
例如,要使用tag_py
标签进行迭代:
ObjectWrapperSet ow_set;
ObjectWrapperSet::index_const_iterator<tag_py>::type it = ow_set.get<tag_py>().begin();
for ( ; it != ow_set.get<tag_py>().end(); ++it ) {
const ObjectStorage& os = *it; // note `it` is the iterator for ObjectStorage
// now you can do
boost::python::object po = os.getPythonObject();
// or
boost::python::object po = it->getPythonObject();
}
使用tag_zindex
标记:
ObjectWrapperSet::index_const_iterator<tag_zindex>::type it = ow_set.get<tag_zindex>().begin();
for ( ; it != ow_set.get<tag_zindex>().end(); ++it ) {
boost::shared_ptr<Object> cpp_obj = it->getCppObject();
// do something
}
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- DrawIndexedInstanced 具有不同的 Index Count per Instance (Directx
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 可视化C++:发布模式的运行时库作为'Multi-threaded Debug DLL'
- OpenGL VBO Indexing ( How to compute Index Array)
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 如何根据排序索引的向量对 std::index 集进行排序?
- 在一行代码中理解 index++
- 为什么打印 c 样式字符串的'address of index n'会导致子字符串的输出
- boost beast Websocket Multi Request Server/Client 并不是真正的 mul
- 使用 std::get 访问 std::variant<index>
- 代码中的"vector<<int>vector>&index"是什么意思?
- c++如何使用Curl Multi-Interface连续循环
- CStringArray::GetAt(int index) 返回一个 const. 为什么?
- 如何配置 Doxygen 以在"Class Index"中包含类的类型定义?
- 在 R 中调用"multi file C++ code with external libraries referenced"
- 如何使用 std::get 作为 boost-multi-index 容器键的global_fun
- Boost Multi Index标识索引
- Boost Multi Index:基于列表内容的索引
- Boost Multi-Index中的多个索引查询