[英]How to traverse a recursive variant vector using Boost C++
我想建立一個向量樹,構造如下
struct myStruct {
int a;
string b;
};
typedef boost::make_recursive_variant<
myStruct *
, std::vector< boost::recursive_variant_ >
>::type myStruct_tree;
如果我附加多個向量,以及這些向量中的向量,我將如何使用某種位置向量遍歷樹
vector<int>
它在矢量指針樹中定義的每個矢量/子矢量中定位對象的位置。
我不確定100%理解您的問題,因此請澄清我是否弄錯了。 我假設向量是一條路徑,每個元素給出要移動到的元素的索引。 考慮到這一點,我會嘗試這樣的事情:
myStruct_tree t = ...;
myStruct_tree* tit = &t;
for(iterator pit=path.begin(); pit!=path.end(); ++pit)
{
// retrieve branches at current position
vector<myStruct_tree>& vec = get<2>(*tit);
// get next position in tree, will throw on bad index
tit = &vec.at(*pit);
}
assert(tit);
// retrieve the leaf at the final position
myStruct* res = get<1>(*tit);
筆記:
get<1>(*tit);
我在上面使用過的意思是檢索第一個元素(即myStruct指針)的那個。 此外,它應該是返回引用或引發錯誤以處理錯誤的引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.