迭代 std 容器中的所有元素对 (C++)
迭代 std 容器中所有元素对的最佳方法是什么,例如 std::list
、std::set
、std::vector
, ETC。?
基本上与此等效,但使用迭代器:
for (int i = 0; i < A.size()-1; i++)
for(int j = i+1; j < A.size(); j++)
cout << A[i] << A[j] << endl;
What's the best way to iterate over all pairs of elements in std container like std::list
, std::set
, std::vector
, etc.?
Basically to do the equivalent of this, but with iterators:
for (int i = 0; i < A.size()-1; i++)
for(int j = i+1; j < A.size(); j++)
cout << A[i] << A[j] << endl;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法就是按字面意思重写代码:
将
auto
替换为 C++98/03 的 const_iterator。或者将其放在自己的函数中:The easiest way is just rewriting the code literally:
Replace
auto
with a const_iterator for C++98/03. Or put it in its own function:只是为了遍历,使用 const_iterators。如果要修改值,请使用迭代器。
例子:
Just to traverse, use const_iterators. If you want to modify values, use iterator.
Example: