0

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. 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. 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. 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; 
2
  • find(array[i][j]) Commented Feb 18, 2021 at 12:44
  • or for range to avoid index: for (const auto& s : array) { for (char c : s) { total += hashMap[c]; } } Commented Feb 18, 2021 at 12:48

1 Answer 1

1
map<char, int> hashMap; 

Your variable name is highly confusing. std::map is not a 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. Yet, you are trying to call a member function size on an array.

You can use std::size instead:

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

Or you can use a range-for loops for better readability:

 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; Don't use 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. I suspect that you intended to write array[i][j] - use just c if you use the suggested range-for.

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; 
Sign up to request clarification or add additional context in comments.

2 Comments

for-range seems even better than std::size(array) :-)
@Jarod42 Good point. Added to the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.