`sTD :: sample()的输出序列是否遵循输入序列的顺序
Does the output sequence of `std::sample()` follow the order of the input sequence?
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
在您的情况下, PopulationIterator
是 std::vector<int>::iterator
,确实是 legacyforwarderator (实际上是a legacyrandomAccessiterator ),所以,是的,这些元素应具有相同的元素output
与input
一样。
相关文章:
- 检查 2 棵树是否具有相同的顺序
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- C++编译器是否优化了顺序静态变量读取?
- 同一互斥锁顺序上的锁定和解锁是否一致?
- C++评估顺序优化是否意味着对不同的操作数使用不同的内核?
- 标记为 std::memory_order_seq_cst 的单个原子操作是否会在所有位置触发顺序一致性?
- 如何检查参数包是否具有执行顺序中的确切类型
- 视觉C++:在 DLL 加载期间,全局变量初始化顺序是否具有确定性?
- c++标准是否指定了运算符&&(内置)的求值顺序?
- 顺序一致的原子负载(负载-负载对)是否形成线程间同步点
- 在C++中,是否可以在单个循环中顺序迭代多个迭代器
- 初始化值是否保证通过其自己的地址反映,而不考虑内存顺序
- 如果 RMW 操作没有任何变化,是否可以针对所有内存顺序对其进行优化
- 是否可以跨动态库控制销毁顺序?
- 验证(使用 static_assert)元组类型是否遵循某种顺序(有状态编译时检查)
- 委派的 ctor 是否受参数计算顺序的影响?
- 如何检查 2 个 c++ 数组在 O(1) 或 O(log n) 时间复杂度中是否相同(所有元素都相同,顺序很重要)?
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- 编译器是否会简化按顺序执行多次的操作
- C++是否保证标准容器比较中操作数的顺序?