I want to use a cache, implemented by boost's unordered_map, from a dynamic_bitset to a dynamic_bitset. The problem, of course, is that there is no default hash function from the bitset. It doesn't seem to be like a conceptual problem, but I don't know how to work out the technicalities. How should I do that?
C++ rehash()和reserve()方法之间有什么区别unordered_map?为什么需要两种不同的方法?
我正在尝试使用在Android NDK中定义的hash_map,但我收到了"弃用警告":
ndk/sources/cxx-stl/gnu-libstdc++/4.6/include/ext/../backward/backward_warning.h:33:2:
error: #warning This file includes at least one deprecated or antiquated header which may
be removed without further notice at a future date. Please use a non-deprecated interface
with equivalent functionality instead. For a listing of replacement headers and
interfaces, consult the file backward_warning.h. To disable this warning use -Wno-
deprecated. [-Werror=cpp]
Run Code Online (Sandbox Code Playgroud)
由于"unordered_map"存在于gnu-libstdc ++/4.6/include /以及gnu-libstdc ++/4.6/include/tr1 /中,我相信有一种方法可以使用它.
关键是我找不到它.以下哪一项是正确的(如果有的话):
#include <tr1/unordered_map.h>
#include <unordered_map>
Run Code Online (Sandbox Code Playgroud)
然后,如何使用它?__gnu_cxx :: unordered_map无法识别...我不知道如何找到这些信息.
我正在选择将事物放入 unordered_map 的两种方法之一:
std::unordered_map<Key, Value> map;
map.emplace(
std::piecewise_construct,
std::forward_as_tuple(a),
std::forward_as_tuple(b, c, d));
Run Code Online (Sandbox Code Playgroud)
对比
std::unordered_map<Key, DifferentValue> map;
auto& value = map[a];
if (value.isDefaultInitialized())
value = DifferentValue(b, c, d);
Run Code Online (Sandbox Code Playgroud)
我做了一些实验,看看哪个效果更好,发现在插入唯一元素时,行为(如效率)基本上是等效的。
然而,在插入重复项的情况下,考虑到Value或DifferentValue的构造不是微不足道的,我惊讶地发现,emplace构造对象而不管它是否插入它。
因此,在这种情况下,第二种方法似乎胜出,因为默认构造函数中只有 isDefaultInitialized_(true) 而不是更多。
对于 emplace,代码似乎是:
... _M_emplace(std::true_type, _Args&&... __args) {
__node_type* __node = _M_allocate_node(std::forward<_Args>(__args)...);
const key_type& __k = this->_M_extract()(__node->_M_v);
...
if (__node_type* __p = _M_find_node(__bkt, __k, __code)) {
_M_deallocate_node(__node);
return std::make_pair(iterator(__p), false);
}
return std::make_pair(_M_insert_unique_node(__bkt, __code, __node), true);
}
Run Code Online (Sandbox Code Playgroud)
因此,尽管我将使用第二种方法(即使它需要移动赋值和移动构造函数和额外的字段),但我想知道为什么 emplace 创建一个后来忽略的对象有很好的理由吗?也就是说,它是否应该首先检查它是否需要创建对象并在它已经存在时提前退出?
(请注意,对于我的特殊情况,默认初始化项不被认为是有效的,所以问题实际上只是关于 emplace)
作为记录,我在 23.2.4 表 102 下找到了一些内容:
E?ects: Inserts …Run Code Online (Sandbox Code Playgroud) 如何识别一个中的键是否std::unordered_map经历了哈希冲突?
也就是说,如何识别是否存在任何碰撞链?
#include <bits/stdc++.h>
std::unordered_map<std::pair<int,int>, int> mp;
int main()
{
mp[make_pair(1, 2)]++;
}
Run Code Online (Sandbox Code Playgroud)
使用时[] operator,我得到了这个
error: no match for ‘operator[]’ (operand types are ‘std::unordered_map<std::pair<int, int>, int>’ and ‘std::pair<int, int>’)
Run Code Online (Sandbox Code Playgroud)
但是,当用 做同样的事情时std::map,不会发生错误。为什么?
我怎样才能让它工作std::unorderd_m?
我需要从给定的字符串中提取特定大小的所有子字符串,然后在中查找每个子字符串std::unordered_map<string, int*>。我尝试使用此答案中的建议并用作std::less<>比较器,但编译器(gcc 8.2)抱怨以下错误。我不知道在出现错误的情况下摆弄比较器是否有意义unordered_map。
/afs/software/gcc/8.2.0/lssc0-linux/include/c++/8.2.0/bits/hashtable.h:195:21: error: static assertion failed: hash function must be invocable with an argument of key type
static_assert(__is_invocable<const _H1&, const _Key&>{}
c_counter.cpp: In function ‘void process(char*)’:
c_counter.cpp:158:27: error: no matching function for call to ‘std::unordered_map<std::__cxx11::basic_string<char>, int*, std::less<void> >::find(std::string_view&)’ if (counts->find(k) != counts->end()) {
Run Code Online (Sandbox Code Playgroud)
代码是:
std::unordered_map<std::string, int*, std::less<>> *counts = new std::unordered_map<std::string, int*, std::less<>> ;
// stuff
void prcoess(char* r) {
std::string_view seq(r) ;
for (int i = 0 ; i …Run Code Online (Sandbox Code Playgroud) 我正在解决LeetCode上的问题,但是没有人能够解释我的问题。
给定一个任意的赎金票据字符串和另一个包含所有杂志字母的字符串,编写一个函数,如果可以从杂志中构造赎金票据,则该函数将返回true;否则,它将返回false。
杂志字符串中的每个字母只能在赎金记录中使用一次。
注意:您可以假定两个字符串都只包含小写字母。
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
Run Code Online (Sandbox Code Playgroud)
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
if(ransomNote.size() > magazine.size()) return false;
unordered_map<char, int> m;
for(int i = 0; i < magazine.size(); i++)
m[magazine[i]]++;
for(int i = 0; i < ransomNote.size(); i++)
{
if(m[ransomNote[i]] <= 0) return false;
m[ransomNote[i]]--;
}
return true;
}
};
Run Code Online (Sandbox Code Playgroud)
bool canConstruct(string ransomNote, string magazine) {
int lettersLeft = ransomNote.size(); // Remaining # of …Run Code Online (Sandbox Code Playgroud) 考虑这个代码。我为 unordered_map 保留 6 个点并插入 6 个元素。之后,有7个桶。为什么是这样?max_load_factor 为 1,并且有足够的存储桶用于我插入的元素数量。
#include <iostream>
#include <unordered_map>
using namespace std;
int main () {
unordered_map<std::string,std::string> mymap = {
{"house","maison"},
{"apple","pomme"},
{"tree","arbre"},
{"book","livre"},
{"door","porte"},
{"grapefruit","pamplemousse"}
};
unordered_map<std::string,std::string> mymap2; // THIS ONE!!!
mymap2.reserve(6);
for (auto i:mymap) {
mymap2[i.first] = i.second;
}
std::cout << "max_load factor " << mymap2.max_load_factor() << " mymap has " << mymap2.bucket_count() << " buckets.\n";
for (unsigned i=0; i<mymap2.bucket_count(); ++i) {
cout << "bucket #" << i << " contains: ";
for (auto …Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么插入到std::unordered_map容器中只会使迭代器无效,而不会使引用和指针无效。另外,我无法理解 https://en.cppreference.com/w/cpp/container/unordered_map/insert 中的以下声明的含义
如果插入成功,则在元素保存在节点句柄中时获得的指向该元素的指针和引用将失效,而在提取该元素之前获得的指向该元素的指针和引用将变得有效。