gpt4 book ai didi

c++ - 如何在 Linux C++ 中使用字符串到字符串 hash_map(hash_map

转载 作者:行者123 更新时间:2023-11-30 00:39:31 27 4
gpt4 key购买 nike

我在 Ubuntu 中使用 GCC 定义了一个 hash_map stringHashMap。我保证 stringHashFunction 是正确的,因为我可以在 hash_map 中正确使用 string

当我打电话

string a = "sgsg";
string temp = stringHash[a];

编译器报告错误:

error: passing ‘const __gnu_cxx::hash_map, std::basic_string, StringHashFunctionStruct>’ as ‘this’ argument of ‘_Tp& __gnu_cxx::hash_map<_Key, _Tp, _HashFn, _EqualKey, _Alloc>::operator[](const key_type&) [with _Key = std::basic_string, _Tp = std::basic_string, _HashFn = StringHashFunctionStruct, _EqualKey = std::equal_to >, _Alloc = std::allocator >, __gnu_cxx::hash_map<_Key, _Tp, _HashFn, _EqualKey, _Alloc>::key_type = std::basic_string]’ discards qualifiers [-fpermissive]

为什么会这样?我应该如何使用string to string hashMap?

最佳答案

[] 访问器可以修改映射,因此它不能是非常量。改用查找

来自 http://www.cplusplus.com/reference/stl/map/operator%5B%5D/

Notice how the last access (to element 'd') inserts a new element in the map with that key and initialized to its default value (an empty string) even though it is accessed only to retrieve its value. Member function map::find does not produce this effect.

关于c++ - 如何在 Linux C++ 中使用字符串到字符串 hash_map(hash_map<string, string, stringHashFunction>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8652509/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com