使用std :: list作为循环列表是否安全? [英] Is it safe to use std::list as a circular list?
本文介绍了使用std :: list作为循环列表是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以递增或递减end()迭代器是在标准中定义的吗?在linux上,begin()实现为end()++。
So incrementing or decrementing the end() iterator is defined in the standard? On linux, the begin() is implemented as end()++.
#include <list>
#include <iostream>
int main()
{
std::list<int> numbers;
for (int i = 0; i < 10; i++)
numbers.push_back(i);
auto it = numbers.begin();
int count = 3;
while (count)
{
std::cout << *it++;
if (it == numbers.end())
{
++it; // is this ok ???
--count;
std::cout << '\n';
}
}
}
所以输出总是一样的在每个平台上?
So the output always the same on every platform?
输出:
0123456789
0123456789
0123456789
推荐答案
增加从中返回的迭代器任何标准C ++库容器的 end()
都会导致未定义的行为。由于 std :: list< T>
的大多数实现所共有的实现细节,它可能会增加 list.end()
但不保证确实如此。
Incrementing the iterator returned from end()
of any of the standard C++ library containers results in undefined behavior. Due to an implementation detail common to most implementations of std::list<T>
it may work to increment list.end()
but there is no guarantee that it does.
这篇关于使用std :: list作为循环列表是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文