gpt4 book ai didi

c++ - std::map 是通过引用、按值获取分配器还是纯粹将其用作类型?

转载 作者:搜寻专家 更新时间:2023-10-31 00:27:31 25 4
gpt4 key购买 nike

std::map 将 Allocator 作为构造函数中的参数时,它通过引用从其类模板参数中获取的类型来获取它:

explicit map(const Allocator& alloc);

它是将此引用存储在对象中,还是获取一个拷贝(按值存储),或者两者都不做,仅通过模板参数将其用作类型?你是如何确定的?

最佳答案

分配器被复制到映射中。 std::map 没有具体说明它的作用,所以我们回到 [container.requirements.general]/8 ,它指出:

[...]All other constructors for these container types take a const allocator_type& argument. [ Note: If an invocation of a constructor uses the default value of an optional allocator argument, then the Allocator type must support value-initialization. — end note ] A copy of this allocator is used for any memory allocation and element construction performed, by these constructors and by all member functions, during the lifetime of each container object or until the allocator is replaced.[...]

强调我的

关于c++ - std::map 是通过引用、按值获取分配器还是纯粹将其用作类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48749545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com