迭代器的boost ::变种 [英] Iterator for boost::variant

查看:158
本文介绍了迭代器的boost ::变种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海兰那里,

我试图去适应现有的code,以提高::变体。该想法是使用升压::变体为多相载体。的问题是,code使用迭代的其余部分来访问向量的元素。有没有办法使用了boost ::变种迭代的方式?

I'm trying to adapt an existing code to boost::variant. The idea is to use boost::variant for a heterogeneous vector. The problem is that the rest of the code use iterators to access the elements of the vector. Is there a way to use the boost::variant with iterators?

我试过

 typedef boost::variant<Foo, Bar> Variant;
 std::vector<Variant> bag;
 std::vector<Variant>::iterator it;
 for(it= bag.begin(); it != bag.end(); ++it){

 cout<<(*it)<<endl;
 }

但它没有工作。

编辑:谢谢您的帮助!但是,在我的设计,我需要从列表中得到一个元素,并通过它周围code的其他部分(这可能是讨厌的,因为我使用的GSL)。使用迭代的想法是,我可以迭代传递给函数,该函数将来自该特定元件上的返回数据进行操作。我看不出如何,使用的for_each做。我需要做类似的东西:

Thank you for your help! But in my design, I need to get one element from the list and pass it around other parts of the code (and that can be nasty, as I'm using GSL). The idea of using an iterator is that I can pass the iterator to a function, and the function will operate on the return data from that specific element. I can't see how to do that using for_each. I need to do something similar to that:

for(it=list.begin(); it!=list.end();++it) {
  for(it_2=list.begin(); it_2!=list.end();++it_2) {

     if(it->property() != it_2->property()) {

        result = operate(it,it_2);

       }
    }

}

谢谢!

推荐答案

当然,那么有。解引用迭代器自然会产生一个的boost ::变体LT; ......方式&gt; 引用或常量引用

Well of course there is. Dereferencing the iterators will naturally yield a boost::variant<...> reference or const-reference.

但它确实意味着,code的其余部分应变量感知。并且特别使用的boost :: static_visitor 来的变种执行操作。

However it does mean that the rest of code should be variant-aware. And notably use the boost::static_visitor to execute operations on the variants.

修改

轻松!

struct Printer: boost::static_visitor<>
{
  template <class T>
  void operator()(T const& t) const { std::cout << t << std::endl; }
};

std::for_each(bag.begin(), bag.end(), boost::apply_visitor(Printer());

请注意如何编写一个访问者自动产生一个predicate的STL算法,MIAM!

Note how writing a visitor automatically yields a predicate for STL algorithms, miam!

现在,返回值的问题:

class WithReturn: boost::static_visitor<>
{
public:
  WithReturn(int& result): mResult(result) {}

  void operator()(Foo const& f) const { mResult += f.suprise(); }
  void operator()(Bar const& b) const { mResult += b.another(); }

private:
  int& mResult;
};


int result;
std::for_each(bag.begin(), bag.end(), boost::apply_visitor(WithReturn(result)));

编辑2:

这很容易,但确实需要一点点拨:)

It's easy, but indeed need a bit of coaching :)

首先,我们此话有2个不同的操作:<!$​​ C $ C> = 和工作

First, we remark there are 2 different operations: != and operate

 struct PropertyCompare: boost::static_visitor<bool>
 {
   template <class T, class U>
   bool operator()(T const& lhs, U const& rhs)
   {
     return lhs.property() == rhs.property();
   }
 };

 struct Operate: boost::static_visitor<result_type>
 {
   result_type operator()(Foo const& lhs, Foo const& rhs);
   result_type operator()(Foo const& lhs, Bar const& rhs);
   result_type operator()(Bar const& lhs, Bar const& rhs);
   result_type operator()(Bar const& lhs, Foo const& rhs);
 };

for(it=list.begin(); it!=list.end();++it) {
  for(it_2=list.begin(); it_2!=list.end();++it_2) {

    if( !boost::apply_visitor(PropertyCompare(), *it, *it_2) ) {

      result = boost::apply_visitor(Operate(), *it, *it_2));

    }

  }
}

对于每一个都不是什么好事在这里,因为这个如果。如果你能以某种因素会工作的如果工作虽然。

For each is not that good here, because of this if. It would work if you could somehow factor the if in operate though.

另外请注意,我通过迭代器不但是引用。

Also note that I pass not iterators but references.

这篇关于迭代器的boost ::变种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆