I am writing a wrapper around std :: unorered_map, but I'm a little sure how I should provide a public member function to access the iteration provided by the ":" function in C ++ 11, for example:
for(auto x : my_map){
}
How can I provide the same features as above through my wrapper around unordered_map?
Fixed solution:
#include <unordered_map>
#include <mutex>
template<typename T, typename K>
class MyClass{
private:
std::unordered_map<T,K> map;
std::mutex mtx;
public:
MyClass(){}
MyClass<T,K>(const MyClass<T,K>& src){
}
void insert(T key, K value){
mtx.lock();
map[T] = K;
mtx.unlock();
}
K operator[](T key) const
{
return map[key];
}
K get(T key){
return map[T];
}
decltype(map.cbegin()) begin() const
{
return map.begin();
}
decltype(map.cend()) end() const {
return map.end();
}
bool count(T key){
int result = false;
mtx.lock();
result = map.count(key);
mtx.unlock();
return result;
}
};
source
share