矢量和地图抛出异常

时间:2018-12-10 09:19:55

标签: c++ c++11

我的任务是输出所有十位数的数字,其中数字不重复。我首先使用的是这样的东西:

#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#include <functional>

using namespace std;

void Task5() {
    auto initialization = [](map<int, bool> *m, int count) {
        for (int i = 0; i < 10; ++i)
            m[i] = true;
    };
 /*For cut duplicate number in map*/
    auto cutting = [](map<int, bool> *m, int count, int value) {
        for (int i = 9; i > count; --i)
            m[count][i][value] = false;
    };
 /*For create copy map*/
    auto mould = [](map<int,bool> *m, map<int, bool> *m_copy, int count) -> map<int, bool>* {
        if (m_copy == nullptr) {
            map<int, bool> *m_copy = new map<int, bool>[10 - count];
            for (int i = 9; i > count; --i)
                for (int j = 0; j < 10; ++j)
                    m_copy[i][j] = m[i][j]; /*<= here throw exepition*/
            return m_copy;
        }
        else {
            for (int i = 9; i > count; --i)
                for (int j = 0; j < 10; ++j)
                    m[i][j] = m_copy[i][j];
            return m;
        }
    };

    function<void(map<int, bool>*, int, int*)> recursive;
    recursive = [mould, cutting, &recursive](map<int, bool> *m, int count = 1, int *result = nullptr) -> void {
        if (count != 10) {
            for (int i = 0; i < 10; ++i) {
                static map<int, bool> *m_copy;
                if (i == 0)
                    m_copy = mould(m, nullptr, 1);
                else {
                    m = mould(m, m_copy, 1);
                    if (m[count][i])
                        result[count - 1] = i;
                    else
                        continue;
                }       
                cutting(m, count, i);
                recursive(m, ++count, result);
            }
            delete[] m_copy;
        }
        else {
            for (int i = 0; i < 10; ++i)
                cout << result[i];
            cout << endl;
        }
    };
     /*Create map
       int is digit(can be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
       if digit is used bool will be false*/
    map<int, bool> *m = new map<int, bool>[10];
    for (int i = 0; i > 10; ++i)
        initialization(m, i);
    m[0][0] = false; //First number cant' be 0
    int *result = new int[10];
    recursive(m, 1, result);
    delete[] m;
    delete[] result;
}

int main(){
    Task5();
    return 0;
}

但是它抛出异常std :: out_of_range。现在我的外观为map [0]的大小为1,其他map(map [1],map [2]等)的大小为0。为什么呢? 所以我转到论坛,找不到答案。所以我决定重写解决方案。并编写如下内容:

#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#include <functional>
#include <vector>

using namespace std;

auto end_task = []() {
    cout << endl << endl << endl;
};

void initialization(vector<bool> &vec) {
    vec.reserve(10);
    for (int i = 0; i < 10; ++i)
        vec[i] = true;
}

void cutting(vector<bool> *vec, int count, int value) {
    for (int i = 9; i > count; --i)
        vec[i][value] = false;
}

vector<bool> *mould(vector<bool> *vec, vector<bool> *vec_copy, int count) {
    if (vec_copy == nullptr) {
        vector<bool> *vec_copy = new vector<bool>[10 - count];
        for (int i = 9; i > count; --i)
            for (int j = 0; j < 10; ++j)
                vec_copy[i][j] = vec[i][j];
        return vec_copy;
    }
    else {
        for (int i = 9; i > count; --i)
            for (int j = 0; j < 10; ++j)
                vec[i][j] = vec_copy[i][j];
        return vec;
    }
}

void recursive(vector<bool> *vec, int count = 1, int *result = nullptr) {
    if (count != 10) {
        for (int i = 0; i < 10; ++i) {
            static vector<bool> *vec_copy;
            if (i == 0)
                vec_copy = mould(vec, nullptr, 1);
            else {
                vec = mould(vec, vec_copy, 1);
                if (vec[count][i])
                    result[count - 1] = i;
                else
                    continue;
            }
            cutting(vec, count, i);
            recursive(vec, ++count, result);
        }
        delete[] vec_copy;
    }
    else {
        for (int i = 0; i < 10; ++i)
            cout << result[i];
        cout << endl;
    }
}

void Task5() {
    vector<bool> *vec = new vector<bool>[10];
    for (int i = 0; i > 10; ++i)
        initialization(vec[i]);
    vec[0][0] = false;
    int *result = new int[10];
    recursive(vec, 1, result);
    delete[] m;
    delete[] result;
    end_task();
}


int main(){
    Task5();
    return 0;
}

(没有lambda函数,因为我开始怀疑它们了)但是这里向量的大小是1和0。而且我有错误:向量迭代器不可取消。为什么?我的错误在哪里?

2 个答案:

答案 0 :(得分:5)

解决STL问题的一种简单方法是使用std::next_permutation

std::vector<int> digits{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // Sorted

do
{
    for (auto d : digits) {
        std::cout << d;
    }
    std::cout << std::endl;
} while (std::next_permutation(digits.begin(), digits.end()));

Demo

答案 1 :(得分:3)

让我们仔细看看出现异常的行(连同一些上下文):

map<int, bool> *m_copy = new map<int, bool>[10 - count];
for (int i = 9; i > count; --i)
    for (int j = 0; j < 10; ++j)
        m_copy[i][j] = m[i][j]; /*<= here throw exepition*/

第一行创建了一个名为m_copy的全新变量(由于它隐藏了具有相同名称的lambda自变量,这更加令人困惑)并使其指向10 - count元素的“数组”。

该“数组”的最高索引将是10 - count - 1,如果9仅等于count == 0。这意味着外循环将在count > 0的任何时候以无效且越界的索引开始。