[Variant] Sorting data out of a vector of variant

Hi all! I have some class with a member which is a vector of boost.variant, i.e. class Example { public: std::vector<variant<typeA, typeB, typeC> > content; } I want to add three members to my class that are vectors of pointers to the specific content items class Example { public: std::vector<variant<typeA, typeB, typeC> > content; std::vector<typeA> *contentA; std::vector<typeB> *contentB; std::vector<typeC> *contentC; } Now the question: how can I iterate through the vector content whilst adding its items to the other 3 vectors depending on their type? I know there has to be some way using the apply_visitor functor, but I just don't see how exactly... Thanks for your help! Best regards, Oli

class Example { public: std::vector<variant<typeA, typeB, typeC> > content; }
I want to add three members to my class that are vectors of pointers to the specific content items
class Example { public: std::vector<variant<typeA, typeB, typeC> > content;
std::vector<typeA> *contentA; std::vector<typeB> *contentB; std::vector<typeC> *contentC; }
Now the question: how can I iterate through the vector content whilst adding its items to the other 3 vectors depending on their type? I know there has to be some way using the apply_visitor functor, but I just don't see how exactly...
// untested pseudo-code struct segregator : boost::static_visitor<> { segregator(vector &contentA, etc for all the types) : contentA_(contentA), etc for all the types {} void operator()(const typeA &a) const { contentA_.push_back(a); } // etc for all the types private: vector<typeA> &contentA_;//etc for all the types }; int main() { typedef variant<typeA, typeB, typeC> variant_type; vector<typeA> contentA; segregator seg(contentA,...); for_each(content.begin(), content.end(), seg); }
participants (2)
-
Igor R
-
Oliver Rudolph