unordered-map

Key already exists in unordered_map, but “find” returns as not found

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-20 02:19:15
问题 I constructed an unordered_map using key type rot3d, which is defined below: #ifndef EPS6 #define EPS6 1.0e-6 #endif struct rot3d { double agl[3]; // alpha, beta, gamma in ascending order bool operator==(const rot3d &other) const { // printf("== used\n"); return abs(agl[0]-other.agl[0]) <= EPS6 && abs(agl[1]-other.agl[1]) <= EPS6 && abs(agl[2]-other.agl[2]) <= EPS6; } }; Equality of rot3d is defined by the condition that each component is within a small range of the same component from the

Key already exists in unordered_map, but “find” returns as not found

烂漫一生 提交于 2021-02-20 02:18:08
问题 I constructed an unordered_map using key type rot3d, which is defined below: #ifndef EPS6 #define EPS6 1.0e-6 #endif struct rot3d { double agl[3]; // alpha, beta, gamma in ascending order bool operator==(const rot3d &other) const { // printf("== used\n"); return abs(agl[0]-other.agl[0]) <= EPS6 && abs(agl[1]-other.agl[1]) <= EPS6 && abs(agl[2]-other.agl[2]) <= EPS6; } }; Equality of rot3d is defined by the condition that each component is within a small range of the same component from the

std::unordered_map, can you access the elements in a bucket with its bucket number? [duplicate]

纵饮孤独 提交于 2021-02-19 08:01:10
问题 This question already has an answer here : C++11 get all items of one bucket in a unordered_map (1 answer) Closed 5 years ago . for std::unordered_map , can you access the elements in some bucket i if its bucket_size is not zero? 回答1: The answer is to use std::unordered_map::begin(bucket_num) or std::unordered_map::cbegin(bucket_num) to get the iterator pointing to the the first element to that bucket and iterate to the end of the bucket std::unordered_map::end(bucket_num) for ( auto it = u

std::unordered_map, can you access the elements in a bucket with its bucket number? [duplicate]

社会主义新天地 提交于 2021-02-19 08:01:09
问题 This question already has an answer here : C++11 get all items of one bucket in a unordered_map (1 answer) Closed 5 years ago . for std::unordered_map , can you access the elements in some bucket i if its bucket_size is not zero? 回答1: The answer is to use std::unordered_map::begin(bucket_num) or std::unordered_map::cbegin(bucket_num) to get the iterator pointing to the the first element to that bucket and iterate to the end of the bucket std::unordered_map::end(bucket_num) for ( auto it = u

Why does the following code compile using clang but not gcc

这一生的挚爱 提交于 2021-02-16 18:24:47
问题 #include <iostream> #include <unordered_map> #include <string> struct tree_node { // tree_node() : attrib_val{"null"} {} std::unordered_map<std::string, tree_node> child; }; int main(int argc, char const *argv[]) { return 0; } This code compiles just fine on my mac with clang: $ g++ --version Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1 Apple LLVM version 10.0.1 (clang

Why does the following code compile using clang but not gcc

点点圈 提交于 2021-02-16 18:24:25
问题 #include <iostream> #include <unordered_map> #include <string> struct tree_node { // tree_node() : attrib_val{"null"} {} std::unordered_map<std::string, tree_node> child; }; int main(int argc, char const *argv[]) { return 0; } This code compiles just fine on my mac with clang: $ g++ --version Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1 Apple LLVM version 10.0.1 (clang

Is the order of two same unordered_maps the same?

隐身守侯 提交于 2021-02-07 18:41:44
问题 In other words, if I fill two unordered_map , or unordered_set , objects with exactly the same content and the same hashing function, will iterating over them give the same sequence of key/value pairs? If so, then what are the conditions for this to hold (e.g. same hashing function, same keys, not necessarily same values). 回答1: No. There is no requirement, for example, that objects that have the same hash be placed in any particular order. In fact, in general it's impossible for an unordered

Why is vector faster than unordered_map?

狂风中的少年 提交于 2021-02-05 03:47:26
问题 I am solving a problem on LeetCode, but nobody has yet been able to explain my issue. The problem is as such: Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. Each letter in the magazine string can only be used once in your ransom note. Note: You may assume that both strings contain only lowercase letters.

Why is vector faster than unordered_map?

梦想的初衷 提交于 2021-02-05 03:29:46
问题 I am solving a problem on LeetCode, but nobody has yet been able to explain my issue. The problem is as such: Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. Each letter in the magazine string can only be used once in your ransom note. Note: You may assume that both strings contain only lowercase letters.

Timed vector vs map vs unordered_map lookup

百般思念 提交于 2020-12-31 04:05:25
问题 I was curious on vector lookup vs map lookup and wrote a little test program for it.. its seems like vector is always faster the way I'm using it.. is there something else I should take into consideration here? Is the test biased in any way? The results of a run is at the bottom.. its in nanoseconds, but gcc doesn't seem to support it on my platform. Using string for the lookup would of course change things a lot. The compile line I'm using is this: g++ -O3 --std=c++0x -o lookup lookup.cpp