如何使用next_permutation
how to use next_permutation
我想要一个井字游戏板的安排。所以我有下面的代码:
// 5 turns for x if x goes first
std::string moves = "xxxxxoooo";
do {
std::cout << moves << std::endl;
} while ( std::next_permutation(moves.begin(), moves.end()) );
但是它只输出原始字符串一次。我假设每个角色都是独一无二的。我该怎么做呢?
std::next_permutation
按字典顺序返回下一个排列,如果生成了第一个排列(按该顺序),则返回false
。
由于开头的字符串("xxxxxoooo"
)实际上是该字符串字符按字典顺序的最后一次排列,因此循环立即停止。
因此,在开始在循环中调用next_permutation()
之前,您可以尝试对moves
进行排序:
std::string moves = "xxxxxoooo";
sort(begin(moves), end(moves));
while (std::next_permutation(begin(moves), end(moves)))
{
std::cout << moves << std::endl;
}
下面是一个的实例
相关文章:
- 对于set上的循环-获取next元素迭代器
- 为什么 std::next 在以空地图开头馈送时卡住
- 为什么gmp会在这里与"invalid next size"重新定位一起崩溃?
- "fast"或"normal"在"free(): invalid next size (fast)"中是什么意思?
- curl_slist->next 永远不会为空,因此当循环卡住时
- std::next 是否检查我们是否已经在容器的末尾?
- Clang, std::next, libstdc++ and constexpr-ness
- 错误 C2864:'element::next':只能在类 (STRUCT) 中初始化静态常量整数数据成员
- 在 c++ 程序中调试链表以添加两个数字: 有人可以解释为什么 l3=l3->next;导致错误?
- head->next 不为空
- LinkedListInit, LinkedListCreateH 方法中的"L->next=NULL"有什么问题吗?
- C++ 带有 hasNext 和 Next 的迭代器
- 是 std::next 对于向量 O(n) 或 O(1)
- 如何检查STD :: Next(x)指向C 中的有效地址
- 跟进哈希表 C++,this->next = NULL 指针
- node.prev.next = .. 和 node = 之间的区别
- 如何获取链表中的所有"next"节点?构建调用字符串不起作用
- GRPC C 称为PloteimionQueue关闭,但PostemionQueue.Next在有待处理事件时永远阻
- 是否必须在内存中"next to each other"两个相邻声明的公共字段?
- C :在多迭代器中的Next()和PRAV()的运行时间