如何编写一个以通用方式接受迭代器或集合的函数? [英] How to write a function that takes an iterator or collection in a generic way?
问题描述
我是一个Java程序员,几乎专门为过去8年左右,最近我一直在玩C ++。这里有一个问题,我反对关于C + + STL和Java中的迭代器。
I've been a Java programmer almost exclusively for the past 8 years or so, and recently I've been playing with C++ again. Here's an issue that I've come up against with regards to iterators in C++ STL and Java.
在Java中,你可以编写一个方法,
In Java, you can write a method that takes an iterator like this:
void someMethod(Iterator<String> data) {
// ...
}
您传入迭代器
需要知道迭代器的底层集合是什么,这是好的。
You pass in an Iterator
and the method does not need to know what the underlying collection of that iterator is, which is good.
在C ++中,没有公共的基类为迭代器(据我所知)。我必须写一个这样的函数:
In C++, there is no common base class for iterators (as far as I know). I'd have to write a function like this:
void some_function(std::vector<std::string>::const_iterator data) {
// ...
}
, some_function
知道迭代器是向量
上的迭代器。这不好,因为我想让函数工作,而不管迭代器的底层集合是什么。
In other words, some_function
knows that the iterator is an iterator over a vector
. That's not good, because I want the function to work regardless of what the underlying collection of the iterator is.
我如何在C ++中做这个?如果它不是真的可能,那么什么是最好的方法来创建一个函数在一个集合作为参数,但不需要知道什么确切的集合是什么样的C ++?
How can I do this in C++? If it isn't really possible, then what is the best way to create a function in C++ that takes a collection as a parameter, but that doesn't need to know what the exact kind of collection is?
附录
感谢您的回答。除了答案,我在这本书的第7.5段(Iterator Traits)中找到了一些很好的信息: C ++标准库:教程和参考(Nicolai M. Josuttis)。第7.5.1节解释了如何为不同的迭代器类别编写特殊版本的函数。
Thanks for the answers. In addition to the answers I found some good information on this in paragraph 7.5 (Iterator Traits) of the book The C++ Standard Library: A Tutorial and Reference (by Nicolai M. Josuttis). Paragraph 7.5.1 explains how to write specialized versions of functions for different iterator categories.
推荐答案
迭代器的类型以及迭代器需要拥有的属性的种类。以下是迭代器的一些常见命名约定:
Its best to indicate through a naming convention the kind of iterator and subsequently the kind of properties the iterator is required to posses. Below are some common naming conventions for iterators:
template<typename Iterator>
void foo_iterator(Iterator begin, Iterator end)
{
typedef typename std::iterator_traits<Iterator>::value_type T;
....
}
template<typename RandomIterator>
void foo_random_iterator(RandomIterator begin, RandomIterator end)
{
typedef typename std::iterator_traits<RandomIterator>::value_type T;
....
}
template<typename ForwardIterator>
void foo_forward_iterator(ForwardIterator begin, ForwardIterator end)
{
typedef typename std::iterator_traits<ForwardIterator>::value_type T;
....
}
template<typename ReverseIterator>
void foo_forward_iterator(ReverseIterator begin, ReverseIterator end)
{
typedef typename std::iterator_traits<ReverseIterator>::value_type T;
....
}
template<typename InputIterator>
void foo_input_iterator(InputIterator begin, InputIterator end)
{
typedef typename std::iterator_traits<InputIterator>::value_type T;
....
}
template<typename OutputIterator>
void foo_output_iterator(OutputIterator out)
{
// We don't have a type T, as we can't "always"
// know the type, as this type of iterator is a sink.
....
}
下面是序列类型容器的通用定义,包括向量和deque 。
Below is a generic definition for sequence type containers, which include vector and deque.
template <typename T,
class Allocator,
template <class,class> class Sequence>
inline void foo_sequence(Sequence<T,Allocator>& sequence)
{
....
}
这篇关于如何编写一个以通用方式接受迭代器或集合的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!