diff --git a/cpp/map.cpp b/cpp/map.cpp index d92b13b..5c1b520 100644 --- a/cpp/map.cpp +++ b/cpp/map.cpp @@ -15,9 +15,9 @@ There seems to be no explicit hashmap container, only a generic map interface, - See map. + However unordered_map is likely to be hashmap based. - Nonstandard `hash_map` already provided with gcc and msvc++. + A nonstandard `hash_map` already provided with gcc and msvc++. It is placed in the `std::` namespace, but it is *not* ISO. */ diff --git a/cpp/set.cpp b/cpp/set.cpp index a84d495..fab1b65 100644 --- a/cpp/set.cpp +++ b/cpp/set.cpp @@ -102,8 +102,8 @@ int main() { assert(ret.first == s.find(0)); assert(ret.second == true); - //item already present: - //nothing is done and returns false on the pair + // Item already present: + // nothing is done and returns false on the pair. ret = s.insert(1); assert(ret.first == s.find(1)); assert(ret.second == false); diff --git a/cpp/unique_ptr.cpp b/cpp/unique_ptr.cpp index 65b5979..6621940 100644 --- a/cpp/unique_ptr.cpp +++ b/cpp/unique_ptr.cpp @@ -74,8 +74,8 @@ int main() { manual_ptr_test(); assert(Base::count == 10); - // Convert to raw pointer. - // Not possible. + // ERROR: Convert to raw pointer. + // Not possible, the cast operator is not defined. { std::unique_ptr p(new int); //int *raw = p; diff --git a/cpp/unordered_set.cpp b/cpp/unordered_set.cpp index baabce6..ec71904 100644 --- a/cpp/unordered_set.cpp +++ b/cpp/unordered_set.cpp @@ -22,5 +22,32 @@ #include "common.hpp" -int main() { +class MyClass { + public: + int i; + int j; + MyClass(int i, int j) : i(i), j(j) {} +}; + +bool operator==(const MyClass& lhs, const MyClass& rhs) { + return lhs.i == rhs.i; +} + +namespace std { + template <> + struct hash { + std::size_t operator()(const MyClass& k) const { + return k.i; + } + }; +} + +int main() { + std::unordered_set mySet; + assert(mySet.size() == 0); + mySet.insert(MyClass(1, 2)); + assert(mySet.size() == 1); + mySet.insert(MyClass(1, 3)); + assert(mySet.size() == 1); + assert(mySet.find(MyClass(1, -1))->j == 2); }