使用自定义比较器进行 std::list::sort 时出错(预计在 ' 之前的主表达式)' 令牌)

发布于 2024-07-26 16:56:46 字数 1516 浏览 3 评论 0原文

标题是主要问题。 确切的场景(我是“使用命名空间 std;”):

void SubstringMiner::sortByOccurrence(list<Substring *> & substring_list) {
   list::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator);
}

这是比较器定义:

class Substring {
    // ...
    class OccurrenceComparator {
        public:
            bool operator() (Substring * a, Substring *b);
    }
};

比较器的实现直观且简单。 我还在 std::set 中使用了一个非常相似的比较器,它工作得很好。 当我添加 sortByOccurrence() 函数时,它在标题中给出了错误。

我应该怎么办?

编辑:我现在尝试传递 Substring::OccurrenceComparator() 作为比较器,并收到以下错误:

g++ -Wall -g -c substring_miner.cpp -o obj/subtring_miner.o
substring_miner.cpp: In function ‘void SubstringMiner::sortByOccurrence(std::list<Substring*, std::allocator<Substring*> >&)’:
substring_miner.cpp:113: error: no matching function for call to ‘std::list<Substring*, std::allocator<Substring*> >::sort(std::_List_iterator<Substring*>, std::_List_iterator<Substring*>, Substring::OccurrenceComparator)’
/usr/include/c++/4.3/bits/list.tcc:303: note: candidates are: void std::list<_Tp, _Alloc>::sort() [with _Tp = Substring*, _Alloc = std::allocator<Substring*>]
make: *** [substring_miner] Error 1

我的代码行现在:

list<Substring *>::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator());

我无法删除模板,或者它给了我错误提示模板参数错误。

The title is the main question. The exact scenario (I am 'using namespace std;'):

void SubstringMiner::sortByOccurrence(list<Substring *> & substring_list) {
   list::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator);
}

This is the comparator definition:

class Substring {
    // ...
    class OccurrenceComparator {
        public:
            bool operator() (Substring * a, Substring *b);
    }
};

Implementation of the comparator is intuitive and trivial. I am also using a very similar comparator in a std::set and it works fine. When I add the sortByOccurrence() funcion it gives me the error in the title.

What should I do?

EDIT: I'm now trying to pass Substring::OccurrenceComparator() as the comparator, and am getting the following error:

g++ -Wall -g -c substring_miner.cpp -o obj/subtring_miner.o
substring_miner.cpp: In function ‘void SubstringMiner::sortByOccurrence(std::list<Substring*, std::allocator<Substring*> >&)’:
substring_miner.cpp:113: error: no matching function for call to ‘std::list<Substring*, std::allocator<Substring*> >::sort(std::_List_iterator<Substring*>, std::_List_iterator<Substring*>, Substring::OccurrenceComparator)’
/usr/include/c++/4.3/bits/list.tcc:303: note: candidates are: void std::list<_Tp, _Alloc>::sort() [with _Tp = Substring*, _Alloc = std::allocator<Substring*>]
make: *** [substring_miner] Error 1

My code line is now:

list<Substring *>::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator());

I can't remove the template or it gives me an error saying that template parameters were wrong.

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

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

发布评论

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

评论(3

帅哥哥的热头脑 2024-08-02 16:56:46

list 成员 sort 是一个非静态函数,因此必须在列表实例上调用。

substring_list.sort( Substring::OccurrenceComparator() );

编辑:您不能使用自由函数std::sort,因为它需要随机访问迭代器,而list迭代器则不需要。

list member sort is a non-static function so must be called on a list instance.

substring_list.sort( Substring::OccurrenceComparator() );

Edit: You can't use the free function std::sort as it requires random access iterators which list iterators are not.

笑着哭最痛 2024-08-02 16:56:46

您将一个作为参数传递给函数。 您不能这样做 - 您必须创建该类的实例,并传递该实例:

substring_list.sort(Substring::OccurrenceComparator());

请注意上面 OccurenceComparator 之后的额外括号,它们使用默认构造函数创建该类的临时对象。

另一个错误是您将 list::sort 作为类 std::list 上的静态函数调用。 它不是静态的,因此您需要将其作为 substring_list 上的成员函数进行调用。

You're passing a class as an argument to a function. You cannot do that - you have to create an instance of the class, and pass that:

substring_list.sort(Substring::OccurrenceComparator());

Note the extra parentheses after OccurenceComparator above that create a temporary object of the class using default constructor.

Another mistake is that you're calling list::sort as a static function on class std::list. It's not static, so you need to call it as a member function on substring_list.

孤独岁月 2024-08-02 16:56:46

原来的问题已经被 Pavel Minaev 上面解决了。
但有一些额外的注意事项。

operator() 可能应该是 const (以及参数)。
对于像这样的简单类,将它们构造成结构会更容易。

struct OccurrenceComparator
{
    bool operator() (Substring const* a, Substring const* b)  const;
};

请注意,比较必须提供严格的弱排序:

模板
无效排序(BinaryPredicate comp);

Comp 必须是一个比较函数,它会产生严格的弱排序(如 T 类型对象的 LessThan Comparable 要求中所定义)。该函数根据 Comp 对列表 *this 进行排序。排序是稳定的,即相对保留等效元素的顺序。所有迭代器仍然有效并继续指向相同的元素。 [6] 比较次数大约为 N log N,其中 N 是列表的大小。

The original problem was already solved by Pavel Minaev above.
But some extra notes.

The operator() should probably be const (as well as the parameters).
For simple classes like this it is easier to just make them structures.

struct OccurrenceComparator
{
    bool operator() (Substring const* a, Substring const* b)  const;
};

Note the comparison must provide a strict weak ordering:

template<class BinaryPredicate>
void sort(BinaryPredicate comp);

Comp must be a comparison function that induces a strict weak ordering (as defined in the LessThan Comparable requirements on objects of type T. This function sorts the list *this according to Comp. The sort is stable, that is, the relative order of equivalent elements is preserved. All iterators remain valid and continue to point to the same elements. [6] The number of comparisons is approximately N log N, where N is the list's size.

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