I use next_permutation function to have a vector list permutation, but the stack is overflow when running the program, due to size of the vector
#include <vector>
#include <algorithm>
vector<int> uuid_list_index;
vector<vector<int>> permutation_uuid_lists;
for(size_t i=0; i<12; i )
{
uuid_list_index.push_back(i);
}
do permutation_uuid_lists.push_back(uuid_list_index);
while(next_permutation(uuid_list_index.begin(), uuid_list_index.end()));
when run the program, the binary overflow crash, How implement a permutation function for list {0,1,2,3,4,5,6,7,8,9,10,11,12}?
CodePudding user response:
This is not too surprising at all. uuid_list_index
has 12 different entries.
The number of permutations of a sequence of length N is N!; and
12! = 479001600.
permutation_uuid_lists
contains more than 479 million std::vector<int>
s; since each vector has at least a 12 Byte header (armv8) and contains a pointer to at least 12× 4-byte integer memory elements, plus implies a 16-byte entry in a memory allocation table: you're trying to use around 30 GB of RAM. That's more than your phone has.