Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用std::sort对指针向量进行排序会导致地址混乱吗?_C++_Sorting_Pointers_Vector - Fatal编程技术网

C++ 使用std::sort对指针向量进行排序会导致地址混乱吗?

C++ 使用std::sort对指针向量进行排序会导致地址混乱吗?,c++,sorting,pointers,vector,C++,Sorting,Pointers,Vector,我正在编写一个函数来对类指针向量进行排序。排序之前,向量包含以下指针: {0x784170, 0x7841a0, 0x784050, 0x783f10, 0x783f40, 0x7832a0, 0x7832d0, 0x7831a0, 0x7831d0, 0x783080, 0x7830b0, 0x782f40, 0x782f70, 0x7827c0, 0x7827f0} 排序后,它包含以下内容: {0x3141954a38, 0x3141954a38, 0x784050, 0x783f10,

我正在编写一个函数来对类指针向量进行排序。排序之前,向量包含以下指针:

{0x784170, 0x7841a0, 0x784050, 0x783f10, 0x783f40, 0x7832a0, 0x7832d0, 0x7831a0, 0x7831d0, 0x783080, 0x7830b0, 0x782f40, 0x782f70, 0x7827c0, 0x7827f0}
排序后,它包含以下内容:

{0x3141954a38, 0x3141954a38, 0x784050, 0x783f10, 0x783f40, 0x7832a0, 0x7832d0, 0x7831a0, 0x7831d0, 0x783080, 0x7830b0, 0x782f40, 0x782f70, 0x7827c0, 0x80}
以下是我的代码:

bool DNodeComparator(DNode* x, DNode* y)
{
return x->getDFN() < y->getDFN();
}

void sortFunction(){
    vector<DNode*> operations = ApplicationGraph.getOperations();
    std::sort(myvector.begin(), myvector.end(), mycomparator);
}

class DGraph
{
    private:
        vector<DNode*> Nodes;

    public:
        vector<DNode*> getOperations(){return Nodes;}
};
bool-DNodeComparator(DNode*x,DNode*y)
{
返回x->getDFN()getDFN();
}
void sortFunction(){
向量操作=ApplicationGraph.getOperations();
排序(myvector.begin(),myvector.end(),mycomparator);
}
类DGraph
{
私人:
向量节点;
公众:
向量getOperations(){return Nodes;}
};
代码在包含DNode*对象向量的依赖关系图上运行。我返回这个指针向量,并根据它的DFN字段对每个节点进行排序。DNode类包含Operation*对象、一些布尔标志和一个int值,用于计算依赖关系图的深度优先编号。第一行中显示的地址是在调用
std::sort
之前的gdb
print operations
调用的结果。第二行中的地址是紧跟在
std::sort
之后的
print operations
调用


我尝试了更多的调试步骤,但仍然无法协调此错误。返回DNode*的向量将创建此向量的所需副本,该副本存储在单独的地址中,因此修改此新向量(例如通过添加更多DNode)不会修改graph类中的实际向量。这不会导致任何问题,因为向量仍然包含每个DNode*

这里是一个基于比较器的工作示例

#include <algorithm>
#include <iostream>
#include <vector>

struct Obj {
  int id;
  int getID() {
    return id; }
  Obj(int i) : id(i) {} };

bool mycomparator(Obj* a, Obj* b) {
  return a->getID() < b->getID(); }

int main(int, char* []) {
  std::vector<Obj*> myvector;

  for (int i = 0; i < 4; ++i) {
    myvector.push_back(new Obj(i)); }

  std::random_shuffle(myvector.begin(), myvector.end());

  for (std::vector<Obj*>::iterator i = myvector.begin();
       i < myvector.end(); ++i) {
    std::cout << (*i) << " " << (*i)->getID() << " "; }

  std::cout << std::endl;
  std::sort(myvector.begin(), myvector.end(), mycomparator);

  for (std::vector<Obj*>::iterator i = myvector.begin();
       i < myvector.end(); ++i) {
    std::cout << (*i) << " " << (*i)->getID() << " "; }

  std::cout << std::endl;
  return 0; }

您的代码与此有何不同?

粘贴精确不替换
{some pointers}
;我认为您的代码中存在问题,但不是您在这里展示的代码。我怀疑一些
{some pointers}
实际上指向已被破坏的(本地)对象,因此它们的存储被重用。或者使用多线程,但是sortFunction不受某些同步对象的保护。
std::sort
无法更改范围内的值,它只能对它们重新排序,因此问题在于您没有向我们展示的内容。
0x80010308 2 0x80010318 1 0x80010328 3 0x800102f8 0
0x800102f8 0 0x80010318 1 0x80010308 2 0x80010328 3