将矢量<int>推入矢量<矢量<int>>时,SIGABRT - free():下一个大小无效(快速)

While pushing vector<int> into vector <vector<int> >, SIGABRT - free(): invalid next size (fast)

本文关键字:lt gt int 下一个 无效 快速 矢量 SIGABRT free      更新时间:2023-10-16

大家好,我正在尝试编写一个函数,该功能将返回 给定向量的所有排列的向量。例如,对于输入[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]]
    • 其他:
      1. 从输入向量V中删除ELEM =第一个元素,V'是新的向量,是第一个元素删除
      2. 通过递归函数调用找到V'的排列。
      3. 对于排列(V'(中的每个排列向量,通过将ELEM插入所有可能的位置并将此新创建的置换矢量插入到要返回的最终输出中来创建一个新的排列向量。
  • 下面给出的是一个测试案例:

    • 输入vector = [1,2]
    • 预期输出= [[1,2],[2,1]
    • 测试案例:
      1. 1被视为Elem,[2]变为V'
      2. v'I.2的排列。[2]是[[2]],因为它是递归的基本案例
      3. 那么,对于排列(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<<的修复,似乎给出了正确的结果。