选择满足条件的特定对象对象、条件

2023-09-11 06:46:46 作者:ミ旧人旧事旧感情。

比方说,我有一个看起来非常大致是这样的对象:

Let's say I have objects which look very roughly like this:

class object
{
  public:
    // ctors etc.

    bool has_property_X() const { ... }
    std::size_t size() const { ... }

  private:
    // a little something here, but not really much
};

我存储的载体内,这些目的和向量是相当小的(例如,至多约1000的元素)。然后,性能关键算法里面,我想选择一个既有参数X的和的具有最小尺寸(如果有多个这样的对象,选择其中的任何)的对象。我需要做这种选择多次,并且属性的X既保持和尺寸可以的选择之间变化中,以便这些对象的方式动态这里。这两个查询(属性,大小)可以在固定的时间进行。

I'm storing these objects inside a vector and the vector is rather small (say, at most around 1000 elements). Then, inside a performance critical algorithm, I would like to choose the object that both has the property X and has the least size (in case there are multiple such objects, choose any of them). I need to do this "choosing" multiple times, and both the holding of the property X and the size may vary in between the choices, so that the objects are in a way dynamic here. Both queries (property, size) can be made in constant time.

我如何能最好地实现这一目标?性能成型为在这里是很重要的。我目前的想法:

How would I best achieve this? Performance is profiled to be important here. My ideas at the moment:

1)使用std :: min_element用合适的predicate。这恐怕还需要提高:: filter_iterator或类似的东西来遍历对象满足参数X?

1) Use std::min_element with a suitable predicate. This would probably also need boost::filter_iterator or something similar to iterate over objects satisfying property X?

2)使用的一些数据结构,例如一个优先级队列。我将指针或reference_wrappers存储的对象等等。这ATLEAST对我来说,感觉迟钝,可能是因为物体的动态性质也不可行。

2) Use some data structure, such as a priority queue. I would store pointers or reference_wrappers to the objects and so forth. This atleast to me, feels slow and probably it's not even feasible because of the dynamic nature of the objects.

在这些想法的任何其他建议或意见?如果我只是继续前进,并尝试所有这些计划或两者配置文件?

Any other suggestions or comments on these thoughts? Should I just go ahead and try any or both of these schemes and profile?

推荐答案

您最后的选择总是好的。我们如何code将运行的直觉往往是错误的。因此,在可能的情况分析总是在关键的code有用。

Your last choice is always a good one. Our intuitions about how code will run are often wrong. So where possible profiling is always useful on critical code.