考虑以下代码:
struct A{};
int main()
{
std::set<A*> aset;
aset.emplace();
std::cout << aset.size() << std::endl; //prints "1"
return 0;
}
为什么空的 emplace()
会向指针集添加一个元素?
最佳答案
因为 emplace
将:
[Insert] a new element into the container by constructing it in-place with the given args if there is no element with the key in the container.
容器之前是空的,所以您肯定要插入一个新元素。零参数是 A*
的有效构造函数,因此代码会编译,您最终会得到一个 set
,其中包含一个指向 A
的值初始化指针>.
关于C++ 为什么 empty set::emplace() 将一个元素插入到一组指针中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30339164/