std::sort 没有函子

发布于 2024-07-22 21:39:03 字数 584 浏览 3 评论 0原文

我有一个关于 std::sort 算法的问题。 这是我的测试代码:

struct MyTest
{
    int m_first;
    int m_second;

    MyTest(int first = 0, int second = 0) : m_first(first), m_second(second)
    {
    }
};


int main(int argc,char *argv[])
{
    std::vector<MyTest> myVec;
    for(int i = 0; i < 10; ++i)
    {
        myVec.push_back(MyTest(i, i + 1));
    }


    //Sort the vector in descending order on m_first without using stand alone function or functors


    return 0;

}  

是否可以在不使用任何独立函数或函子的情况下对变量 m_first 上的向量进行排序? 另外,请注意,我没有使用 boost。

I have a question regarding the std::sort algorithm. Here is my test code:

struct MyTest
{
    int m_first;
    int m_second;

    MyTest(int first = 0, int second = 0) : m_first(first), m_second(second)
    {
    }
};


int main(int argc,char *argv[])
{
    std::vector<MyTest> myVec;
    for(int i = 0; i < 10; ++i)
    {
        myVec.push_back(MyTest(i, i + 1));
    }


    //Sort the vector in descending order on m_first without using stand alone function or functors


    return 0;

}  

Is it possible to sort the vector on the variable m_first without using any stand alone functions or functors? Also, please note that I am not using boost.

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

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

发布评论

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

评论(6

不一样的天空 2024-07-29 21:39:06

您应该在 MyTest 中定义一个 operator<,它应该如下所示:

bool operator<(const MyTest &other) const {
  return m_first < other.m_first;
};

You should define an operator< in MyTest and it should look like this:

bool operator<(const MyTest &other) const {
  return m_first < other.m_first;
};
不必你懂 2024-07-29 21:39:06

http://ideone.com/3QLtP

这没有定义运算符 <,但定义了函子。

然而,穿越时空或编​​译过程是很有趣的。

http://ideone.com/3QLtP

This defines no operator <, but does define functor.

However, it's fun to travel through time or compilation process.

一身仙ぐ女味 2024-07-29 21:39:05

可以使用成员函数来完成此操作,但独立函数是最佳选择。

bool operator <(const MyTest &lhs, const MyTest &rhs)
{
    return lhs.m_first<rhs.m_first;
}

为什么 ..

Scott Meyers:非成员函数如何改进封装

如果你正在编写一个函数,可以
作为会员或
作为非朋友非会员,您应该
更喜欢以非会员身份实施
功能。 这个决定增加了
类封装。 你想的时候
封装,你应该想到
非成员函数。

惊讶吗? 继续阅读

It is possible to do it with a member function but the stand alone function is the way to go.

bool operator <(const MyTest &lhs, const MyTest &rhs)
{
    return lhs.m_first<rhs.m_first;
}

Why ..

Scott Meyers: How Non-Member Functions Improve Encapsulation

If you're writing a function that can
be implemented as either a member or
as a non-friend non-member, you should
prefer to implement it as a non-member
function. That decision increases
class encapsulation. When you think
encapsulation, you should think
non-member functions.

Surprised? Read on

不必了 2024-07-29 21:39:05

写一个运算符< 为你的结构。 这是 sort 使用的默认函数,也是让它在自定义数据结构上运行的最简单方法。

Write an operator< for your struct. This is the default function used by sort and the easiest way to allow it to function on your custom data structures.

小苏打饼 2024-07-29 21:39:05

定义运算符<

struct MyTest
{
...
    bool operator<(const MyTest& a_test) const {
        return m_first < a_test.m_first;
    }
};

Define the operator<

struct MyTest
{
...
    bool operator<(const MyTest& a_test) const {
        return m_first < a_test.m_first;
    }
};
凡间太子 2024-07-29 21:39:05

是的,只要要排序的范围内的值类型有一个运算符<定义了“严格弱排序”,也就是说可以用来比较两个MyTest 实例正确。 你可能会这样做:

class MyTest
{
  ...
  bool operator <(const MyTest &rhs) const
  {
    return m_first<rhs.m_first;
  }
};

Yes, so long as the value type in the range to be sorted has an operator < that defines a "strict weak ordering", that is to say, it can be used to compare two MyTest instances correctly. You might do something like:

class MyTest
{
  ...
  bool operator <(const MyTest &rhs) const
  {
    return m_first<rhs.m_first;
  }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文