我想通过传递输出迭代器从函数内部填充容器,因为据我所知,这是最有效的方法。例如
template <typename OutputIterator>
void getInts(OutputIterator it)
{
for (int i = 0; i < 5; ++i)
*it++ = i;
}
( Is returning a std::list costly? )
但是我如何强制迭代器应该指向的类型呢?基本上我想说“这个函数接受一个类型为 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;
}
关于c++ - 通过传递输出迭代器从函数填充 std::[container],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4537581/