I am trying to use std::unordered_map
with std::wstring
and std::pair<int, int>
as value, but I want to compare keys in lower letters. I am using C++20
But I am unable to compile this code.
#include <algorithm>
#include <cctype>
#include <locale>
#include <string>
std::wstring FOREX1 = L"BTCUSD";
std::wstring FOREX2 = L"BTCUSD";
struct CmpByWStringEquality {
bool operator()(const std::wstring& a, const std::wstring& b) const {
// Convert both strings to lowercase
std::wstring lowerA = a;
std::wstring lowerB = b;
std::transform(lowerA.begin(), lowerA.end(), lowerA.begin(), ::towlower);
std::transform(lowerB.begin(), lowerB.end(), lowerB.begin(), ::towlower);
return lowerA == lowerB;
}
};
struct PatternMatcher {
std::wstring inputValue;
explicit PatternMatcher(std::wstring a_inputValue) : inputValue(std::move(a_inputValue)) {}
static void thirdFunction(std::unordered_map<std::wstring, std::pair<int, int>, CmpByWStringEquality>& m_symbol_spreads_map) {
m_symbol_spreads_map.insert(std::make_pair(FOREX2, std::make_pair(-1, 3)));
}
static void secondFunction(std::unordered_map<std::wstring, std::pair<int, int>, CmpByWStringEquality>& m_symbol_spreads_map) {
if (m_symbol_spreads_map.find(FOREX1) == m_symbol_spreads_map.end()) {
thirdFunction(m_symbol_spreads_map);
}
double bid_spread = m_symbol_spreads_map.at(FOREX1).first;
double ask_spread = m_symbol_spreads_map.at(FOREX1).second;
std::cout << "Bid spread: " << bid_spread << std::endl;
std::cout << "Ask spread: " << ask_spread << std::endl;
}
static void firstFunction() {
std::unordered_map<std::wstring, std::pair<int, int>, CmpByWStringEquality> m_symbol_spreads_map = {};
secondFunction(m_symbol_spreads_map);
std::cout << "Symbol spread map size: " << m_symbol_spreads_map.size() << std::endl;
std::cout << m_symbol_spreads_map.at(FOREX1).first << std::endl;
std::cout << m_symbol_spreads_map.at(FOREX1).second << std::endl;
}
};
This is my program output when compiling this code
====================[ Build | untitled | Debug ]================================
"D:Program FilesCLion 2024.1bincmakewinx64bincmake.exe" --build C:UserspcCLionProjectsuntitledcmake-build-debug --target untitled -j 6
[1/2] Building CXX object CMakeFilesuntitled.dirmain.cpp.obj
FAILED: CMakeFiles/untitled.dir/main.cpp.obj
C:PROGRA~1MICROS~22022COMMUN~1VCToolsMSVC1440~1.338binHostx86x86cl.exe /nologo /TP /DWIN32 /D_WINDOWS /EHsc /Ob0 /Od /RTC1 -std:c++17 -MDd -Zi /showIncludes /FoCMakeFilesuntitled.dirmain.cpp.obj /FdCMakeFilesuntitled.dir /FS -c C:UserspcCLionProjectsuntitledmain.cpp
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includexhash(120): error C2064: term does not evaluate to a function taking 1 arguments
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includexhash(120): note: class does not define an 'operator()' or a user defined conversion operator to a pointer-to-function or reference-to-function that takes appropriate number of arguments
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includexhash(120): note: while trying to match the argument list '(const _Kty)'
with
[
_Kty=std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>
]
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includexhash(120): note: the template instantiation context (the oldest one first) is
C:UserspcCLionProjectsuntitledmain.cpp(25): note: see reference to class template instantiation 'std::unordered_map<std::wstring,std::pair<int,int>,CmpByWStringEquality,std::equal_to<std::wstring>,std::allocator<std::pair<const std::wstring,std::pair<int,int>>>>' being compiled
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includeunordered_map(436): note: while compiling class template member function 'std::pair<int,int> &std::unordered_map<std::wstring,std::pair<int,int>,CmpByWStringEquality,std::equal_to<std::wstring>,std::allocator<std::pair<const std::wstring,std::pair<int,int>>>>::at(const std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> &)'
C:UserspcCLionProjectsuntitledmain.cpp(33): note: see the first reference to 'std::unordered_map<std::wstring,std::pair<int,int>,CmpByWStringEquality,std::equal_to<std::wstring>,std::allocator<std::pair<const std::wstring,std::pair<int,int>>>>::at' in 'PatternMatcher::secondFunction'
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includeunordered_map(437): note: while compiling class template member function 'size_t std::_Uhash_compare<_Kty,_Hasher,_Keyeq>::operator ()<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>>(const _Keyty &) noexcept(<expr>) const'
with
[
_Kty=std::wstring,
_Hasher=CmpByWStringEquality,
_Keyeq=std::equal_to<std::wstring>,
_Keyty=std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>
]
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includexhash(143): note: see reference to variable template 'const bool _Nothrow_hash<CmpByWStringEquality,std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > >' being compiled
ninja: build stopped: subcommand failed.