Boost logo

Boost Users :

Subject: Re: [Boost-users] [Variant] Sorting data out of a vector of variant
From: Igor R (boost.lists_at_[hidden])
Date: 2009-10-14 10:35:34


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



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net