问题
I would like to know if I can use Hash table as a return type of a function in C++. :)
回答1:
The C++ standard library implementation of a hash table is std::unordered_map and yes, you can happily return it from a function:
std::unordered_map<X, Y> foo() {
std::unordered_map<X, Y> map;
return map;
}
It can be copied because it has a copy constructor†. If you implement your own hash table, it will also be returnable if it has a copy constructor.
† In C++11, a move constructor will be sufficient for the example given.
来源:https://stackoverflow.com/questions/15114790/hash-table-as-return-type-of-a-function-in-c