Re: How to use the iterator from base class in a derived class?
wangxiaohu wrote:
> I just want to see if I can add a Sort() method to the existing vector
> container.
>
> What you've shown is write a individual function, which does work for
> the purpose, but does not answer my question.
>
It appears you asked out of curiosity. Here is one way to do it:
template < typename T >
class sort_vector : public std::vector< T > {
public:
sort_vector ( void )
: std::vector< T > ()
{}
template < typename A >
sort_vector ( A a )
: std::vector< T > ( a )
{}
template < typename A, typename B >
sort_vector ( A a, B b )
: std::vector< T > ( a, b )
{}
template < typename A, typename B, typename C >
sort_vector ( A a, B b, C c )
: std::vector< T > ( a, b, c )
{}
template < typename A, typename B, typename C, typename D >
sort_vector ( A a, B b, C c, D d )
: std::vector< T > ( a, b, c, d )
{}
void sort ( void ) {
std::sort( this->begin(), this->end() );
}
}; // sort_vector<>
Note the templated constructors. They just forward everything to the
underlying vector. One of those even allows to convert from std::vector<T>
to sort_vector<T>. That mitigates one of the most troublesome drawbacks of
public inheritance from standard containers: surprises when used with
functions like
template < typename T, typename A >
std::vector<T,A> reverse_order ( std::vector<T,A> const & );
Note that sort_vector<T> will match the argument type. But the result will
not be a sort_vector<T>.
> On 10 18 , 1 22 , Kai-Uwe Bux <jkherci...@gmx.net> wrote:
>> wangxiaohu wrote:
and: please don't top post. It is frowned upon around these parts of the
net. See the FAQ for the sentiment about top-posting and other netiquette
matters that most regulars of this group share.
[snip]
Best
Kai-Uwe Bux
|