假设我有一张 map
std::map<int, double> foo;
是我写作中的行为foo[2] += 3.0;
定义?也就是说,在我的情况下,是否会自动初始化任何隐式添加的 map 元素(希望为 0.0
)?
如果不是,我是否会引入一大堆未定义的行为?如果是这样,我可以用分配器做一些时髦的事情来强制初始化到0.0
吗? ?
最佳答案
是的,它将是 value-initialized (在您的情况下为 0.0
)。根据cppreference :
Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.
If an insertion is performed, the mapped value is value-initialized (default-constructed for class types, zero-initialized otherwise) and a reference to it is returned.
关于c++ - 添加到 std::map 的元素是否会自动初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32247436/