简体   繁体   English

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

[英]Insert new pair in std::map<CString, CString>

I have two lists and a name to find.我有两个列表和一个名称要查找。 If the name to find is not in the first list then it might be in the second list with a slightly different format.如果要查找的名称不在第一个列表中,则它可能在格式稍有不同的第二个列表中。 Conversion function between the two formats are given.给出了两种格式之间的转换函数。

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;
    }
}

The error which appears is:出现的错误是:

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

I would like to know why the operator ++ is involved here and then how should I treat this error.我想知道为什么这里涉及运算符++ ,然后我应该如何处理这个错误。

The first argument to map::insert is an iterator, not a CString. map::insert的第一个参数是一个迭代器,而不是一个 CString。 Internally, the method is trying to increment the iterator.在内部,该方法试图增加迭代器。 This apparently makes a call to operator++ .这显然是对operator++的调用。 You don't need to use this insert overload.您不需要使用此插入重载。 It's intended to improve performance when you know a position close to where the element will be inserted.当您知道靠近元素插入位置的位置时,它旨在提高性能。 Just call convertedNames->insert(std::make_pair(*it, justConvertedName)) instead.只需调用convertedNames->insert(std::make_pair(*it, justConvertedName))代替。

Most of the various insert functions of std::map require an iterator. std::map大多数各种insert函数都需要迭代器。 Instead, you pass the pointed-to-object, (which is a CString , I suppose):相反,您传递指向对象,(我想这是一个CString ):

convertedNames->insert(*it, justConvertedName);
                       ^^^
                       this is a CString, not a std::map<CString,CString>::iterator

If you want to insert a key-value pair, use the map's value_type instead which is basically a std::pair made up of key and value:如果要插入键值对,请改用映射的value_type ,它基本上是由键和值组成的std::pair

convertedNames->insert(std::make_pair(*it, justConvertedName));

So the first thing to understand is that templates are always fully implemented in the header, as any classes required are built with that object (just think if the std lib had all possible std::vector implementations built in!)所以首先要理解的是模板总是在头文件中完全实现,因为任何需要的类都是用那个对象构建的(试想一下,如果 std lib 内置了所有可能的 std::vector 实现!)

This means that the implementation of the template is exposed - and in this case, there's a ++ somewhere there.这意味着模板的实现是公开的 - 在这种情况下,那里有一个 ++ 。 If you take the whole error printed (it'll be quite a few more lines) you may even be told which parameter you've got wrong.如果您将整个错误打印出来(它会多几行),您甚至可能会被告知哪个参数出错了。

In any case, we can see that *it is clearly going to be a CString, but I'd guess that this在任何情况下,我们都可以看到 *it 显然将是一个 CString,但我猜这

... convert to the other format ...转换为其他格式

is probably not returning what you think可能不会返回您的想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM