unordered_set add example

This commit is contained in:
Ciro Santilli 2017-12-19 23:10:36 +00:00
parent 5283148a47
commit 93afc683dc
4 changed files with 34 additions and 7 deletions

View File

@ -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.
*/

View File

@ -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);

View File

@ -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<int> p(new int);
//int *raw = p;

View File

@ -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<MyClass> {
std::size_t operator()(const MyClass& k) const {
return k.i;
}
};
}
int main() {
std::unordered_set<MyClass> 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);
}