我有两个列表和一个名字要查找。如果要查找的名称不在第一个列表中,那么它可能位于第二个列表中,但格式略有不同。给出了两种格式之间的转换函数。
std::map<CString, CString>* convertedNames;
BOOL CSome::SeekNameWithConversion(std::set<CString> names, CString nameToFind)
{
for (auto it = names.begin(); it != names.end(); ++it)
{
if (nameToFind.Compare(*it) == 0) return true;
auto convertedIt = convertedNames->find(*it);
if (convertedIt != convertedNames->end() &&
nameToFind.Compare(convertedIt->second) == 0)
return true;
CString justConvertedName = ConvertToTheOtherFormat(nameToFind);
convertedNames->insert(*it, justConvertedName); // Error here
return nameToFind.Compare(justConvertedName) == 0;
}
}
出现的错误是:
error C2675: unary '++':
'ATL::CStringT<char,StrTraitMFC_DLL<char,ATL::ChTraitsCRT<_CharType>>>' does
not define this operator or a conversion to a type acceptable to the
predefined operator
我想知道为什么这里会涉及运算符++
,然后我该如何处理这个错误。
最佳答案
大多数各种insert
std::map
的函数需要迭代器。相反,您传递指向的对象(我想它是一个CString
):
convertedNames->insert(*it, justConvertedName);
^^^
this is a CString, not a std::map<CString,CString>::iterator
如果您想插入键值对,请使用映射的value_type
,它基本上是由键和值组成的std::pair
:
convertedNames->insert(std::make_pair(*it, justConvertedName));
关于c++ - 在 std::map<CString, CString> 中插入新对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39875536/