STL 映射和指针出现问题

Trouble with STL map and pointers

本文关键字:问题 指针 映射 STL      更新时间:2023-10-16
typedef map<char,string> someMap;
someMap *mapPtr=someClass.getMap();
*(mapPtr)["a"].length();

此代码的最后一行失败。我应该做什么才能使这项工作?

    (*mapPtr)['a'].length();

*运算符的优先级低于[]因此您必须这样做,但将其放在括号中。 "a"是字符串文字(字符数组),而您想要'a'

可在此处找到C++运算符优先级的完整列表

[]的优先级高于*

这可能是您的意图:

(*mapPtr)["a"].length();