|
Objective-C++
Custom Hash & Equals Functors for Using Objective-C Objects as Keys in C++ Containers »
|
template <typename T> struct ObjCHash {
size_t operator()(const T s1) const { return (size_t)s1; }
};
template <typename T> struct ObjCEquals {
size_t operator()(const T, const T b) const { return a==b; }
};
// Maps of NSInvocations to call count...
std::unordered_map< NSInovcation *, NSUInteger, ObjCHash<NSInvocation *>, ObjCEquals<NSInvocation *> >callCount; |
|
C++
Declare a C++ Unordered Map with a Custom Pair Type as Key »
|
typedef std::pair<IMAxis, IMMotionType> _IMAxisMotionTypePair;
// Functor to compute hash of the custom std::pair
typedef struct {
std::size_t operator() (const _IMAxisMotionTypePair& pair) const {
return pair.first << 8 | pair.second; // Combine the enums
}
} _IMAxisMotionTypePairHash;
// Declare with custom hash functor
// std::pair has an equals function so no need to define one here
std::unordered_map< _IMAxisMotionTypePair, NSTimeInterval, _IMAxisMotionTypePairHash > _motionTimeWindowMap; |
|
C++
Checking Whether a Key Exists in a C++ unordered_map »
|
// mymap is a std::unordered_map
if (myMap.find(key) != map.end()) {
return;
} |
|
C++
Initialise a C++ Queue with a Set of Values »
|
int poolSize = 100;
float initial = 0.0;
std::queue<float> myQueue(std::list<float>(initial, poolSize)); |
|
C++
Iterating Through Map’s, Vectors, and Set’s in C++11 »
|
// SETS & VECTORS
//
std::unordered_set<T> mySet;
for (const auto& elem: mySet) {
// ...
}
// The older way...
std::unordered_set<T> mySet;
for (auto itr = mySet.begin(); itr != mySet.end(); ++itr) {
// Use (*itr)
}
//
// MAPS
//
std::unordered_map<int, string> myMap;
for (const auto& item: myMap) {
// Use item->first, item->second
}
// Or
std::unordered_map<int, string> myMap;
for (auto itr = myMap.begin(); itr != myMap.end(); ++itr) {
// Use (*itr)->first, (*itr)->second
} |
|
intermediate
Obj-C safe, forward declaration for a C++ class in an Obj-C++ header file »
|
// Let's say you have an Obj-C++ class which needs to reference a C++ class,
// perhaps for an instance variable, and you wish to include and use this
// Obj-C++ class in a regular Obj-C class. You can't include the C++ header in the
// Obj-C++ header file as this will cause problems with the importing Obj-C class.
// This snippet is the equivalent of @class for forward defining the C++ class
// in the Obj-C++ class in a way compatible with plain old Obj-C
/// Forward definition for C++ class
struct MyCPPClass;
typedef struct MyCPPClass MyCPPClass;
@interface MyObjCPPClass {
MyCPPClass anIvar;
}
// ...
@end |