Ori*_*ion 1 c++ iterator g++ c++11
根据SGI,cplusplus.com以及我得到的所有其他源代码,std :: list的sort()成员函数不应使迭代器无效.但是,当我运行此代码时,情况似乎并非如此(c ++ 11):
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include "print.hpp"
unsigned int seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
std::uniform_int_distribution<unsigned int> distribution(1, 1000000000);
auto rng = std::bind(distribution, generator);
// C++11 RNG stuff. Basically, rng() now gives some unsigned int [1, 1000000000]
int main() {
unsigned int values(0);
std::cin >> values; // Determine the size of the list
std::list<unsigned int> c;
for (unsigned int n(0); n < values; ++n) {
c.push_front(rng());
}
auto c0(c);
auto it(c.begin()), it0(c0.begin());
for (unsigned int n(0); n < 7; ++n) {
++it; // Offset these iterators so I can print 7 values
++it0;
}
std::cout << "With seed: " << seed << "\n";
std::cout << "Unsorted list: \n";
print(c.begin(), c.end()) << "\n";
print(c.begin(), it) << "\n\n";
auto t0 = std::chrono::steady_clock::now();
c0.sort();
auto d0 = std::chrono::steady_clock::now() - t0;
std::cout << "Sorted list: \n";
print(c0.begin(), c0.end()) << "\n";
print(c0.begin(), it0) << "\n"; // My own print function, given further below
std::cout << "Seconds: " << std::chrono::duration<double>(d0).count() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在print.hpp中:
#include <iostream>
template<class InputIterator>
std::ostream& print(InputIterator begin, const InputIterator& end,
std::ostream& out = std::cout) {
bool first(true);
out << "{";
for (; begin != end; ++begin) {
if (first) {
out << (*begin);
first = false;
} else {
out << ", " << (*begin);
}
}
out << "}";
return out;
}
Run Code Online (Sandbox Code Playgroud)
样本输入/输出:
11
With seed: 3454921017
Unsorted list:
{625860546, 672762972, 319409064, 8707580, 317964049, 762505303, 756270868, 249266563, 224065083, 843444019, 523600743}
{625860546, 672762972, 319409064, 8707580, 317964049, 762505303, 756270868}
Sorted list:
{8707580, 224065083, 249266563, 317964049, 319409064, 523600743, 625860546, 672762972, 756270868, 762505303, 843444019}
{8707580, 224065083}
Seconds: 2.7e-05
Run Code Online (Sandbox Code Playgroud)
除了打印之外,一切都按预期工作.它应该显示7个元素,但实际数字相当偶然,只要"值"设置为大于7.有时它不提供,有时它给1,有时10,有时7等等.所以,有没有我的代码有些明显错误,或者这是否表明g ++的std :: list(和std :: forward_list)不符合标准?
提前致谢!
Ste*_*sop 10
迭代器仍然有效,仍然引用已重新排序的列表中的相同元素.
所以我不认为你的代码符合你的想法.它将列表从开头打印到列表排序后第7个元素结束的位置.因此,它打印的元素数量取决于列表中的值.
请考虑以下代码:
#include <list>
#include <iostream>
int main() {
std::list<int> l;
l.push_back(1);
l.push_back(0);
std::cout << (void*)(&*l.begin()) << "\n";
l.sort();
std::cout << (void*)(&*l.begin()) << "\n";
}
Run Code Online (Sandbox Code Playgroud)
打印的两个地址不同,表明(不像std::sort),std::list::sort通过更改元素之间的链接进行排序,而不是通过为元素分配新值.
我一直认为这是强制性的(同样适用reverse()).我实际上并不能找到明确的文字这么说,但如果你看的描述merge,并认为该理由list::sort存在是可能因为归并与列表工作得很好,那么我认为这是"明显"之意.merge说,"指针和x的移动元素的引用现在引用那些相同的元素,但作为*this的成员"(23.3.5.5./23),以及包含merge和sort说"由于列表允许快速" 的部分的开头从列表中间插入和删除,专门为它们提供了某些操作"(23.3.5.5/1).