copy construction
unknown
c_cpp
a year ago
1.6 kB
20
Indexable
Never
#include <iostream> #include <unordered_map> #include <chrono> using namespace std; static const int kA = 54059; // a prime static const int kB = 76963; // another prime static const int kFinish = 37; // also prime uint32_t StrHash(const char* s, int size) { uint32_t h = kFinish; while (size > 0) { h = (h * kA) ^ (s[0] * kB); s++; size--; } return h; } const int UseridLen = 128; class UserIdWrapper { // 比字符串作为key省不少空间 public: char s[UseridLen]; UserIdWrapper(const char *t) { cout << "UserIdWrapper construction" << endl; for (int i = 0; i < UseridLen; i++) s[i] = t[i]; } UserIdWrapper(const UserIdWrapper &rhs) { cout << "UserIdWrapper copy construction" << endl; } bool operator== (const UserIdWrapper &other) const { for (int i = 0; i < UseridLen; i++) { if (s[i] != other.s[i]) { return false; } } return true; } }; namespace std { template <> struct hash<UserIdWrapper> { size_t operator()(const UserIdWrapper &k) const{ return StrHash(k.s, UseridLen); } }; } int main() { std::unordered_map<UserIdWrapper, int64_t> idx_user_id_list_; char s[UseridLen] = "12345;"; int n = 0; auto start = std::chrono::system_clock::now(); for (int i = 0; i < 1; i++) { idx_user_id_list_.emplace(std::make_pair(UserIdWrapper(s), 0)); } auto end = std::chrono::system_clock::now(); std::chrono::duration<double> elapsed_seconds = end-start; cout << "elapsed time: " << elapsed_seconds.count() << "s" << endl; }