如何定义“无所事事”?种类?

发布于 2024-08-22 20:19:44 字数 928 浏览 5 评论 0原文

我正在开发一个系统,我需要能够通过给定的谓词对向量进行排序,而我的类不应该控制该谓词。基本上,我向他们传递一个派生类,他们盲目地对其进行排序。

作为“令人愉快的怪癖”之一,排序模式之一是输入顺序。 这是我到目前为止所得到的。

struct Strategy
{
   virtual bool operator()(const Loan& lhs, const Loan& rhs) const = 0;
};

struct strategyA : public Strategy
{
   bool operator()(const Loan& lhs, const Loan& rhs) const
   {
      return true;
   }
};

struct strategyB : public Strategy
{
   bool operator()(const Loan& lhs, const Loan& rhs) const
   {
      return lhs.getID() > rhs.getID();
   }
};

struct strategyC : public Strategy
{
   bool operator()(const Loan& lhs, const Loan& rhs) const
   {
      return lhs.getFee() > rhs.getFee();
   }
};

显然,由于 StrategyA 是自反的,因此无法使用它,如果我将其设置为 false,它会将所有内容视为平等,我可以与我的数据吻别。

这是我的问题。有没有一种方法可以定义一个谓词函数来对向量进行排序,而不会改变任何东西?

我知道最简单的解决方案可能是向 Loan 类添加输入变量的顺序,或者将其与一对中的一个结合使用。或者,我可以在谓词中输入一个参数,告诉排序器是否使用它。

I'm working on a system where I need to be able to sort a vector by a given predicate, which my classes shouldn't have control over. Basically, I pass them a derived class and they blindly sort on it.

As one of the "delightful quirks", one of the sort patterns is order of entry.
Here's what I've got so far.

struct Strategy
{
   virtual bool operator()(const Loan& lhs, const Loan& rhs) const = 0;
};

struct strategyA : public Strategy
{
   bool operator()(const Loan& lhs, const Loan& rhs) const
   {
      return true;
   }
};

struct strategyB : public Strategy
{
   bool operator()(const Loan& lhs, const Loan& rhs) const
   {
      return lhs.getID() > rhs.getID();
   }
};

struct strategyC : public Strategy
{
   bool operator()(const Loan& lhs, const Loan& rhs) const
   {
      return lhs.getFee() > rhs.getFee();
   }
};

Obviously, as strategyA is reflexive, it can't be used, and if I set it to false, it'll treat everything as equal and I can kiss my data goodbye.

So here's my question. Is there a way of defining a predicate function for sorting a vector which will NOT change anything?

I'm aware that possibly the simplest solution is to add an order of entry variable to the Loan class, or partner it with one in a pair. Alternatively I could feed a parameter in with the predicate that tells the sorter whether to use it or not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

星星的轨迹 2024-08-29 20:19:44

有没有一种方法可以定义一个谓词函数来对向量进行排序,而不会改变任何东西?

这取决于算法。如果您的排序是稳定排序,则“相等”元素的顺序不会被改变(对于不稳定的排序是未定义的)。

考虑使用std::stable_sort

Is there a way of defining a predicate function for sorting a vector which will NOT change anything?

It depends on the algorithm. If your sort is a stable sort, the order of "equal" elements won't be changed (which is undefined for unstable sorts).

Consider using std::stable_sort.

生活了然无味 2024-08-29 20:19:44

就我个人而言,我认为你的策略类应该有一个“排序”方法。这样,它就可以根据需要调用 std::sort 或不调用 std::sort 。 是否以及如何成为排序策略的一部分。

Darios stable_sort 的答案非常好,如果你可以使用它的话。

可以根据向量中的项目位置进行排序,但这并不意味着项目不会移动(许多排序算法基本上会先打乱然后重新排序您的数据),因此您必须有一些可靠的方法来确定您开始时项目所在的位置。

比较可以保留当前位置到原始位置的映射,但需要做很多工作。理想情况下,逻辑需要内置到排序算法中 - 而不仅仅是比较 - 这本质上就是 stable_sort 的工作原理。

另一个问题 - 根据容器 - (例如)项目地址的顺序并不总是项目的顺序。

Personally, I think your strategy class should have a "sort" method. That way, it can either call std::sort or not, as it sees fit. Whether as well as how becomes part of the sorting strategy.

Darios stable_sort answer is very good, if you can use it.

It is possible to do sorting based on item position in a vector, but it doesn't mean items won't move (many sort algorithms will basically scramble-then-resort your data), so you have to have some reliable way of determining where the items were when you started.

It's possible for the comparison to keep a mapping of current-position to original-position, but a lot of work. Ideally the logic needs to be built into the sort algorithm - not just the comparison - and that's essentially how stable_sort works.

Another problem - depending on the container - the order of (say) item addresses isn't always the order of the items.

神爱温柔 2024-08-29 20:19:44

如果它只是您正在谈论的一个向量,也许您可​​以提供一个接口来确定是否应该排序。向量不是有序容器,因此您需要显式对它们进行排序。只是根本不要对它们进行排序。

if it is simply a vector you are talking about, perhaps you can get away with providing an interface that determines whether you should sort or not. vectors are not an ordered container, so you need to explicitly sort them. Just don't sort them at all.

阿楠 2024-08-29 20:19:44

没有排序功能可以仅根据项目的值来保持项目的顺序。如果可能的话,您需要为您的策略提供更多信息。

There is no sort function which would keep the order of items based only on items' values. You need to provide more information to your Strategy, if it's possible.

ら栖息 2024-08-29 20:19:44

另一种方法可能是将数据的语义引入容器。考虑使用 boost::multi_index 对相同数据进行不同的访问和排序:

http://www.boost.org/doc/libs/1_42_0/libs/multi_index/doc/index.html

A different approach might be to bring the semantics of your data to the container. Consider using boost::multi_index for different ways of access and ordering on the same data:

http://www.boost.org/doc/libs/1_42_0/libs/multi_index/doc/index.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文