简体   繁体   English

如何在字符串数组中找到 map 的键?

[英]How to find the keys of a map within a string array?

Hello I am trying to add the values of the map to the int variable named "total", once the corresponding keys are found within the string array.您好,一旦在字符串数组中找到相应的键,我正在尝试将 map 的值添加到名为“total”的 int 变量中。 For an example "aab" will give a total of 5 and "bbc" will give a total of 7. I tried running the code, but it wouldn't work.例如,“aab”总共给出 5,“bbc”总共给出 7。我尝试运行代码,但它不起作用。 I instead got the following error:相反,我收到以下错误:

C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_map.h:1221:2: note:   template argument deduction/substitution failed:
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_map.h: In substitution of 'template<class _Kt> decltype (((const std::map<char, int>*)this)->std::map<char, int>::_M_t.std::_Rb_tree<char, std::pair<const char, int>, std::_Select1st<std::pair<const char, int> >, std::less<char>, std::allocator<std::pair<const char, int> > >::_M_count_tr<_Kt, _Req>(__x)) std::map<char, int>::count<_Kt>(const _Kt&) const [with _Kt = std::__cxx11::basic_string<char>]':
Maps1.cpp:34:29:   required from here
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_map.h:1221:2: error: no matching function for call to 'std::_Rb_tree<char, std::pair<const char, int>, std::_Select1st<std::pair<const char, int> >, std::less<char>, std::allocator<std::pair<const char, int> > >::_M_count_tr(const std::__cxx11::basic_string<char>&) const'
In file included from C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/map:60,
                 from Maps1.cpp:1:
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_tree.h:1250:2: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_count_tr(const _Kt&) const [with _Kt = _Kt; _Req = _Req; _Key = char; _Val = std::pair<const char, int>; _KeyOfValue = std::_Select1st<std::pair<const char, int> >; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, int> >]'
  _M_count_tr(const _Kt& __k) const
  ^~~~~~~~~~~
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_tree.h:1250:2: note:   template argument deduction/substitution failed:
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_tree.h:1247:9: error: no type named 'type' in 'struct std::__has_is_transparent<std::less<char>, std::__cxx11::basic_string<char>, void>'
         typename _Req =
         ^~~~~~~~
Maps1.cpp:35:21: error: no match for 'operator[]' (operand types are 'std::map<char, int>' and 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'})
     total += hashMap[array[j]];
                     ^
In file included from C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/map:61,
                 from Maps1.cpp:1:
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_map.h:490:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = char; _Tp = int; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, int> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]'
       operator[](const key_type& __k)
       ^~~~~~~~
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_map.h:490:7: note:   no known conversion for argument 1 from 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'} to 'const key_type&' {aka 'const char&'}
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_map.h:510:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = char; _Tp = int; _Compare = std::less<char>; _Alloc = std::allocator<std::pair<const char, int> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]'
       operator[](key_type&& __k)
       ^~~~~~~~
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_map.h:510:7: note:   no known conversion for argument 1 from 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'} to 'std::map<char, int>::key_type&&' {aka 'char&&'}

I did a similar thing, but using a single string instead of a string array, and it worked.我做了类似的事情,但是使用单个字符串而不是字符串数组,并且它起作用了。 I am a complete beginner in c++, and I am very confused.我是 c++ 的完全初学者,我很困惑。 Please could someone explain the reason for this error and give me an alternative solution.请有人解释这个错误的原因并给我一个替代解决方案。 Thank you very much for your time and effort.非常感谢您的时间和精力。 My code is shown below:我的代码如下所示:

#include <map>
#include <iostream> 
#include <string> 
using namespace std; 
  
int main(){
     map<char, int> hashMap;
     string array[] = {"aab", "ab", "bbc", "cab"};
     hashMap['a'] = 1;
     hashMap['b'] = 3;
     hashMap['c'] = 1;
     int total = 0;
     for(int i = 0; i < array.size(); i++){
         for(int j = 0; j < array[i].size(); j++){
            if(hashMap.find(array[j]) != hashMap.end()){
                   total += hashMap[array[j]];
         }
     }
  }
  cout << total;
 map<char, int> hashMap;

Your variable name is highly confusing.您的变量名称非常混乱。 std::map is not a hashmap. std::map不是 hashmap。

 string array[] = {"aab", "ab", "bbc", "cab"}; ... for(int i = 0; i < array.size(); i++){

The problem here is that arrays do not have member functions.这里的问题是 arrays 没有成员函数。 Yet, you are trying to call a member function size on an array.然而,您正试图在数组上调用成员 function size

You can use std::size instead:您可以改用std::size

for(int i = 0; i < std::size(array); i++){

Or you can use a range-for loops for better readability:或者您可以使用 range-for 循环以获得更好的可读性:

 for(auto& str : array) {
     for(char c : str) {

There is also a potential naming confusion because you shadow the standard name std::array that you had introduced to the global namespace with using namespace std;还有一个潜在的命名混淆,因为您using namespace std;了您引入全局命名空间的标准名称std::array Don't use using namespace std;不要使用using namespace std; . .

 if(hashMap.find(array[j]).= hashMap;end()){ total += hashMap[array[j]];

The problem here is that keys of your map are char , but you are trying to search for a string.这里的问题是您的 map 的键是char ,但您正在尝试搜索字符串。 I suspect that you intended to write array[i][j] - use just c if you use the suggested range-for.我怀疑您打算编写array[i][j] - 如果您使用建议的范围,则仅使用c

Furthermore, it is unnecessarily inefficient to do the lookup twice.此外,进行两次查找是不必要的低效。 Store the iterator so you can get the value from it without looking it up again:存储迭代器,以便您可以从中获取值而无需再次查找:

auto it = hashMap.find(array[i][j]);
if(it != hashMap.end()){
    total += it->second;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM