简单的 C++哈希集示例

发布于 2024-08-03 17:46:39 字数 3170 浏览 2 评论 0 原文

我是 C++ 和 STL 的新手。我遇到了以下存储自定义数据结构的哈希集的简单示例:

#include <iostream>
#include <ext/hash_set>

using namespace std;
using namespace __gnu_cxx;

struct trip {
    int trip_id;
    int delta_n;
    int delta_secs;

    trip(int trip_id, int delta_n, int delta_secs){
        this->trip_id = trip_id;
        this->delta_n = delta_n;
        this->delta_secs = delta_secs;
    }
};


struct hash_trip
{
    size_t operator()(const trip t)
    {
        hash<int> H;
        return H(t.trip_id);
    }
};

struct eq_trip
{
    bool operator()(const trip t1, const trip t2) {
        return (t1.trip_id==t2.trip_id) &&
        (t1.delta_n==t2.delta_n) &&
        (t1.delta_secs==t2.delta_secs);
    }
};

int main()
{
    hash_set<trip, hash_trip, eq_trip> trips;

    trip t  = trip(3,2,-1);
    trip t1  = trip(3,2,0);

    trips.insert(t);

}

当我尝试编译它时,我收到以下错误消息:

/usr/include/c++/4.2.1/ext/hashtable.h: In member function ‘size_t __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::_M_bkt_num_key(const _Key&, size_t) const [with _Val = trip, _Key = trip, _HashFcn = hash_trip, _ExtractKey = std::_Identity<trip>, _EqualKey = eq_trip, _Alloc = std::allocator<trip>]’:
/usr/include/c++/4.2.1/ext/hashtable.h:599:   instantiated from ‘size_t __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::_M_bkt_num(const _Val&, size_t) const [with _Val = trip, _Key = trip, _HashFcn = hash_trip, _ExtractKey = std::_Identity<trip>, _EqualKey = eq_trip, _Alloc = std::allocator<trip>]’
/usr/include/c++/4.2.1/ext/hashtable.h:1006:   instantiated from ‘void __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::resize(size_t) [with _Val = trip, _Key = trip, _HashFcn = hash_trip, _ExtractKey = std::_Identity<trip>, _EqualKey = eq_trip, _Alloc = std::allocator<trip>]’
/usr/include/c++/4.2.1/ext/hashtable.h:437:   instantiated from ‘std::pair<__gnu_cxx::_Hashtable_iterator<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>, bool> __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::insert_unique(const _Val&) [with _Val = trip, _Key = trip, _HashFcn = hash_trip, _ExtractKey = std::_Identity<trip>, _EqualKey = eq_trip, _Alloc = std::allocator<trip>]’
/usr/include/c++/4.2.1/ext/hash_set:197:   instantiated from ‘std::pair<typename __gnu_cxx::hashtable<_Value, _Value, _HashFcn, std::_Identity<_Value>, _EqualKey, _Alloc>::const_iterator, bool> __gnu_cxx::hash_set<_Value, _HashFcn, _EqualKey, _Alloc>::insert(const typename __gnu_cxx::hashtable<_Value, _Value, _HashFcn, std::_Identity<_Value>, _EqualKey, _Alloc>::value_type&) [with _Value = trip, _HashFcn = hash_trip, _EqualKey = eq_trip, _Alloc = std::allocator<trip>]’
try.cpp:45:   instantiated from here
/usr/include/c++/4.2.1/ext/hashtable.h:595: error: passing ‘const hash_trip’ as ‘this’ argument of ‘size_t hash_trip::operator()(trip)’ discards qualifiers

我做错了什么?

I am new to C++ and STL. I am stuck with the following simple example of a hash set storing custom data structures:

#include <iostream>
#include <ext/hash_set>

using namespace std;
using namespace __gnu_cxx;

struct trip {
    int trip_id;
    int delta_n;
    int delta_secs;

    trip(int trip_id, int delta_n, int delta_secs){
        this->trip_id = trip_id;
        this->delta_n = delta_n;
        this->delta_secs = delta_secs;
    }
};


struct hash_trip
{
    size_t operator()(const trip t)
    {
        hash<int> H;
        return H(t.trip_id);
    }
};

struct eq_trip
{
    bool operator()(const trip t1, const trip t2) {
        return (t1.trip_id==t2.trip_id) &&
        (t1.delta_n==t2.delta_n) &&
        (t1.delta_secs==t2.delta_secs);
    }
};

int main()
{
    hash_set<trip, hash_trip, eq_trip> trips;

    trip t  = trip(3,2,-1);
    trip t1  = trip(3,2,0);

    trips.insert(t);

}

when I try to compile it, I get the following error message:

/usr/include/c++/4.2.1/ext/hashtable.h: In member function ‘size_t __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::_M_bkt_num_key(const _Key&, size_t) const [with _Val = trip, _Key = trip, _HashFcn = hash_trip, _ExtractKey = std::_Identity<trip>, _EqualKey = eq_trip, _Alloc = std::allocator<trip>]’:
/usr/include/c++/4.2.1/ext/hashtable.h:599:   instantiated from ‘size_t __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::_M_bkt_num(const _Val&, size_t) const [with _Val = trip, _Key = trip, _HashFcn = hash_trip, _ExtractKey = std::_Identity<trip>, _EqualKey = eq_trip, _Alloc = std::allocator<trip>]’
/usr/include/c++/4.2.1/ext/hashtable.h:1006:   instantiated from ‘void __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::resize(size_t) [with _Val = trip, _Key = trip, _HashFcn = hash_trip, _ExtractKey = std::_Identity<trip>, _EqualKey = eq_trip, _Alloc = std::allocator<trip>]’
/usr/include/c++/4.2.1/ext/hashtable.h:437:   instantiated from ‘std::pair<__gnu_cxx::_Hashtable_iterator<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>, bool> __gnu_cxx::hashtable<_Val, _Key, _HashFcn, _ExtractKey, _EqualKey, _Alloc>::insert_unique(const _Val&) [with _Val = trip, _Key = trip, _HashFcn = hash_trip, _ExtractKey = std::_Identity<trip>, _EqualKey = eq_trip, _Alloc = std::allocator<trip>]’
/usr/include/c++/4.2.1/ext/hash_set:197:   instantiated from ‘std::pair<typename __gnu_cxx::hashtable<_Value, _Value, _HashFcn, std::_Identity<_Value>, _EqualKey, _Alloc>::const_iterator, bool> __gnu_cxx::hash_set<_Value, _HashFcn, _EqualKey, _Alloc>::insert(const typename __gnu_cxx::hashtable<_Value, _Value, _HashFcn, std::_Identity<_Value>, _EqualKey, _Alloc>::value_type&) [with _Value = trip, _HashFcn = hash_trip, _EqualKey = eq_trip, _Alloc = std::allocator<trip>]’
try.cpp:45:   instantiated from here
/usr/include/c++/4.2.1/ext/hashtable.h:595: error: passing ‘const hash_trip’ as ‘this’ argument of ‘size_t hash_trip::operator()(trip)’ discards qualifiers

What am I doing wrong?

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

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

发布评论

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

评论(2

绝不放开 2024-08-10 17:46:39

您应该研究使用 STL 的 TR1 扩展,即

  • unordered_map
  • unordered_set
  • unordered_multimap
  • unordered_mutliset

大多数现代 C++ 编译器都附带这些扩展,因此不需要使用非标准类,例如 hash_set 等。

You should investigate using the STL's TR1 extension namely

  • unordered_map
  • unordered_set
  • unordered_multimap
  • unordered_mutliset

Most modern C++ compilers ship with these extensions, hence there is no need to use a non-standard class such as hash_set etc.

看透却不说透 2024-08-10 17:46:39

这么近!输出中的最后一个错误表明您的 hash_trip 例程应声明为 const

size_t operator()(const trip t) const // note the ending 'const'
{
    //...
}

您可能需要对 eq_trip 执行相同的操作。另外,我建议通过常量引用将参数传递给这些函数,以避免对所传递的数据进行不必要的复制:

size_t operator()(const trip& t) const // note the '&'
{
    //...
}

So close! The last error in your output reveals your hash_trip routine should be declared const:

size_t operator()(const trip t) const // note the ending 'const'
{
    //...
}

You'll probably need to do the same thing for eq_trip. Also, I would recommend passing the arguments to these functions by constant reference to avoid an unnecessary copy of the data you're passing:

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