C++ Problem resolution - is it the best way to simulate a "tuple"?
I've got the following problem:
"Write a template function vectorMAXMIN() that will accept a vector and a number indicating the size of the vector and will return the max and the min values of the vector"...
So i think in it... Create a class vector to avoid the "size" passing value and control the insertions and can get from this the max and min values... ( dunno if it's a good idea )
The problem is "how to return a tuple?" When i read the problem, i thought in a tuple to return "max, min values" is it correct?
The code:
#include <iostream>
template < typename T >
class _tuple
{
public:
T _Max;
T _Min;
};
template < typename T >
class _vector
{
public:
_vector( int cnt = 0);
~_vector();
_tuple< T > get_tuple( void );
void insert( const T );
private:
T *ptr;
int cnt;
int MAX;
};
template < typename T >
_vector< T >::_vector( int N )
{
ptr = new T [N] ;
MAX = N;
cnt = 0;
}
template < typename T >
_tuple<T> _vector< T >::get_tuple( void )
{
_tuple< T > _mytuple;
_mytuple._Max = ptr[0];
_mytuple._Min = ptr[0];
for( int i = 1; i < cnt; i++)
{
if( _mytuple._Max > ptr[i] )
_mytuple._Max = ptr[i];
if( _mytuple._Min < ptr[i] )
_mytuple._Min = ptr[i]开发者_Python百科;
}
return _mytuple;
}
template < typename T >
void _vector< T >::insert( const T element)
{
if( cnt == MAX )
std::cerr << "Error: Out of range!" << std::endl;
else
{
ptr[cnt] = element;
cnt++;
}
}
template < typename T >
_vector< T >::~_vector()
{
delete [] ptr;
}
int main()
{
_vector< int > v;
_tuple < int > t;
v.insert(2);
v.insert(1);
v.insert(5);
v.insert(0);
v.insert(4);
t = v.get_tuple();
std::cout << "MAX:" << t._Max;
std::cout << " MIN:" << t._Min;
return 0;
}
I would use a std::pair
to return the minimum and maximum values.
Also, if you're doing this in C++, you might want to use the std::vector
implementation instead of a homegrown one and "just" scan the passed-in std::vector
once instead of implementing your own vector. Though, the way the question reads, you're probably expected to pass in an array and the array size instead.
Simulation of tuples is not necessary in general with recent compilers, and at all for your particular use case. For your use case -- a tuple of two values -- std::pair
is perfectly suitable (as James McNellis mentioned). For tuples of two or more values, TR1 provides std::tr1::tuple
and C++0x provides std::tuple
.
EDIT: For older compilers lacking TR1 or C++0x support, of course there is boost to come to the rescue -- see in particular boost.tuple and boost.fusion.
Or just use std::pair.
@Timo Geusch... i was reading a bit more and i code it... do you think is it correct?:
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
int main()
{
int a[] = {1,6,5,-1};
vector< int > v( a, a+4 );
pair < int, int > _tuple( *v.begin(), *v.begin() ); // < MAX, MIN >
for( vector< int >::iterator it = v.begin() + 1; it != v.end(); it++ )
{
if( _tuple.first > *it )
_tuple.first = *it;
if( _tuple.second < *it )
_tuple.second = *it;
}
cout << "MAX:" << _tuple.first << endl;
cout << "MIN:" << _tuple.second << endl;
return 0;
}
精彩评论