I try to find a way to find the minimum of an array of numbers different type (int,float,double..)
i have written this

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

template <class T>
T min(vector<T> a())
{
    int i = 0;
    T min = a[0];
    vector<int>::iterator it;
   
    for( it = a.begin(); it!=a.end(); ++it)
    {
        if((*it)<a[i++])
            min = *it;

    }
    return min;
}



int main()
{
    vector<int> b(10);
    for(int i = 0;i<b.size();++i)
        b[i] = i + 2;

	cout<<min(b)<<endl;
	
        return 0;
}

I get this Error:

main.cpp: In function `int main()':

main.cpp:31: error: no matching function for call to `min(std::vector<int, std::allocator<int> >&)'

anyone knows what i'm doing wrong???

Recommended Answers

All 7 Replies

This is wrong :

T min(vector<T> a())

Try to guess which part.

Edit : Hint look at the error

i know parenthesis, i erased it before and again wasn't building , probably
netbeans got confused, with a clean and build worked just fine.
thnx anyway for replying...

now i saw that i have put

vector<int>::iterator it;

and i want

vector<T>::iterator it;

but like this

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

template <class T>
T min(vector<T> a)
{
    int i = 0;
    T min = a[0];
    vector<T>::iterator it;

    for( it = a.begin(); it!=a.end(); ++it)
    {
        if((*it)<a[i++])
            min = *it;

    }
    return min;
}



int main()
{
    vector<int> b(10);
    for(int i = 0;i<b.size();++i)
        b[i] = i + 2;

	cout<<min(b)<<endl;

        return 0;
}

get an error:

main.cpp: In function `T min(std::vector<T, std::allocator<_CharT> >)':

main.cpp:11: error: expected `;' before "it"

main.cpp:13: error: `it' was not declared in this scope

main.cpp: In function `T min(std::vector<T, std::allocator<_CharT> >) [with T = int]':

main.cpp:30:   instantiated from here

main.cpp:11: error: dependent-name ` std::vector<T,std::allocator<_CharT> >::iterator' is parsed as a non-type, but instantiation yields a type

main.cpp:11: note: say `typename  std::vector<T,std::allocator<_CharT> >::iterator' if a type is meant

You have some obvious problems that need correcting:

Problem 1:

template <class T>
T min(vector<T> a)
{

Don't use the name "min". There is already min and max functions in the standard template library, and you shouldn't override these. Use something like vectorMin(), or vMin()

Problem 2:

T min = a[0];

This is no good. The function is named min, so this variable's name needs to change. Perhaps use minimum?

Problem 3:

vector<T>::iterator it;

    for( it = a.begin(); it!=a.end(); ++it)
    {

For the vector container class, iterators are not necessary for looping over the contents. They are important for the list containers (and others) because the members of a std::list are not stored in adjacent memory. For vectors, however, the data is stored just like an array so this:

for( int i=0; i<(int)a.size(); i++ ){
     doSomething( a[i] );
}

is just as fast and much easier to code / read. Using an iterator is not wrong, but it is uneccesary extra work.

Problem 4:

if((*it)<a[i++])
            min = *it;

The index to your maximum value is getting changed every time the if statement executs. So, your maximum will shift, and weird behavior will appear. You should only keep track of the index of the maximum value if you really need it.

I recommend you re-write this in a simpler form such as:

template <class T>
T vMin( const vector<T>& v ){
    T minimum = v[0];
    for( int i=1; i<(int)v.size(); i++ )
        if( v[i] < T )
            T = v[i]
    return minimum;
}

thnx very much, you made some very good observations, and
indeed it works what you last posted, but you have some mistakes, maybe rush.
Right:

template <class T>
T vMin( const vector<T>& v )
{
    T minimum = v[0];

    for( int i=1; i<(int)v.size(); i++ )
    {
        if( v[i] < minimum )
            minimum = v[i];
    }
    return minimum;
}

anyway in the first first post the program runs only for int because i put

vector<int>::iterator it;

but i wanted this

vector<T>::iterator it;

and this caused problems, then you said to handle it as an array and worked but is there a way to handle it with the way i first used?

Also instead of returning the minimum value, consider returning the minimum index, that way the person calling the function also knows where the minimum is as well.

thnx very much, you made some very good observations, and
indeed it works what you last posted, but you have some mistakes, maybe rush.

Yeah, sorry. Didn't notice I was trying to assign to the template type! Whoops.

but i wanted this

vector<T>::iterator it;

and this caused problems, then you said to handle it as an array and worked but is there a way to handle it with the way i first used?

Yes, you can use an iterator for any type in any stl container. Familiarize yourself with the information here: http://www.cplusplus.com/reference/stl/. This page is very helpful.

Also instead of returning the minimum value, consider returning the minimum index, that way the person calling the function also knows where the minimum is as well.

This would not be too difficult of a change:

template <class T>
T& vMin( vector<T>& v, int& m )
{
    m = 0;
    for( int i=1; i<(int)v.size(); i++ )
    {
        if( v[i] < v[m] )
            m = i;
    }
    return v[m];
}

I changed the function to return a reference to the minimum value. So, if you are using this function for large types, there is no copy in or out. The m variable refers to the index of the minimum value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.