首页 文章

矢量和 Map 抛出异常

提问于
浏览 1706
4

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

#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;
}

但它抛出了exdition 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.而且我有错误:矢量迭代器不是dereferencable . 为什么?我的错误在哪里?

2 回答

  • 5

    让我们仔细看看你得到异常的那一行(以及一些上下文):

    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参数而增加了混淆)并使其指向"array"的 10 - count 元素 .

    "array"的顶部索引将是 10 - count - 1 ,如果 count == 0 ,它将仅等于 9 . 这意味着外部循环将在 count > 0 的任何时候以无效和越界索引开始 .

  • 3

    使用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

相关问题