【发布时间】:2018-02-16 20:53:11
【问题描述】:
大家好,我正在尝试编写一个函数,它会返回一个 给定向量的所有排列的向量。例如,对于输入 [1,2], 输出应该是 [[1,2], [2,1]]。对于输入 [1,2,3],输出应 为 [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]] .注意 排列的顺序在输出向量中并不重要。
-
我使用以下逻辑递归生成排列:
- 函数将向量作为输入,返回向量
作为输出。 - 如果输入向量的大小为1,即输入向量=[int],则输出为[[int]]
- 其他:
- 从输入向量 V 中删除 ELEM = 第一个元素,V' 是删除第一个元素的新向量
- 使用递归函数调用查找 V' 的排列。
- 对于 permutations(V') 中的每个置换向量,通过在所有可能的位置插入 ELEM 创建一个新的置换向量,并将这个新创建的置换向量附加到要返回的最终输出。
- 函数将向量作为输入,返回向量
-
下面是一个测试用例:
- 输入向量 = [1,2]
- 预期输出 = [[1,2], [2,1]]
- 测试用例:
- 1被当作ELEM,[2]变成V'
- V' i.2 的排列。 [2] 是 [[2]],因为它是递归的基本情况
- 然后,对于 permutations(V') 中的每个排列,即对于 [[2]] 中的 [2],我们在 [1,2] 和 [2,1] 的所有位置添加 ELEM 1。这些新创建的排列将被添加到返回排列的最终向量中。
以下是代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
ostream & operator <<(ostream & out, vector<int> printVector) {
out << "[ ";
for(int i=0; i<printVector.size();i++) {
out << printVector[i] << " ";
}
out << "]";
return out;
}
ostream & operator <<(ostream & out, vector< vector<int> >
printVectorOfVectors) {
out << "[" << endl;
for(int i=0; i<printVectorOfVectors.size(); i++) {
out << printVectorOfVectors[i] << endl;
}
out << "]";
return out;
}
vector< vector<int> > generatePermutations(vector<int> baseVector) {
if(baseVector.size() == 1) {
vector< vector<int> > temp;
temp.push_back(baseVector);
// DEBUG
cout << "ROOT CASE , RET PERM : " << endl << temp << endl;
// \DEBUG
return temp;
}
else {
vector< vector<int> > temp;
int elem = baseVector[0];
baseVector.erase(baseVector.begin());
// DEBUG
cout << "ELEM : " << endl << elem << endl;
cout << "BASE VECTOR : " << endl << baseVector << endl;
// \DEBUG
vector< vector<int> > processPermutationsVector = generatePermutations(baseVector);
// DEBUG
cout << "PROCESS PERMS : " << endl << processPermutationsVector << endl;
// \DEBUG
for(int i=0; i<processPermutationsVector.size(); i++) {
vector<int> v_i = processPermutationsVector[i];
// DEBUG
cout << "V_i : " << endl << v_i << endl;
// \DEBUG
for(int k=0; k<v_i.size()+1; k++) {
vector<int>::iterator it = v_i.begin();
cout << "k : " << k << endl;
cout << "ORG PERM : " << endl << v_i << endl;
v_i.insert(it+k, elem);
cout << "PUSH PERM : " << endl << v_i << endl;
temp.push_back(v_i);
cout << "RET PERMS : " << endl << temp << endl;
v_i.erase(it+k);
cout << "CLEANED PERM : " << endl << v_i << endl;
}
}
return temp;
}
}
int main() {
vector<int> testVector{1,2};
cout << "TEST VECTOR : " << endl << testVector << endl;
vector< vector<int> > testPermutationsVector = generatePermutations(testVector);
cout << "TEST PERMUTATIONS VECTOR" << endl << testPermutationsVector << endl;
return 0;
}
代码给出以下输出:
TEST VECTOR :
[ 1 2 ]
ELEM :
1
BASE VECTOR :
[ 2 ]
ROOT CASE , RET PERM :
[
[ 2 ]
]
PROCESS PERMS :
[
[ 2 ]
]
V_i :
[ 2 ]
k : 0
ORG PERM :
[ 2 ]
PUSH PERM :
[ 1 2 ]
RET PERMS :
[
[ 1 2 ]
]
CLEANED PERM :
[ 2 ]
k : 1
ORG PERM :
[ 2 ]
PUSH PERM :
[ 2 1 ]
RET PERMS :
[
[ ]
[ 2 1 ]
]
CLEANED PERM :
[ 2 ]
double free or corruption (out)
在 codechef online c++ ide 上执行的代码给出了 SIGABRT 的运行时错误 - `./prog' 中的错误:free(): invalid next size (fast)。 新创建的排列未插入向量“temp”的向量中。请帮忙。
【问题讨论】:
-
你需要调试你的代码,我们不会为你做这个。
-
由于我在做一个在线ide,所以我使用cout语句来调试。 “RET PERMS”语句打印出最终向量的内容,即使在推送排列之后也是空的。
-
您的函数
generatePermutations无法正确处理空向量。它会导致未定义的行为。你的printVectorOfVectors[0]应该是printVectorOfVectors[i]。 -
您只能使用在线 IDE 到此为止(而且这不是很远)。我建议您下载并安装合适的 IDE。
-
@ccpak 打印诊断通常是一个好的开始。另一个有用的工具:onlinegdb.com 这使您可以检查一个翻译单元程序的行为,如果需要,可以逐行执行。更复杂的程序需要离线开发环境。