提高property_tree:在模板类上,每个键具有多个值 [英] Boost property_tree: multiple values per key, on a template class

查看:108
本文介绍了提高property_tree:在模板类上,每个键具有多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于此主题,提高property_tree:每个键有多个值提出了一种读取升压树的多重密钥的方法。我将在下面粘贴修改后的版本

On this topic, Boost property_tree: multiple values per key, a method is proposed to read multiples keys of a boost tree. I will paste a modified version below

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);
  }

};

现在,我应该如何正确注册翻译?

And now, how should i do to register the translator properly ?

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用作:

After you include the previous code, you can use property_tree as:

  pt.get<my_vector ??and here where do i put the size ?>("box.x");

以某种方式,我觉得类my_vector没用。我不能直接使用std :: vector吗?

And somehow i feel that the class my_vector is useless. Couldn't I work directly with std::vector ?

我想要类似的东西:

pt.get<std::vector<double>, 3>("mesh.a_line_where_i_know_i_have_3_doubles_to_read");

谢谢。

推荐答案

在我看来,大小已经是 my_vector 类模板的模板参数。

It seems to me that size is already the template argument to the my_vector class template.

所以

pt.get<my_vector<3> >("box.x");

应该不错

部分专业化可以引入额外的模板参数,因此只需在列表中添加 size_t N

A partial specialization can introduce extra template arguments, so just add size_t N to the list:

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;
        };
} }

这篇关于提高property_tree:在模板类上,每个键具有多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆