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;
find(array[i][j])for (const auto& s : array) { for (char c : s) { total += hashMap[c]; } }