将矢量<int>推入矢量<矢量<int>>时,SIGABRT - free():下一个大小无效(快速)
While pushing vector<int> into vector <vector<int> >, SIGABRT - free(): invalid next size (fast)
大家好,我正在尝试编写一个函数,该功能将返回 给定向量的所有排列的向量。例如,对于输入[1,2], 输出应为[[1,2],[2,1]。对于输入[1,2,3],输出应 be [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]。注意 排列的顺序在输出向量中并不重要。
-
我已经使用以下逻辑来递归生成排列:
- 该函数将向量作为输入,返回向量&lt;向量>作为输出。
- 如果输入向量的大小为1,即输入向量= [int],则输出为[[int]]
- 其他:
- 从输入向量V中删除ELEM =第一个元素,V'是新的向量,是第一个元素删除
- 通过递归函数调用找到V'的排列。
- 对于排列(V'(中的每个排列向量,通过将ELEM插入所有可能的位置并将此新创建的置换矢量插入到要返回的最终输出中来创建一个新的排列向量。
-
下面给出的是一个测试案例:
- 输入vector = [1,2]
- 预期输出= [[1,2],[2,1]
- 测试案例:
- 1被视为Elem,[2]变为V'
- v'I.2的排列。[2]是[[2]],因为它是递归的基本案例
- 那么,对于排列(v'(中的每个排列,即[[2]]中的[2],我们在所有位置上添加Elem 1,这些位置将是[1,2]和[2,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((中的错误((:无效的下一个大小(fast(。新创建的置换并未插入向量"临时"的向量中。请帮忙。
问题是
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<<
的修复,似乎给出了正确的结果。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中