c++11 - std::map emplace gcc 4.8.2

标签 c++11 map emplace

我正在尝试使用 std::map 的 emplace 函数,但它似乎没有实现(但我读到它是在 4.8 中实现的)

以下代码:

std::map<std::string, double> maps;
maps.emplace("Test", 1.0);

造成:
class std::map<std::basic_string<char>, double>' has no member named 'emplace'

有人可以澄清 emplace 函数是在哪个 gcc 版本中实现的吗?

最佳答案

这是一些源代码:

#include <map>
#include <string>

int main()
{
    std::map<std::string, double> maps;
    maps.emplace("Test", 1.0);
}

让我们尝试编译它:
[9:34am][wlynch@apple /tmp] /opt/gcc/4.8.2/bin/g++ -std=c++98 foo.cc
foo.cc: In function ‘int main()’:
foo.cc:7:10: error: ‘class std::map<std::basic_string<char>, double>’ has no member named ‘emplace’
     maps.emplace("Test", 1.0);
          ^
[9:34am][wlynch@apple /tmp] /opt/gcc/4.8.2/bin/g++ -std=c++11 foo.cc
[9:34am][wlynch@apple /tmp]

请注意,当我们使用 -std=c++11 时有用。这是因为 std::map::emplace() 是 2011 C++ 标准中添加的一项功能。

此外,我可以验证:
  • g++ 4.7.3 不支持 std::map::emplace() .
  • g++ 4.8.0 确实支持 std::map::emplace() .
  • 关于c++11 - std::map emplace gcc 4.8.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24389014/

    相关文章:

    c++ - 读取数字并转换为单词

    c++ - 没有辅助变量的 std::vector 中的 Pushing_back 值

    objective-c - 在图像上绘制纬度/经度点

    c++ - 指向 char 数组而不是单个 char 的 Char 指针?

    c++ - 如何将不可复制类的默认构造对象放置到 boost::container::map 中?

    c++ - 用另一个函数模板重载一个函数模板是否合法?

    c++ - 如何使一个部分特化模板类成为另一个部分特化模板类的 friend ?

    ios - 如何在 iOS6 中获取行车方向数据?

    c++ - 插入具有没有复制构造函数的对象的 vector

    c++ - 放置指向 shared_ptr 的多重映射的指针不起作用