简体   繁体   English

std::set 与自定义比较器

[英]std::set with custom comparator

I am trying to order wordItems in descending wordCount order in a set.我试图在一个集合中按 wordCount 降序对 wordItems 进行排序。

I am in data structures, and our professor gives us challenges to use STL containers on previous assignments.我是数据结构的,我们的教授给我们挑战,让我们在以前的作业中使用 STL 容器。 The reason an std::set is being used here is because my Prof. wants to show us how to use an STL BST in practice.这里使用 std::set 的原因是我的教授想向我们展示如何在实践中使用 STL BST。

I cannot seem to figure out the std::set and all of its functionality...我似乎无法弄清楚std::set及其所有功能......

Here is what I believe to be the relevant code pertaining to my wordItem struct:这是我认为与我的 wordItem 结构相关的相关代码:

// header file (HashTable.hpp)
struct wordItem
{
    std::string word;
    int count;
    wordItem* next;
};

struct comp 
{
    // I have also tried bool operator<()(wordItem const &lhs, wordItem const &rhs)     
    bool operator()(wordItem const &lhs, wordItem const &rhs) const {
        if (lhs.count == rhs.count) {
            return lhs.word > rhs.word;
        }
        return lhs.count > rhs.count;
    }
};

And then here is my function that would actually use this set:然后这是我实际使用这个集合的函数:

// implementation file (HashTable.cpp)
#include "HashTable.hpp"
void HashTable::printTopN(int n) {
    set<wordItem,comp> s;

    for (int i = 0; i < hashTableSize; i++) {
        if (hashTable[i] != nullptr) {
            wordItem *temp = hashTable[i];
            s.insert(*temp);
            while (temp->next != nullptr) {
                temp = temp->next;
                s.insert(*temp);
            }
        }
    }
}

However, I am getting an error message that says I am using undeclared identifiers.但是,我收到一条错误消息,指出我正在使用未声明的标识符。 Both comp and s are causing this... This is the exact error message: comps都导致了这个......这是确切的错误消息:

HashTable2.cpp:129:16: error: use of undeclared identifier 'comp' 
set<wordItem,comp> s;
             ^
HashTable2.cpp:134:7: error: use of undeclared identifier 's'
s.insert(*temp);
^
HashTable2.cpp:137:9: error: use of undeclared identifier 's'
s.insert(*temp); 
^

The set is supposed to contain wordItem 's with the largest wordCount first, and if two words share the same count the tiebreaker should be lexicographical order.集合应该包含wordItem具有最大的wordCount第一,如果两个单词共享相同的数抢七应该是字典序。

I feel like the issue is probably coming from the fact that I am probably not comparing these wordItem 's correctly我觉得这个问题可能来自于我可能没有正确比较这些wordItem的事实

Lastly, I apologize because I assume its very messy to mix my own custom data structures with the STL structures, but any help would be greatly appreciated最后,我深表歉意,因为我认为将我自己的自定义数据结构与 STL 结构混合起来非常麻烦,但任何帮助将不胜感激

When you use a custom comparator std::map uses it as当您使用自定义比较器时std::map将其用作

comp_object(a, b)

In

struct comp {
    bool operator<(wordItem const &lhs, wordItem const &rhs) const {
        if (lhs.count == rhs.count) {
            return lhs.word > rhs.word;
        }
        return lhs.count > rhs.count;
    }
};

you define an operator < , not an operator () , so it wont work.你定义了一个operator < ,而不是一个operator () ,所以它不会工作。 Change it to将其更改为

struct comp {
    bool operator()(wordItem const &lhs, wordItem const &rhs) const {
        if (lhs.count == rhs.count) {
            return lhs.word > rhs.word;
        }
        return lhs.count > rhs.count;
    }
};

And it will work.它会起作用。 This is called a functor and you can read more about there here: What are C++ functors and their uses?这称为函子,您可以在此处阅读更多相关信息: 什么是 C++ 函子及其用途?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM