关于这个话题,Boost property_tree: multiple values per key ,提出了一种读取 boost 树的多个键的方法。我将在下面粘贴修改后的版本
template<int dim>
struct my_vector
{
std::vector<double> a_vector;
};
翻译者将是:
template<int dim>
struct my_vector_translator
{
typedef std::string internal_type;
typedef my_vector external_type;
// Get a my_vector from a string
boost::optional<external_type> get_value(const internal_type& str)
{
if (!str.empty())
{
std::vector val;
val.resize(dim)
std::stringstream s(str);
double temp;
for (int i=0, i < dim; ++i){
s >> temp;
val.a_vector[i] = temp;
return boost::optional<external_type>(val);
}
else
return boost::optional<external_type>(boost::none);
}
};
现在,我应该如何正确注册翻译器?
namespace boost { namespace property_tree
{
template<typename Ch, typename Traits, typename Alloc>
struct translator_between<std::basic_string< Ch, Traits, Alloc >, my_vector<it want something here of course, where do i get it from ?> >
{
typedef my_vector_translator<it want something here of course, where do i get it from ?> type;
};
} }
包含前面的代码后,您可以将 property_tree 用作:
pt.get<my_vector ??and here where do i put the size ?>("box.x");
不知何故,我觉得 my_vector 类毫无用处。我不能直接使用 std::vector 吗?
我想要这样的东西:
pt.get<std::vector<double>, 3>("mesh.a_line_where_i_know_i_have_3_doubles_to_read");
谢谢。
最佳答案
在我看来,size 已经是 my_vector
类模板的模板参数。
所以
pt.get<my_vector<3> >("box.x");
应该不错
注册:
部分特化可以引入额外的模板参数,因此只需将 size_t N
添加到列表中即可:
namespace boost { namespace property_tree {
template<typename Ch, typename Traits, typename Alloc, size_t N>
struct translator_between<std::basic_string<Ch, Traits, Alloc>, my_vector<N> >
{
typedef my_vector_translator<N> type;
};
} }
关于c++ - 在模板类上 boost property_tree : multiple values per key,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29801003/