将向量 <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.

代码如下:

#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<< 似乎给出了正确的结果。