如何使用Boost C到遍历的递归变型矢量++ [英] How to traverse a recursive variant vector using Boost C++
本文介绍了如何使用Boost C到遍历的递归变型矢量++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想建立一个矢量树结构如下:
I want to build a vector tree constructed as follows
struct myStruct {
int a;
string b;
};
typedef boost::make_recursive_variant<
myStruct *
, std::vector< boost::recursive_variant_ >
>::type myStruct_tree;
如果我追加多个向量,向量和这些载体,将如何使用某种位置矢量的我遍历树
If I append a multiple vectors, and vectors in those vectors, how would I traverse the tree using some kind of position vector
vector<int>
将查找在矢量指针树中定义每个矢量/子向量的对象的位置。
Which locates the objects position in each vector/subvector defined in the vector pointer tree.
推荐答案
我不是100%肯定我明白你的问题,所以如果我得到的东西错了,请澄清。我假定的载体是路径,每个元件使移动到元素的索引。考虑到这一点,我想尝试这样的:
I'm not 100% sure I understand your question, so please clarify if I get something wrong. I'm assuming the vector is a path, with each element giving the index of the element to move to. With that in mind, I'd try something like this:
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);
注:
- 使用一个无符号整数的指数会多一点自然的我。
- 矢量::在()抛出对超出范围的异常。
- 我没有抬头的如何获得变种的元素确切的语法。随着版本
获取。1 GT;(*针锋相对);
我上面使用,我的意思是检索的第一个元素(即MYSTRUCT指针)之一。另外,它应该是一个返回参考或以处理错误引发之一。 - 考虑更换,以提供更好的诊断用自己的code中的检查范围,。我没有做到这一点,保持清晰的算法的结构。
- Using an unsigned integer for the indices would be a bit more natural to me.
- vector::at() throws an exception on out-of-range.
- I haven't looked up the exact syntax of how to get an element of the variant. With the version
get<1>(*tit);
I used above, I meant the one that retrieves the first element (i.e. the myStruct pointer). Further, it should be the one that returns a reference or throws in order to handle errors. - Consider replacing the range-checking with your own code in order to provide better diagnostics. I didn't do it to keep the structure of the algorithm clear.
这篇关于如何使用Boost C到遍历的递归变型矢量++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文