`sTD :: sample()的输出序列是否遵循输入序列的顺序

Does the output sequence of `std::sample()` follow the order of the input sequence?

本文关键字:是否 顺序 输入 输出 sTD sample      更新时间:2023-10-16

std::sample()的输出序列是否遵循输入序列的顺序?

例如,

const std::vector<int> input{2, 4, 6, 8, 1, 3, 5, 7};
std::vector<int> output;
std::sample(input.begin(), input.end(), std::back_inserter(output), 3, any_urbg);

是否可以保证output永远不会成为[1, 2, 3]

std::sample状态的引用:

算法是稳定的(时,只有在 supersiterator 满足 legacyforwarderator

的要求时,仅保留所选元素的相对顺序)。

superiterator 在这里 std::vector的迭代器。std::vector的迭代器是A legacyrandomAccessiterator ,它满足 legaacybidirectionaliterator 的类型,又满足了 leg> legsacyforwarditerator 。。

的类型

所以,是的,可以保证永远不会成为[1, 2, 3],因为这会违反所选元素的相对顺序,即[2, 1, 3]

来自std ::样本:

算法是稳定的(保留所选元素的相对顺序),当superitiTerator符合LegacyForwardIterator

的要求时

根据https://en.cppreference.com/w/cpp/algorithm/sample:

只有在 PopulationIterator符合 legacyforwarditerator

的要求时,该算法是稳定的(保留所选元素的相对顺序)

在您的情况下, PopulationIteratorstd::vector<int>::iterator,确实是 legacyforwarderator (实际上是a legacyrandomAccessiterator ),所以,是的,这些元素应具有相同的元素outputinput一样。

相关文章: