📄 howtos.sgml
字号:
<!-- ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| section -->
<section id="howtos">
<title>How-to's</>
<!-- ||||||||||||||||||||||||||||| subsection -->
<section id="howtos.search">
<title>How to find something in a sequence?</>
<para>
If you just want to make an enquiry if a sequence contains a type with certain properties, you can do it like that:
</>
<programlisting>
<![CDATA[
typedef mpl::list5<> types;
typedef mpl::contains<types,int>::type res; // res::value == true
]]>
</>
<para>
A predicate version of <literal>contains</> algorithm is spelled <literal>any</>:
</>
<programlisting>
<![CDATA[
// find if any type in 'types' is derived from 'my'
typedef mpl::any<types, mpl::is_convertible<_1,my> >::type res;
]]>
</>
<para>
Now, if you really want to find type in a sequence - that is, to obtain an iterator on its position, when the should-be-known-from-STL <literal>find</> and <literal>find_if</> algorithms are most probably exactly what you need:
</>
<programlisting>
<![CDATA[
// find if any type in 'types' is derived from 'my'
typedef mpl::find<types, int >::type iterator;
typedef mpl::find_if<types, mpl::is_convertible<_1,my> >::type iterator;
]]>
</>
<para>
Unless, of course, the sequence is sorted. If it is, then the other known names come into the play - <literal>lower_bound</>, <literal>upper_bound</>, or <literal>binary_search</>:
</>
<programlisting>
<![CDATA[
// find if any type in 'types' is derived from 'my'
typedef mpl::lower_bound<types, mpl::int_c<5>, mpl::less<_1,_2> >::type iterator;
typedef mpl::upper_bound<types, mpl::int_c<5>, mpl::less<_1,_2> >::type iterator;
typedef mpl::binary_search<types, mpl::int_c<5>, mpl::less<_1,_2> >::type iterator;
]]>
</>
</section>
</section>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -