通过传递输出迭代器从函数填充std :: [容器] [英] populating an std::[container] from a function by passing an output iterator
本文介绍了通过传递输出迭代器从函数填充std :: [容器]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过传递输出迭代器从函数内部填充容器,因为这是我理解的最有效的方法。例如。
I want to populate a container from inside a function by passing an output iterator as this is the most efficient way to do it as I understand. e.g.
template <typename OutputIterator>
void getInts(OutputIterator it)
{
for (int i = 0; i < 5; ++i)
*it++ = i;
}
但是如何强制执行该类型,迭代器应该指向?基本上我想说这个函数采用boost :: tuple类型的输出迭代器。
But how can I enforce the type, the iterator should point to ? Basically I want to say "this function takes an output iterator of type boost::tuple" .
推荐答案
你可以使用 boost :: enable_if 与 std:iterator_traits :
#include <boost/type_traits/is_same.hpp>
#include <boost/utility/enable_if.hpp>
template <typename OutputIterator>
typename boost::enable_if<
boost::is_same<
int, /* replace by your type here */
typename std::iterator_traits<OutputIterator>::value_type
>
>::type getInts(OutputIterator it)
{
for (int i = 0; i < 5; ++i)
*it++ = i;
}
这篇关于通过传递输出迭代器从函数填充std :: [容器]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文