简体   繁体   English

从排序比较器 function 内部访问该向量时,在向量中找到 Null 值

[英]Null values found inside a vector while accessing that vector from inside a sort comparator function

I had a vector of some shared pointers.我有一些共享指针的向量。 I was trying to sort that vector according to some criteria using sort comparator function.我试图使用排序比较器 function 根据某些标准对该向量进行排序。 The problem is that if we try to access the values of the vector from inside the comparator function;问题在于,如果我们尝试从比较器 function 内部访问向量的值; at certain places I was getting null values.在某些地方,我得到了 null 值。 This is happening only in the case of shared pointers.这只发生在共享指针的情况下。 For normal pointers, I didn't find such problem.对于普通指针,我没有发现这样的问题。 Why are we getting null values inside the vector in such scenario?在这种情况下,为什么我们会在向量内获取 null 值?

My code-我的代码-

class Tmp {
private :
    int a,b;
public :
    int getA() {
        return a;
    }
    int getB() {
        return b;
    }
    Tmp(int x, int y) : a(x), b(y) {}
};

int main() {
    std::shared_ptr<Tmp> t1 = std::make_shared<Tmp>(11,19);
    std::shared_ptr<Tmp> t2 = std::make_shared<Tmp>(2,3);
    std::shared_ptr<Tmp> t3 = std::make_shared<Tmp>(5,6);
    std::shared_ptr<Tmp> t4 = std::make_shared<Tmp>(3,5);

    vector<std::shared_ptr<Tmp>> v;
    v.push_back(t1);
    v.push_back(t2);
    v.push_back(t3);
    v.push_back(t4);

    auto fun = [&v] (std::shared_ptr<Tmp> &l, std::shared_ptr<Tmp> &r) -> bool {
      for (auto it : v) {
        if (!it) {
            cout<<"null value"<<endl;
        } else {
            cout<<it->getA()<<", "<<it->getB()<<endl;
        }  
      }
      cout<<"-------"<<endl;
      return (l->getA() < r->getA());
  };
  sort(v.begin(), v.end(), fun);
  return 0;
}

Output Output

11, 19
2, 3
5, 6
3, 5
-------
2, 3
11, 19
5, 6
3, 5
-------
2, 3
11, 19
null value
3, 5
-------
2, 3
null value
11, 19
3, 5
-------
2, 3
5, 6
11, 19
3, 5
-------
2, 3
5, 6
11, 19
null value
-------
2, 3
5, 6
null value
11, 19
-------
2, 3
null value
5, 6
11, 19
-------

You don't explain what you expected.你没有解释你的期望。 It would be easier to answer your question usefully if it was clear why you thought this was sensible.如果清楚您为什么认为这是明智的,那么有用地回答您的问题会更容易。

Imagine you're sorting some stacks of papers.想象一下,您正在整理一些文件。 You're going to be moving those papers around.你将四处移动这些文件。 Sometimes, you'll take one out of the stacks to hold aside until you decide where it will go.有时,您会从堆栈中取出一个放在一边,直到您决定将 go 放在哪里。 Anyone looking at the contents of the stacks while you are sorting them should expect to see missing papers, empty spaces, stuff where it doesn't belong, and so on because you are in the middle of sorting them .任何人在您对它们进行排序时查看堆栈的内容应该会看到丢失的文件、空白空间、不属于它的东西等等,因为您正在对它们进行排序

The null values you see are slots that the sorting algorithm has not yet decided what object to fill with.您看到的 null 值是排序算法尚未决定要填充什么 object 的槽。 When the slots contains pure values that have no special semantics, the slot will hold a garbage value while it's being worked on.当插槽包含没有特殊语义的纯值时,插槽将在处理时保存一个垃圾值。 When the slots contains more complex objects, the slots will tend to hold a moved-from object, which for std::shared_ptr is one that tests false.当插槽包含更复杂的对象时,插槽将倾向于保存从 object 移出的对象,对于std::shared_ptr来说,它是一个测试为假的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM