Reputation: 520
I would like to know if I can use Hash table as a return type of a function in C++. :)
Upvotes: 0
Views: 497
Reputation: 110768
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.
Upvotes: 5