gpt4 book ai didi

c++ - 通过 const 函数中的参数索引返回 std::map 的值

转载 作者:行者123 更新时间:2023-11-30 03:35:26 28 4
gpt4 key购买 nike

有没有办法将 std::map 索引作为参数传递给 const 函数?

const SurfaceProperties &  SurfacePropertiesImpl::getSurfaceProperties(const std::string entityId) const
{
return mSurfaceProperties[entityId];
}

--

 error: passing ‘const std::map<std::__cxx11::basic_string<char>, SurfaceProperties>’ as ‘this’ argument discards qualifiers [-fpermissive]
return mSurfaceProperties[entityId];

最佳答案

你可以使用 at ,它是与 operator[] 相反的 const

const SurfaceProperties&
SurfacePropertiesImpl::getSurfaceProperties(const std::string entityId) const
{
return mSurfaceProperties.at(entityId);
}

如果找不到 key ,它将抛出。

关于c++ - 通过 const 函数中的参数索引返回 std::map 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41310008/

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