c++ - 在 std::map<CString, CString> 中插入新对

标签 c++ stl

我有两个列表和一个名字要查找。如果要查找的名称不在第一个列表中,那么它可能位于第二个列表中,但格式略有不同。给出了两种格式之间的转换函数。

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/

相关文章:

c++ - recv() 失败 : Bad file descriptor c++ Linux

c++ - std::copy 与 memcpy 的效率

c++ - 使用 std::map<K,V> 其中 V 没有可用的默认构造函数

c++ - 是否有一个 vector 可以处理非标准位长的整数?

c++ - 在 MS VC++ 2005 中调试堆损坏错误

带有自定义内存分配器的 std::vector 的 C++ 模板

c++ - 使用 ' ios::sync_with_stdio(0)' 测试运行时差异时,为什么我的输出是零散的?

C++,前向声明和递归数据类型

c++ - 在c++标准模板库(STL)中使用set

c++ - boost 日期/时间