gpt4 book ai didi

具有常量值的 C++ Boost ptr_map

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:52 25 4
gpt4 key购买 nike

我正在处理一个现有的项目,我正在尝试替换一个 std::map<AClass,const *BCLass>boost::ptr_map<AClass,const BClass> .问题是我找不到将一对插入 map 的方法。似乎所有的插入函数都不能接受常量值。有解决办法吗?我也尝试过使用 boost::assign::ptr_map_insert() 但没有成功。

问候。

最佳答案

这似乎是不受支持的

我什至无法用 boost::assign::ptr_map_insert¹ 实现奇迹

我建议创建一个构建 map 的函数,然后只返回一个 const 拷贝或对它的引用:

ptr_map<A,V> const m = make_my_map();

¹ 如果您有兴趣:

#include <boost/ptr_container/ptr_map.hpp>
#include <boost/assign/ptr_map_inserter.hpp>

struct A {
int i;
A(int i):i(i) {}
bool operator<(A const& other) const { return i<other.i; }
};
struct B { std::string s;
B(std::string s) : s(std::move(s)) { }
};

int main() {
boost::ptr_map<A, /*const*/ B> m;

boost::assign::ptr_map_insert(m)(42, "forty-two");
}

关于具有常量值的 C++ Boost ptr_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28908864/

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