vector<int*>.push_back() 正在覆盖 front() 指向的值

vector<int*>.push_back() is overwriting values pointed to by front()

本文关键字:覆盖 front back int push vector      更新时间:2023-10-16

代码在 pastebin 上: http://pastebin.com/UknqKvMq

因此,我正在排列 0,1,2,3,4,5,6,7,8,9(即形成 10 位数字)的所有可能排列。我通过调用将每个排列存储在堆上

new int[10]

返回的指针我推到

static vector<int*>

现在,当我将指针推到向量上时,第一项指向的数据正在被修改(也许是向量指向的所有其他项)。我在代码中有许多显示这一点的打印语句,使用 sleep() 调用来减慢进程。为什么会这样?

调用 Permute ,这会将指针inputCopy->arrangement推送到静态向量中。然后立即删除它。这意味着您的向量包含悬空指针。

我不知道

你想在这里做什么,但你可以用几行代码写出来:

使用std::next_permutation

std::vector<int> v{0,1,2,3,4,5,6,7,8,9};
// std::sort(v.begin(), v.end());
do {
    for(const auto&i:v)
      std::cout<<i;
    std::cout<<std::endl;
} while(std::next_permutation(v.begin(), v.end()));