简体   繁体   中英

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. Internally, the method is trying to increment the iterator. This apparently makes a call to 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.

Most of the various insert functions of std::map require an iterator. Instead, you pass the pointed-to-object, (which is a CString , I suppose):

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:

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!)

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

... convert to the other format

is probably not returning what you think

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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