在字符数组上使用next_permutation
Using next_permutation on a array of chars
我在以空结尾的字符数组上使用next_permutation时遇到了问题。当我登录 do while 语句时,它只给了我开始的 charArray 的第一个字符,而不是给我所有可能的排列的第一个字符。 这是代码:
void generatePermutations(int no_ones, int length){
char charArray[length+1];
for(int i = 0; i < length; i++){
if(no_ones > 0){
charArray[i] = '1';
no_ones--;
}else{
charArray[i] = '0';
}
}
charArray[length] = ' ';
do {
std::cout << charArray[0] << std::endl;
} while ( std::next_permutation(charArray, (charArray + length)));
}
如果要使用规范do ... while(std::next_permutation)
循环来访问所有排列,则需要初始化数组,使其按排序顺序排列。这样做的原因是std::next_permutation
算法计算当前排列之后按字典顺序排列的排列。在您的情况下,您以反向排序顺序开始数组,因此算法将在第一次返回 false。
您可以更改代码以初始化数组,也可以保留它并事先添加对std::reverse
的调用。
相关文章:
- 对于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()的运行时间