In the function, the parameter is defined as a constant reference.
elemType& min (const std::vector<elemType> &vec) { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Therefore, functions such as begin and end return an iterator of type const_iterator . Correct
typename std::vector<elemType>::const_iterator select = vec.begin(), it = next(select), end_it = vec.end();
You should also include the title.
#include <iterator>
since the program uses the function std::next , declared in this header.
Also keep in mind that the min function is incorrect, since in the general case the transmitted vector may be empty. And this means that the function has a call to a non-existing element of the vector and, accordingly, a dereferencing of an invalid iterator.
There is a standard algorithm std::min_element , declared in the header <algorithm> , which performs the task.
Your function can be determined by analogy with this algorithm. For example,
#include <iostream> #include <vector> template <class elemType> auto min( const std::vector<elemType> &vec ) { auto select = vec.begin(); if ( select != vec.end() ) { for ( auto it = vec.begin(), end_it = vec.end(); ++it != end_it; ) { if ( *it < *select ) select = it; } } return select; } int main() { int massiv[] = { 35, 66, 98, 15, 32, 41, 24, 90, 55, 100 }; const size_t N = sizeof( massiv ) / sizeof( *massiv ); std::vector<int> vec_train( massiv, massiv + N ); auto it = min( vec_train ); if ( it != vec_train.end() ) { std::cout << "Minimum is " << *it << std::endl; } return 0; }
Outputting the program to the console
Minimum is 15
If your compiler does not support the auto type specifier, the function can be defined as
template <class elemType> typename std::vector<elemType>::const_iterator min( const std::vector<elemType> &vec ) { typedef typename std::vector<elemType>::const_iterator const_iterator; const_iterator select = vec.begin(); if ( select != vec.end() ) { for ( const_iterator it = vec.begin(), end_it = vec.end(); ++it != end_it; ) { if ( *it < *select ) select = it; } } return select; }