2013-07-18 23 views
1

我試圖問我的問題之前,但我認爲我問我的問題的方式是不正確的。所以我想在這裏再次:(我仍然不知道是什麼主題將是合適的)從提升檢索對象:: 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; 
    } 

所以在最後,我們可能有這樣的事情{版,p0_modified,p0_modified,編輯,女編輯,女編輯,p0_modified,p0_modified,編輯,男,...} { 線,點,點,線,頂點,線,頂點,線,點,點,線,頂點,...}

現在我調用的代碼的另一部分該功能(不同的文件)

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

回答

4

因此,您的數據結構目前是Point,LineVertex對象(我將假定第一句中的Surface是拼寫錯誤)的序列。您還知道任何Line必須跟隨兩個Point或一個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; 

而改變你的代碼來產生這個序列。然後使用它變得微不足道。

+2

或'typedef boost :: variant ,Vertex> AfterLine; struct LineWithAfter {Line line; AfterLine之後; }'。 – Yakk

+0

@ Sebastian Redl,謝謝,我試圖這樣做。 –