How to write a function that accepts an iterator or collection in general?

I have been a Java programmer almost exclusively for the last 8 years or so, and recently I played again with C ++. Here's the problem I ran into iterators in C ++ STL and Java.

In Java, you can write a method that accepts an iterator as follows:

void someMethod(Iterator<String> data) {
    // ...
}

You pass Iterator, and the method should not know what the basic collection of this iterator is, which is good.

In C ++, there is no common base class for iterators (as far as I know). I need to write a function like this:

void some_function(std::vector<std::string>::const_iterator data) {
    // ...
}

In other words, some_functionknows that an iterator is an iterator over vector. This is not very good, because I want the function to work no matter what the basic iterator collection is.

++? , ++, , , ?

. 7.5 ( " " ) ++: ( . ). 7.5.1 , .

+2
4

, , . :

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.

template <typename T,
          class Allocator,
          template <class,class> class Sequence>
inline void foo_sequence(Sequence<T,Allocator>& sequence)
{
   ....
}
+2

, . , std <algorithm> , std::for_each.

.

template< class Iterator >
void some_function( Iterator first, Iterator last )
{
    // ...
}

, , .

.

std::vector< double > my_doubles;
// ... populate doubles
some_function( my_doubles.begin(), my_doubles.end() );


std::set< Custom > my_custom_class_set;
// ... populate ...
some_function( my_custom_class_set.begin(), my_custom_class_set.end() );

int raw_array[50];
// ... populate ...
some_function( raw_array, raw_array + 50 );
+6

++ Java. Java ( ). ++ . , - / . . , , , ( , ). , , . .

():

template<typename Iter>
typename std::iterator_traits<Iter>::value_type
sum(Iter begin, Iter end) {
   typedef typename std::iterator_traits<Iter>::value_type vt;
   vt accum = vt();
   while (begin!=end) {
      accum += *begin;
      ++begin;
   }
   return accum;
}

"" - . . , , ( , ), ( ). , , . (.. ForwardIterator) .

, "" "", . / , , . . , . , boost:: any boost:: function.

, (. " " ) (. "SFINAE" ). , googling ++, erasure, iterator. , ( ). , "" ().

+2

, .

, :

template<typename T>
void some_function(std::forward_iterator<T> data) {
   ...
}

for something that requires the ability to move the iterator forward (++) through the collection.

-2
source

All Articles