mirror of
https://github.com/libretro/cpp-cheat.git
synced 2025-04-12 16:23:08 +00:00
unordered_set add example
This commit is contained in:
parent
5283148a47
commit
93afc683dc
@ -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.
|
||||
*/
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user