C++ 为什么 empty set::emplace() 将一个元素插入到一组指针中?

标签 c++ c++11 set move-semantics

考虑以下代码:

struct A{};

int main()
{
    std::set<A*> aset;
    aset.emplace();
    std::cout << aset.size() << std::endl;   //prints "1"
    return 0;
}

DEMO

为什么空的 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/

相关文章:

c++ - 为什么 std::basic_ios 有一个公共(public)构造函数?

c++ - 如何使用 C++ STL 删除集合中从开始到迭代器之前的一个元素的元素?

java - Grails:检查分离的对象是否在附加的集合中

c++ - 函数 to_string() 未使用 g++ mingw --w64 5.3.0 解析

c++ - 如何在 native C++ 中跨线程调用(在主线程上回调)

带 std::array 的 C++ 模板函数

python - 实现 All/Universal 集

启动时出现 C++ 服务错误 : Could not Start the Service on Local Computer

c++ - 检测 CPU 子类型 iPhone

c++ - 为什么编译器允许 string string 而不允许 int int?