从boost :: variant检索对象

'

我曾尝试问过我的问题,但我认为我提出问题的方式是不正确的。所以我在这里再次尝试:(仍然不知道哪个主题是合适的)

首先我定义

typedef boost::variant<point, Line, Vertex> vec_variant;
typedef std::vector<vec_variant> vec;

我编写函数的方式取决于它返回点,线,顶点或什至是它们的组合的情况。

vec my_func::select_T(const mesh::section& s, const char* model) const
{
  vec new_vec;
  .
  .
  .
  .
 //loop over my lines
  else if ( strcmp(model , "Line") == 0 )
  {
    for(section::lineIterator ite = s.beginLine(); ite != s.endLine(); ++ite )
    {
      Line ed = *ite;
        Point p0 = ed.point(0);
        Point p1 = ed.point(1);
        Point p0_modified ( /* some modification */  );
        Point p1_modified ( /* some modification */  ); 

        if( /* some other conditions */ )
        {
          new_vec.push_back(ed);
          new_vec.push_back(p0_modified); //note before pushing back any point 
          new_vec.push_back(p1_modified); //first I pushed back line
        }

         else if ( /* some other conditions */ )
         {
           .
           .
           .
           vertex m = .......;
           new_vec.push_back(ed);
           new_vec.push_back(m);  //note before pushing back any point 
                                  //first I pushed back line
         }
      }
    }
  }
      return new_vec;
    }

所以最后我们可能会有这样的内容:{ed,p0_modified,p0_modified,ed,m,ed,m,ed,p0_modified,p0_modified,ed,m,....} {Line,Point,Point,Line,Vertex ,线,顶点,线,点,点,线,顶点,...}

现在,我在代码的另一部分(不同文件)中调用此函数

first I defined a visitor:

    template<typename T>
    struct T_visitor : public boost::static_visitor<>
    {
       T_visitor(std::vector<T>& v) : zeroVector(v) {}
       template<typename U>
       void operator () (const U&) {}
       void operator () (const T& value)
       {
          zeroVector.push_back(value);
       }
    private:
       std::vector<T>& zeroVector;
    };

我在这里调用了上面的函数:

void func_2( /*......*/ )
{
  . //we can use above visitor to store each type (point, line, Vertex) in vactor<point or line or Vertex)
  . //we do not know what the new_vec is at the end of the loop. the only thing we know is that after each line there
  . //would be either two points or one vertex
  .
  const char *model = "Edge";
  .
  .//How to find line ed and corresponded points?
  .
  create_line( Point& p0_modified, Point& p1_modified, Line& ed);    //two modified points and line
  .
  .//How to find point m and corresponded line?
  .
  create_point( m(0), m(1), m(2), Line& ed);    //point m coordinates and line
  .
  .
}
塞巴斯蒂安·雷德尔(Sebastian Redl)

所以,你的数据结构是当前的序列PointLine以及Vertex对象(我要去假设Surface第一句是笔误)。您还知道附加限制,任何限制都Line必须后面加2Point或1 Vertex现在,您要使用此结构。

这是可能的,但也很烦人。我可以建议您仅更改数据结构吗?

struct LineWithPoints {
  Line line;
  Point p1, p2;
};
struct LineWithVertex {
  Line line;
  Vertex v;
};
typedef boost::variant<LineWithPoints, LineWithVertex> vec_variant;
typedef std::vector<vec_variant> vec;

并更改您的代码以生成此序列。然后,使用它就变得微不足道了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何为模板化类检索boost :: variant值

Boost::variant 带有引用相同变体的对象

增量boost :: variant值

Boost-variant的向量

boost :: variant。boost :: visitor重载功能

打印Boost Boost Python对象

Boost Karma:使用boost :: optional <boost :: variant <... >>编写规则

从istream读取boost :: variant类型

Boost :: variant的多态设置器

嵌套Boost :: variant的分段错误

如何将 Boost Variant 与 struct 对象一起使用 C++

如何将Boost :: make_recursive_variant对象转换为字符串?

Boost.Spirit语法的属性:boost :: variant的std:vector错误

使用boost :: visitor和unique_ptr的boost :: variant

通过boost :: mpl获得boost :: variant的类型索引

使用boost :: variant获得理智的比较器

boost :: variant和多态性

从boost :: variant和模板化AST继承

布尔和字符串的boost :: variant

是否可以将boost :: any或boost :: variant与boost :: pool一起使用?

如何在Boost序列化中检索多个对象值

std :: variant和boost :: variant之间有什么区别?

std::variant 是否提供类似于 boost::variant<>::types 的功能?

从给定的嵌套boost-variant类型创建新的boost-variant类型

将Boost Spirit解析器从boost :: variant过渡到std :: variant 2

Boost Karma对象方法调用

是否有boost :: visit像std :: visit一样用于boost :: variant?

如何返回由boost :: varaint返回类型中包含的类型的子集制成的boost :: variant

通过boost递归对.variant自boost 1.62以来就中断了