1

我目前正在使用 pugi xml,我经常使用这样的循环:

for (pugi::xml_node sth: root.child("name").children())
{
    //do something
}

然后在某个时候,我意识到我需要在哪个迭代中保存信息,我找到了一些价值,因为稍后会在这个循环之外需要它。我可以在不添加计数器的情况下判断我正在进行的迭代吗?

此外,如果该对象是这样的向量:

std::vector<type> vtr;
for (std::vector<type>::iterator it = vtr.begin(); it != vtr.end(); ++it)
{
    //which iteration?
}
4

1 回答 1

5

在任何给定的感兴趣的迭代器上,您都可以这样做

auto index = it - vtr.begin();

vtr.begin()索引也是如此0,然后每个元素从那里递增。
或者你可以使用std::distance.

auto index = std::distance(vtr.begin(), it)
于 2014-07-23T22:30:48.777 回答