# Vector and map throw exception

• A+
Category：Languages

my task is output all ten-digit numbers, where the numbers don't repeat. And I first I am using something like this:

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

But it throw exepition std::out_of_range. Now I had look and map[0] have size 1, other map(map[1], map[2] and other) have size 0. Why it is? So I look to forum and can't find answer. So I I decided to rewrite solution. And write something like this:

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

(Whithout lambda function because I began to suspect them)But here vector size is 1 and 0. And I have error: Vector iterator not dereferencable. Why? Where my mistake?

An easy way to solve your issue with STL is to use `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