将向量 <int> 推入向量 <vector<int> > 时,SIGABRT - free():下一个大小无效(快速)
While pushing vector<int> into vector <vector<int> >, SIGABRT - free(): invalid next size (fast)
Hello to all, I am trying to write a function which will return a
vector of all permutations of a given vector. For eg, for input [1,2],
the output should be [[1,2], [2,1]]. For input [1,2,3], output should
be [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]. Note that
the order of the permutations is not important in the output vector.
我使用以下逻辑递归生成排列:
- 函数以向量作为输入,returns向量<向量>作为输出。
- 如果输入向量的大小为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。这些新创建的排列将添加到 returns 个排列的最终向量中。
代码如下:
#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():无效的下一个大小(快速)。
新创建的排列未插入到向量的向量中 "temp"。请帮忙。
问题在于
v_i.insert(it+k, elem);
使迭代器无效 it
但此处再次使用该迭代器
v_i.erase(it+k);
替代代码
v_i.insert(v_i.begin()+k, elem);
和
v_i.erase(v_i.begin()+k);
运行没有崩溃。并修复了已经提到的 operator<<
似乎给出了正确的结果。
Hello to all, I am trying to write a function which will return a vector of all permutations of a given vector. For eg, for input [1,2], the output should be [[1,2], [2,1]]. For input [1,2,3], output should be [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]. Note that the order of the permutations is not important in the output vector.
我使用以下逻辑递归生成排列:
- 函数以向量作为输入,returns向量<向量>作为输出。
- 如果输入向量的大小为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。这些新创建的排列将添加到 returns 个排列的最终向量中。
代码如下:
#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():无效的下一个大小(快速)。 新创建的排列未插入到向量的向量中 "temp"。请帮忙。
问题在于
v_i.insert(it+k, elem);
使迭代器无效 it
但此处再次使用该迭代器
v_i.erase(it+k);
替代代码
v_i.insert(v_i.begin()+k, elem);
和
v_i.erase(v_i.begin()+k);
运行没有崩溃。并修复了已经提到的 operator<<
似乎给出了正确的结果。