简体   繁体   English

在C ++中,如何创建一个接受InputIterator的函数?

[英]In C++, how do I create a function that accepts an InputIterator?

C++ transform , accumulate , and similar functions accept an InputIterator as an argument, so they can be used with many types of containers. C ++ transformaccumulate和类似函数接受InputIterator作为参数,因此它们可以与许多类型的容器一起使用。 I want to write a function that accepts InputIterator so I can use the function with any type of container. 我想编写一个接受InputIterator的函数,以便可以将该函数与任何类型的容器一起使用。 How do I do that? 我怎么做? What headers do I need, etc. 我需要什么标题,等等。

You don't need any headers. 您不需要任何标题。 You just make a function template, and document your template to require an argument that is an input iterator. 您只需制作一个功能模板,并记录您的模板以要求使用作为输入迭代器的参数。 You may like to use the iterator_traits template from the <iterator> header to extract adherent data, though. 但是,您可能想使用<iterator>头中的iterator_traits模板来提取粘附数据。 For example: 例如:

#include <iterator>

//  Requires: InputIterator is an input iterator
//  Returns: sum of the range, starting at acc

template <typename InputIterator>
typename std::iterator_traits<InputIterator>::value_type
sum(InputIterator it,
    InputIterator last,
    typename std::iterator_traits<InputIterator>::value_type acc)
{
    while (it != last) { acc += *it; ++it; }
    return acc;
}

For some algorithms you don't need traits at all, and therefore no headers. 对于某些算法,您根本不需要特征,因此不需要标题。 For example: 例如:

// Requires: Iter is an input iterator, F is a callable and copyable
// Returns: a copy of f
// Effects: calls f with every value in the range
template <typename Iter, typename F>
F for_each(Iter it, Iter last, F f)
{
    while (it != last) { f(*it); ++it; }
    return f;
}

InputIterator is a concept more than a type. InputIterator一个概念,更是一种概念。

Just write 写吧

template<typename InputIterator>
void myFunc(InputIterator begin, InputIterator end) {
    /* … */
}

and you are good to go, if you apply on the begin and end variables only actions that correspond to the InputIterator concept (incrementation, dereference…). 如果您将beginend变量仅应用到与InputIterator概念相对应的InputIterator (增量,取消引用…),那么您就很好了。

I'd suspect you already did have a look at eg std::transform() documentation yet. 我怀疑您已经看过例如std::transform()文档了。

You should have noticed that they're taking these iterator types as template parameters: 您应该已经注意到,他们将这些迭代器类型用作模板参数:

template< class InputIt, class OutputIt, class UnaryOperation >
OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first,
                    UnaryOperation unary_op );

The particular requirements are checked at compile time, using the various concepts defined in the standard language, and being defined as eg std::iterator_traits<It> . 特定要求在编译时使用标准语言中定义的各种概念进行检查,并定义为例如std::iterator_traits<It>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在 c++ 中实现向量的迭代器插入(const_iterator position,首先是 InputIterator,最后是 InputIterator)? - How can i Implement iterator insert (const_iterator position, InputIterator first, InputIterator last) of vector in c++? 如何创建接受具有不同签名的函数指针的 C++ 类? - How can I create a C++ class that accepts function pointers with different signatures? C ++:我如何创建一个接受连接字符串作为参数的函数? - C++: How can i create a function that accepts concatenated strings as parameter? 如何为接受 `std::function &amp;&amp;` 的函数创建绑定 (Emscripten)? - How do I create a binding (Emscripten) for a function that accepts a `std::function &&`? C ++:如何编写一个接受迭代器并插入元素的函数? - C++: How do I write a function that accepts an iterator and inserts elements? 我如何创建一个接受整数数组及其大小并返回 true 的 c++ 函数 isOdd 是所有数组元素都是奇数 - How could I create a c++ function isOdd that accepts an integer array and its size and returns true is all of the array elements are odd 如何创建对象函数指针的 C++ 映射? - How do I create C++ map of object function pointers? 如何在C ++中创建一个生成3个骰子掷骰的函数 - How do i create a function that generates 3 dice rolls in C++ 如何创建一个接受可变数量的int的C ++构造函数 - How can I create a C++ constructor that accepts a variable number of int's c ++编译错误“要求&#39;_InputIterator std :: __ find_if(_InputIterator,_InputIterator,_Predicate,std :: input_iterator_tag)” - c++ compile error “required from '_InputIterator std::__find_if(_InputIterator, _InputIterator, _Predicate, std::input_iterator_tag) ”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM