c++ - vector 和 map 抛出异常

标签 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;
}

但它会抛出 exepition 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 函数,因为我开始怀疑它们)但是这里 vector 大小是 1 和 0。而且我有错误: vector 迭代器不可取消引用。为什么?我哪里错了?

最佳答案

解决 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

关于c++ - vector 和 map 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53702610/

相关文章:

.net - 使用此库 shlwapi.dll 中的 StrCmpLogicalW 函数在 C++ 中自然排序

c++ - C++中的坐标系

c++ - 为什么在此示例中从 unique_ptr<derived> 自动向上转换到 unique_ptr<base> 失败?

c++ - 为什么要调用基础构造函数?

c++ - 如何检查 Linux 操作系统中使用的 C++ 版本?

c++ - Variadic 模板函数名称查找无法找到特化

c++ - 如何使用 NASM 创建 .dll 文件?

C++程序崩溃

c++ - 对每个簇大小具有上限要求的聚类算法

c++ - 在C++中创建一个没有名字的对象