vector<int*>.push_back() 正在覆盖 front() 指向的值
vector<int*>.push_back() is overwriting values pointed to by front()
代码在 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()));
相关文章:
- 使用ios:ate写入到流会覆盖现有文件
- 我可以重新分配/覆盖std::字符串吗
- muQueue.front() 给出了 const 实例,即使我没有将其标记为 const
- 叮叮当当在修复时插入多个"覆盖"说明符
- 谷歌模拟和覆盖关键字
- TMap::Emplace() 在应用现有密钥时会覆盖吗?
- 此测试()中发生了什么意外过程?为什么总是覆盖 ch[0 1 2..]?
- 使用子类覆盖基类中定义的函数
- 当覆盖存在时调用基本虚拟"binded to object"函数
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- C++指针在 for 循环中被覆盖
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 为什么我的全局 new() 覆盖被绕过了?
- 被覆盖的函数不会反映在基类中,这是正常行为吗?
- C++找出覆盖同一行的数组
- 专门化模板覆盖函数/避免对象切片
- 重写打印函数而不是覆盖基类
- C++中"覆盖功能的异常规范比基本版本更宽松"的奇怪错误
- 如何在 c++ 中覆盖相同的文本文件
- vector
.push_back() 正在覆盖 front() 指向的值