c++ - 在字符串的 std::map 中查找编译错误,长

标签 c++ visual-studio compiler-errors stdmap

我正在使用 VC++ 开发 Visual Studio 2013。 我有一个 std::map 如下,

std::map<std::string, unsigned int> myMap;

作为类的静态成员。

我通过这样做将值填充到其中,

string key = "value";
std::map<std::string, unsigned int>::iterator it = myMap.find(key);

if(it == myMap.end())
    myMap.insert(std::pair<std::string, unsigned int> (key, 1));
else
{
    int prev_value = it->second;
    prev_value++;
    myMap.insert(std::pair<std::string, unsigned int> (key, prev_value));
}

这不编译,我得到这个编译错误,

1   IntelliSense: no suitable user-defined conversion from "std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string, long>>>>" to "std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string, unsigned int>>>>" exists  c:\filename.cpp 15

15就是这一行,

std::map<std::string, unsigned int>::iterator it = myMap.find(key);

这个错误,

2   IntelliSense: no operator "==" matches these operands
        operand types are: std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string, unsigned int>>>> == std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string, long>>>> c:\filename.cpp  17

第 17 行是这样的,

if(it == myMap.end())

有人可以帮我解决这个问题吗?

谢谢

最佳答案

wouldn't be getting this error如果您的声明实际上是:

std::map<std::string, unsigned int> myMap

你会 get this exact error但是,如果您的定义是偶然的:

std::map<std::string, long> myMap

在他著名的 Almost Always Auto , Herb Sutter 假设准则:

Remember that preferring auto variables is motivated primarily by correctness, performance, maintainability, and robustness—and only lastly about typing convenience.

正如 Herb Sutter 所建议的那样 auto (和 make_pair )不仅可以使您的代码正确,而且可以使其更健壮,即它可以独立支持 myMap您使用了上述哪些声明的声明:

const string key = "value"s;
const auto it = arg.find(key);

if(it == arg.end()) {
    arg.insert(make_pair(key, 1));
} else {
    int prev_value = it->second;
    prev_value++;
    arg.insert(make_pair(key, prev_value));
}

Live Example

编辑:

Igor Tandetinik has reminded memap::insert :

Inserts element(s) into the container, if the container doesn't already contain an element with an equivalent key

这意味着您的整个 else block 不会执行任何操作,因为它只会在 map 已经包含具有等效键的元素时触发。大概您打算使用 map::insert_or_assign或类似的,在这种情况下,您的整个代码应该简单地替换为:

++myMap["value"s]

编辑 2:

请注意,上面的代码之所以有效,是因为在 C++ 中,下标运算符的优先级高于前缀递增运算符。但是 myMap["value"s]++ 将不起作用,因为后缀增量的优先级高于下标运算符。查看C++ operator precedence chart有关运算符优先级的更多信息。

在这种情况下,这种排序是一件好事,因为它迫使您做出更好的编码决策。具体来说,在 C++ 中,出于优化原因,最佳实践是使用 prefix 增量运算符而不是 postfix 增量运算符(尽管在大多数情况下,现代编译器会为您最好地清理它实践仍然有效。)

关于c++ - 在字符串的 std::map 中查找编译错误,长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41518080/

相关文章:

c++ - 在 C++ 中从数组函数设置一个数组

c++ - Qt Key Event 的行为与键盘不相似

c++ - 数组模板和内置数组类型之间有区别吗?

python - 为什么将整数转换为字符串,将其切片,然后再转换回整数,这给我带来TypeError吗?

c++ - 命名管道上的 WriteFile 有时会返回 ERROR_NO_DATA

c++ - 未解析的外部符号错误,即使函数存在 char*

c# - 对于 .net core 3.0 中的 wpf 项目,Xaml UI Designer 处于非事件状态

c# - 统计数组中字符和单词的个数

c# - Form1 资源与项目资源

c - .c 源文件消失了,还有希望吗?