// Custom stack implementation
#include
#include
#include
#include
// Custom hash function for std::stack
struct StackHash {
template
std::size_t operator()(const std::stack& s) const {
std::size_t seed = 0;
auto copy = s; // Create a copy to iterate over
while (!copy.empty()) {
std::hash hasher;
seed ^= hasher(copy.top()) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
copy.pop();
}
return seed;
}
};
// Custom equality operator for std::stack
template
bool operator==(const std::stack& lhs, const std::stack& rhs) {
if (lhs.size() != rhs.size()) return false;
auto copyLhs = lhs;
auto copyRhs = rhs;
while (!copyLhs.empty()) {
if (copyLhs.top() != copyRhs.top()) return false;
copyLhs.pop();
copyRhs.pop();
}
return true;
}
int main() {
std::stack stack1, stack2;
stack1.push(1);
stack1.push(2);
stack2.push(1);
stack2.push(2);
std::cout << "Stacks are equal: " << (stack1 == stack2) << std::endl;
StackHash stackHash;
std::cout << "Hash of stack1: " << stackHash(stack1) << std::endl;
return 0;
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?