递归泛型函数用作谓词,编译失败

发布于 2024-11-17 20:58:35 字数 877 浏览 5 评论 0 原文

我正在编写一个函数来比较两个列表的内容。元素的顺序并不重要,因此我在比较之前对它们进行排序。列表可以是普通类型list,也可以是列表的列表list。 >

这是一个完整的精简示例:

#include <list>

template <typename T>
bool lessThanInAnyOrder(T lhs, T rhs)
{
  return lhs < rhs;
}

template <typename T>
bool lessThanInAnyOrder(std::list<T> lhs, std::list<T> rhs)
{
  lhs.sort(lessThanInAnyOrder<T>);
  rhs.sort(lessThanInAnyOrder<T>);

  //Do comparisons here, but for now just:
  return false;
}

int main()
{
  std::list<int> list1;
  std::list<int> list2;
  lessThanInAnyOrder(list1, list2);
}

它在 GCC 4.3.3 中编译,但在 Visual Studio 2008 中,在我调用 lhs.sort() 时出现以下编译错误:

error C2660: 'std::list<_Ty>::sort' : function does not take 1 arguments

有什么建议吗?

I am writing a function to compare the contents of two lists. The order of the elements don't matter, so I sort them before I compare. The lists can be of normal types list<int>, but also be lists of lists list<list<int> >.

Here is a complete stripped down example:

#include <list>

template <typename T>
bool lessThanInAnyOrder(T lhs, T rhs)
{
  return lhs < rhs;
}

template <typename T>
bool lessThanInAnyOrder(std::list<T> lhs, std::list<T> rhs)
{
  lhs.sort(lessThanInAnyOrder<T>);
  rhs.sort(lessThanInAnyOrder<T>);

  //Do comparisons here, but for now just:
  return false;
}

int main()
{
  std::list<int> list1;
  std::list<int> list2;
  lessThanInAnyOrder(list1, list2);
}

This compiles in GCC 4.3.3, but in Visual Studio 2008, it gives the following compilation error where I'm calling lhs.sort():

error C2660: 'std::list<_Ty>::sort' : function does not take 1 arguments

Any suggestions?

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

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

发布评论

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

评论(4

迷荒 2024-11-24 20:58:35

首先:我想如果您想比较集合而不管它们的顺序如何,您可能会寻找 std::setset_differenceset_intersectionset_unionset_symmetry_difference 算法

针对你的问题

你正在尝试实现按策略排序;如果您不能简单地专门化 std::less<>正是为了这个目的而存在),您可以自己取消自定义策略:(codepad.org 上运行的代码)

#include <list>
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>

namespace applogic
{
    template <typename T>
    struct sort_policy
    {
        typedef std::less<T> predicate_t;
    };

    template <> struct sort_policy<std::string>
    {
        struct _Cmp { bool operator()(const std::string& a, const std::string& b) { return a.length()>b.length(); } };
        typedef _Cmp predicate_t;
    };

    template <typename C>
        void sort(C& cont)
    {
        typedef typename sort_policy<typename C::value_type>::predicate_t P;
        std::sort(cont.begin(), cont.end(), P());
    }

    template <typename T>
        void sort(std::list<T>& cont)
    {
        typedef typename sort_policy<T>::predicate_t P;
        cont.sort(P());
    }
}

template <class C>
    static void dump(const C& cont, const std::string& msg="")
{
    std::cout << msg;
    std::copy(cont.begin(), cont.end(), std::ostream_iterator<typename C::value_type>(std::cout, ", "));
    std::cout << std::endl;
}

int main()
{
    using applogic::sort;

    std::vector<int> ints;
    ints.push_back(13);
    ints.push_back(-3);
    ints.push_back(7);

    dump(ints, "before: ");
    sort(ints);
    dump(ints, "after: ");

    std::list<std::string> strings;
    strings.push_back("very very long");
    strings.push_back("tiny");
    strings.push_back("medium size");

    dump(strings, "before: ");
    sort(strings);
    dump(strings, "after: ");

    return 0;
}

First off: I suppose if you want to compare collections regardless of their ordering, you might be looking for std::set with the set_difference, set_intersection, set_union and set_symmetric_difference algorithms

To your question

You're trying to implement sort-by-policy; if you cannot simply specialize std::less<> (which exists for that exact purpose), you could knock off a custom policy yourself: (code running on codepad.org)

#include <list>
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>

namespace applogic
{
    template <typename T>
    struct sort_policy
    {
        typedef std::less<T> predicate_t;
    };

    template <> struct sort_policy<std::string>
    {
        struct _Cmp { bool operator()(const std::string& a, const std::string& b) { return a.length()>b.length(); } };
        typedef _Cmp predicate_t;
    };

    template <typename C>
        void sort(C& cont)
    {
        typedef typename sort_policy<typename C::value_type>::predicate_t P;
        std::sort(cont.begin(), cont.end(), P());
    }

    template <typename T>
        void sort(std::list<T>& cont)
    {
        typedef typename sort_policy<T>::predicate_t P;
        cont.sort(P());
    }
}

template <class C>
    static void dump(const C& cont, const std::string& msg="")
{
    std::cout << msg;
    std::copy(cont.begin(), cont.end(), std::ostream_iterator<typename C::value_type>(std::cout, ", "));
    std::cout << std::endl;
}

int main()
{
    using applogic::sort;

    std::vector<int> ints;
    ints.push_back(13);
    ints.push_back(-3);
    ints.push_back(7);

    dump(ints, "before: ");
    sort(ints);
    dump(ints, "after: ");

    std::list<std::string> strings;
    strings.push_back("very very long");
    strings.push_back("tiny");
    strings.push_back("medium size");

    dump(strings, "before: ");
    sort(strings);
    dump(strings, "after: ");

    return 0;
}
孤独陪着我 2024-11-24 20:58:35

编译失败,因为编译器无法选择重载的“lessThanInAnyOrder”函数传递给 list::sort。您必须像此处那样显式指定其类型。

template <typename T>
bool lessThanInAnyOrder(std::list<T> lhs, std::list<T> rhs)
{
  bool (*comparer)(T, T) = &lessThanInAnyOrder<T>;
  lhs.sort(comparer);
  rhs.sort(comparer);
 
  //Do comparisons here, but for now just:
  return false;
}

Compilation fails because compiler can't select overloaded 'lessThanInAnyOrder' function to pass to list::sort. You'll have to specify it's type explicitly like here.

template <typename T>
bool lessThanInAnyOrder(std::list<T> lhs, std::list<T> rhs)
{
  bool (*comparer)(T, T) = &lessThanInAnyOrder<T>;
  lhs.sort(comparer);
  rhs.sort(comparer);
 
  //Do comparisons here, but for now just:
  return false;
}
柠北森屋 2024-11-24 20:58:35

使用显式类型参数将函数包装在 std::ptr_fun 中:

lhs.sort(std::ptr_fun<T, T>(lessThanInAnyOrder<T>));

Wrap the function in std::ptr_fun with explicit type arguments:

lhs.sort(std::ptr_fun<T, T>(lessThanInAnyOrder<T>));
七度光 2024-11-24 20:58:35

我的猜测,对于 int 类型,你可以简单地这样写:

lhs.sort();
rhs.sort();

Demo.

My guess, for int type you can simply write like this:

lhs.sort();
rhs.sort();

Demo.

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