【发布时间】:2015-04-22 14:39:16
【问题描述】:
在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");
谢谢。
【问题讨论】:
-
没有SSCCE,这不会去任何地方
-
嗯,SSCCE 包含在 J. Calleja 在另一个主题中给出的答案中。我的问题很简单,如果 low_high_value 是一个接受模板的类,如何扩展它。如果您认为有必要,我可以复制粘贴他们的示例。
-
是的。一定要告诉我们您遇到的问题。
-
好的,我插入了一个更详细的示例。感谢您的宝贵时间。
-
您可以使用 vecetor 进行存储,但您仍然需要元数据(大小)可用。所以变化不大
标签: c++ templates boost boost-propertytree