如何在 Linux C++中使用字符串来字符串hash_map(hash_map<string, string, stringHashFunction>
How to use string to string hash_map(hash_map<string, string, stringHashFunction> in Linux C++
我定义了一个hash_map<string,> stringHashMap在Ubuntu与GCC。我保证stringHashFunction是正确的,因为我可以在hash_map中正确使用string to
当我呼叫
string a = "sgsg";
string temp = stringHash[a];
编译器报告错误:
错误:传递' const __gnu_cxx::hash_map, std::basic_string, StringHashFunctionStruct> '作为' this '参数' _t&__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] '放弃限定符[-fpermissive]
为什么会发生这种情况?我应该如何使用字符串到字符串的hashMap?
[]
accessor可以修改map,因此不能是非const。用find代替
注意最后一次访问(对元素'd')是如何插入一个新元素的使用该键映射并初始化为其默认值(一个空的字符串),即使访问它只是为了检索它的值。成员函数map::find不会产生此效果。
stringHash
要么直接是const
,要么作为const引用传递/存储。operator[]
是非const函数,因为它可能需要插入一个项。如果需要在const散列容器中查找项,只需使用find
方法。
- map<string, int> m= {}; 在C++中是什么意思?
- c++11:在VS2015中分配全局STL "map<string, string>" initializer_list崩溃
- freq_table[field_pm_value] = map<string, int>(); 是什么意思?
- C++ Equivalent of Java Map<String, Object>
- 如何在PHPCPP库中将C++map<string,map<string>>转换为php::value?
- 方括号在"map <string, int> instance[numberFeatures];"中是什么意思
- 如何在C++中打印矢量<map<string,int> >
- 如何将返回的 Python 字典转换为 C++ std::map<string,string>
- std::map<string, int> 在执行 m[0] 时崩溃
- boost-python 当C++方法返回 std::map<string,X*>
- C ++如果我使用引用,为什么要清空我的map<string,string>?
- 如何使用单 for 循环遍历 std::map<string>int 和 std::vector<int>?
- 使用 map<string,int> 在开关大小写语句中使用字符串
- Python 数据结构:map<string、vector<int>>
- 比map<string,map<string,vector更好的东西>
- map<string, class> using and couting
- map<string, int> 从 const 函数调用时会抛出 std::out_of_range
- 读取 std::map<string, string> 使用 ReadProcessMemory
- static const std::map<string, int> vs if-elseif
- 为什么map<string,string>接受整数作为值?