stl 文档中是如下描述:
Access element
If
k matches the key of an element in the container, the function returns a reference to its mapped value.
I f k does not match the key of any element in the container, the function inserts a new element with that key and returns a reference to its mapped value. Notice that this always increases the container size by one, even if no mapped value is assigned to the element (the element is constructed using its default constructor).
注意斜体字部分,也就是说,如果你的对象想保存在 map 中并且要用到[] 那么你的类起码要实现以下几个函数:无参数的构造函数 --- 对应元素不存在时 map 自行创建。operator = ----- 用于 通过 [] 操作符来赋值拷贝构造函数 ---- 可能要用到,没具体研究,反正实现了总比不实现要好。
本文详细解析了STL map中使用[]操作符时的行为特点,包括当键不存在时自动插入新元素并初始化的过程,以及为了支持这一行为,类需要实现的几个必要成员函数:无参构造函数、赋值运算符和拷贝构造函数。
1886

被折叠的 条评论
为什么被折叠?



