simple C++ hash_set example

前端 未结 2 1000
旧时难觅i
旧时难觅i 2020-12-31 20:10

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

#include 
#include 

        
相关标签:
2条回答
  • 2020-12-31 20:39

    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 '&'
    {
        //...
    }
    
    0 讨论(0)
  • 2020-12-31 20:39

    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.

    • http://en.wikipedia.org/wiki/Unordered_map_%28C%2B%2B_class%29
    • http://publib.boulder.ibm.com/infocenter/comphelp/v9v111/index.jsp?topic=/com.ibm.xlcpp9.aix.doc/standlib/stl_unordered_map.htm
    • http://www.cplusplus.com/reference/unordered_set/unordered_set/
    0 讨论(0)
提交回复
热议问题