2013-12-07 51 views
0
for(set<int> it = my_set.begin; it!= my_set.end; ++it) 
for(set<int> other = next(it); other != my_set.end; ++other) 

这将不会编译,因为std :: next有关如何实现这样的循环的任何建议?我也尝试使用advance,但它也不起作用。 (注意我知道循环中有一些语法错误)。请勿启用库函数。双向迭代器Next

+1

能否请你上你更清晰”重新尝试实现以外的其他方式来编译它? – 2013-12-07 21:34:42

回答

1

在C++ 03可以使用预先这样说:

for(std::set<int>::iterator it = my_set.begin(); it != my_set.end(); ++it) { 
    std::set<int>::iterator copy = it; 
    std::advance(copy, 1); 
    for(; copy != my_set.end(); ++copy) { 
     std::cout << *copy << std::endl; 
    } 
} 

在C++ 11可以使用未来代替:

for(auto it = my_set.begin(); it != my_set.end(); ++it) { 
    for(auto other = std::next(it); other != my_set.end(); ++other) { 
     std::cout << *other << std::endl; 
    } 
} 
+0

在第一个示例中,“++复制”看起来更简单。 –

+0

不工作接下来不会编译...。我结束了在第二个循环中使用反向迭代器,并停止* other == *它 –

+0

@GoBlue就像我说的,它需要C++ 11,所以你能解释'你不会编译'的意思吗? – 2013-12-07 22:57:51