std::map 放置 gcc 4.8.2

2023-12-08

我正在尝试使用 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().
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

std::map 放置 gcc 4.8.2 的相关文章

随机推荐