0
我剛開始使用boost multi_index,並且到目前爲止有一個容器,它包含一個排序類型和複合鍵unorderd哈希。通過size_t索引迭代到boost :: multi_index中的順序位置?
我想要做的就是訪問順序容器,就好像它是一個std :: vector或std :: list。我的意思是通過使用size_t index
。
這裏是我下面的代碼:
// tags
struct linear { };
struct semantic { };
struct semantic_and_index { };
// typedefs for multi_index
typedef boost::multi_index::sequenced<
boost::multi_index::tag<linear>
> LinearIndex;
typedef boost::multi_index::composite_key<
CVertexElement,
boost::multi_index::const_mem_fun<CVertexElement, Buffer::VertexSemantic, &CVertexElement::GetVertexSemantic>,
boost::multi_index::const_mem_fun<CVertexElement, UInt, &CVertexElement::GetSemanticIndex>
> CompositeSemanticIndex;
typedef boost::multi_index::hashed_non_unique<
boost::multi_index::tag<semantic_and_index>,
CompositeSemanticIndex
> SemanticAndIDIndex;
typedef boost::multi_index::hashed_non_unique<
boost::multi_index::tag<semantic>,
boost::multi_index::const_mem_fun<CVertexElement, Buffer::VertexSemantic, &CVertexElement::GetVertexSemantic>
> SemanticIndex;
class CVertexFormat
{
public:
typedef boost::multi_index::multi_index_container <
CVertexElementPtr,
boost::multi_index::indexed_by <
LinearIndex,
SemanticAndIDIndex,
SemanticIndex
> // index_by
> ElementContainer;
// etc...
protected:
ElementContainer m_elements;
};
我指的功能是:
// this function fails! :(
CVertexElementPtr CVertexFormat::GetElement(size_t index) const
{
//sequenced_index.
typedef ElementContainer::index<linear>::type element_by_linear;
const element_by_linear& index_type = m_elements.get<linear>();
if (index < index_type.size())
{
auto itor = index_type.begin();
for (UInt i = 0; i < index_type.size(); ++i)
++itor;
const CVertexElementPtr& pElement = (*itor);
return pElement;
}
else
{
assert(!"Invalid index called for GetElement");
return CVertexElementPtr();
}
}
圖片在VS2010錯誤的(看看手錶窗口右側):
究竟失敗?你遇到了什麼錯誤? – 2011-05-05 12:36:25
由於某種原因,迭代器無效。當我調試應用程序時,它說'm_elements'中有** 2個節點**。然而'index = 0'從'index_type.begin()'返回的'itor'是無效的......我不知道它是如何失效的?如果我也可以,我會附上一個屏幕截圖。 – Sent1nel 2011-05-05 12:49:21
其實我只是解決了它。它是'for(UInt i = 0; i
Sent1nel
2011-05-05 13:01:30