对象的顺序是否针对不同的程序运行而改变? [英] Order of objects are changed for different runs of program?

查看:114
本文介绍了对象的顺序是否针对不同的程序运行而改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 stl设置容器类来存储指向对象的指针.从stl集读取数据时,由于程序向对象的内存(地址)动态分配,因此对于程序的不同运行,对象的顺序会更改.

假设对象是A,B,C,它们的第一次运行地址是10、16、12.因此,当我将这些对象插入集合并检索它们时,我将获得输出为A C B(b''caz 10< 12< 16>).现在,如果在下一次运行中分配的地址为14、10、8,我将得到输出为C B A(8< 10< 14).
有什么办法可以使我按垂直顺序输出?

重载比较器(自定义比较器)可以解决此问题,但是在那种情况下,我必须将其作为模板参数传递,这会导致在许多地方修改代码.

Iam using stl set container class for storing pointers to objects. While reading from the stl sets, for different runs of the program, the order of objects are getting changed because of dynamic allocation of memory (address) to object.

Lets say objects are A,B,C and their addresses in first run are 10,16,12. So when I insert these objects into set and retrieve them, I will get output as A C B (b''caz 10<12<16). Now if in the next run the addresses alloted are 14, 10, 8, I would get the output as C B A (8<10<14).

Is there any way that I can get output in perticular order?

Overloading comparator (custom comparator) can solve the problem, but in that case I have to pass it as a template parameter, which leads to modifying code at many places. Is there any way of avoiding modifying my code, still able to write custome comparator?

推荐答案

您可以通过专门设置std :: less来覆盖默认比较器,如下所示:
You can override the default comparator by specializing std::less like this:
struct mytype {
   int x;
};
template <>
struct std::less<mytype*>
   : public binary_function<mytype*, mytype*, bool>
{   // functor for operator<
   bool operator()(const mytype* _Left, const mytype* _Right) const
   {    // apply operator< to operands
      return (_Left->x < _Right->x);
   }
};


我已经使用以下代码成功测试了此代码:


I''ve tested this successfully using this code:

void foo() {
   std::set<mytype*> s;
   mytype arr[3];
   arr[0].x = 3;
   arr[1].x = 1;
   arr[2].x = 2;
   s.insert(&arr[2]); // 2
   s.insert(&arr[0]); // 3
   s.insert(&arr[1]); // 1
   std::for_each(s.begin(), s.end(), [](const mytype* p) {
      std::cout << p->x << std::endl;
   });
}


输出为"1,2,3".如您所见,按地址的元素顺序和输入的顺序都不对应于该输出,而是less 替代.


The output is "1,2,3." As you can see, neither the order of elements by address, nor the order of input corresponds to that output, instead it''s the less override.


除了解决方案1之外:请阅读这篇详细而有用的文章:
马特·奥斯汀(Matt Austern),为什么不应该使用set(以及应该使用什么)
.

不,您应该,但是在更罕见的情况下...

—SA
In addition to Solution 1: please read this detailed and useful article:
Matt Austern, Why you shouldn''t use set (and what you should use instead)
.

No, you should, but in more rarer cases…

—SA


为什么要完全使用一套?您希望容器表现如何?

集合是有序的容器,因此,如果您对集合的条目发生更改(例如不同运行中的内存地址),则单个条目的顺序也会发生变化.

如果您不希望使用有序容器,请使用例如 vector [无序集 [ ^ ]或其他内容.
Why do you use a set at all? What do you expect the container to behave?

Sets are ordered containers, so of course if your entry to the set changes (like memory adresses in different runs) the order of the single entries changes as well.

If you dont want an ordered container use for example a vector[^] or an unordered_set[^] or somehing else.


这篇关于对象的顺序是否针对不同的程序运行而改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆