怎样遍历一个元组 [英] How do I iterate over a tuple
本文介绍了怎样遍历一个元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何可以遍历,比如说,从索引1开始2元组?以下是行不通的。
使用boost ::融合::利弊;
typedef的利弊< A,利弊< B,缺点< C,利弊< D> > > > MyTuple;
MyTuple tuple_;模板<类T>
结构DoSomething的{ DoSomething的(T& T公司):■_(& T公司){} 模板<类U>
void运算符()(U U){
提高::融合:: AT< MPL :: int_< U> >(* T_);
}
T * T_;
};提高:: MPL :: for_each的<升压:: MPL :: range_c&下;整型,1,3 GT; >(DoSomething的< MyTuple>(tuple_));
解决方案
我不知道你的意图,但下面的code你有用吗?
我用融合
遍,而不是 MPL
。
结构DoSomething的{
模板<类U>
void运算符()(U U)const的{
性病::法院LT&;< U<<的'\\ n'; // 一个例子
}
};使用空间boost ::融合; //对不起,为了简洁iterator_range的<
的result_of :: advance_c<的result_of ::开始< MyTuple> ::类型,1> ::类型
,的result_of :: advance_c<的result_of ::开始< MyTuple> ::类型,3 GT; ::类型
> IR(advance_c&所述1为卤素;(开始(tuple_))
,advance_c< 3 GT;(开始(tuple_)));
的for_each(1R,DoSomething的());
希望这有助于
How can I iterate over a tuple starting from, say, index 1 to 2? The following doesn't work.
using boost::fusion::cons;
typedef cons<A, cons<B, cons<C, cons<D> > > > MyTuple;
MyTuple tuple_;
template <class T>
struct DoSomething{
DoSomething(T& t) : t_(&t){ }
template <class U>
void operator()(U u){
boost::fusion::at<mpl::int_<u> >(*t_);
}
T* t_;
};
boost::mpl::for_each< boost::mpl::range_c<int, 1, 3> >( DoSomething<MyTuple>(tuple_) );
解决方案
I'm not sure about your intent, but will the following code serve your purpose?
I used fusion
all over instead of mpl
.
struct DoSomething {
template< class U >
void operator()( U u ) const {
std::cout << u << '\n'; // an example
}
};
using namespace boost::fusion; // Sorry, for brevity
iterator_range<
result_of::advance_c< result_of::begin< MyTuple >::type, 1 >::type
, result_of::advance_c< result_of::begin< MyTuple >::type, 3 >::type
> ir( advance_c< 1 >( begin( tuple_ ) )
, advance_c< 3 >( begin( tuple_ ) ) );
for_each( ir, DoSomething() );
Hope this helps
这篇关于怎样遍历一个元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文