problem in stl

V

vicky

i declares a vector and uses its sort function, acc. to a tutorial.
vector<int> c;
c.sort(c.begin(),c.end());

but compiler gives error:
22 C:\Dev-Cpp\ch72.cpp 'class std::vector<int, std::allocator<int> >'
has no member named 'sort'

i m using devcpp 4.9.something, what is the problem???
 
B

Bas

vicky said:
i declares a vector and uses its sort function, acc. to a tutorial.
vector<int> c;
c.sort(c.begin(),c.end());

but compiler gives error:
22 C:\Dev-Cpp\ch72.cpp 'class std::vector<int, std::allocator<int> >'
has no member named 'sort'

i m using devcpp 4.9.something, what is the problem???

std::vector has no sort member function.
You should use std::sort from <algorithm>:

#include <algorithm>

int main(int argc, char* argv[])
{
std::vector<int> c;

std::sort(c.begin(),c.end());

return 0;
}

Bas from Holland
 
A

André Schreiter

Am 15.02.2010 10:18, schrieb vicky:
i declares a vector and uses its sort function, acc. to a tutorial.

std::vector hasn't a sort function. Use the header <algorithm>:

#include <algorithm>
#include <vector>

....
std::vector<int> c;
....
std::sort(c.begin(), c.end());
....
 
V

vicky

Am 15.02.2010 10:18, schrieb vicky:


std::vector hasn't a sort function. Use the header <algorithm>:

#include <algorithm>
#include <vector>

...
std::vector<int> c;
...
std::sort(c.begin(), c.end());
...

but i m reading a book of "Nicolai M. Josuttis". it is highly
recommended one.He has used it many a times.
Is it like it was before and now removed.?
 
A

Alf P. Steinbach

* vicky:
but i m reading a book of "Nicolai M. Josuttis". it is highly
recommended one.He has used it many a times.
Is it like it was before and now removed.?

I think you may have seen use of

list<int> someList;
someList.sort()

but not vector.

It may not appear to be very unified, but the unification is via the std::sort
routine.

The headers for types like vector and sort provide overloads of std::sort where
appropriate. For a type where there's no such override std::sort uses a general
algorithm that requires random access, like indexing of a vector. But that
doesn't work for std::list, so std::list has a sort() member function
(presumably it uses merge sorting or some such that's better suited for linked
lists), and overrides std::sort to call that member function.


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* vicky:

I think you may have seen use of

list<int> someList;
someList.sort()

but not vector.

It may not appear to be very unified, but the unification is via the
std::sort routine.

The headers for types like vector and sort provide overloads of
std::sort where appropriate. For a type where there's no such override
std::sort uses a general algorithm that requires random access, like
indexing of a vector. But that doesn't work for std::list, so std::list
has a sort() member function (presumably it uses merge sorting or some
such that's better suited for linked lists), and overrides std::sort to
call that member function.


Cheers & hth.,

- Alf

Sorry about the brain-to-keyboard errors. I meant to write headers for types
like vector and list, and at the end there I meant to write overload not override.

But anyway...


Cheers,

- Alf
 
A

Alf P. Steinbach

* Pete Becker:
It doesn't.

Sorry for the confusion in that article (note the brain-to-keyboard errors also,
which I had to post follow-up to correct).

I was confusing sort with swap.

Or in other words, the whole article was mostly wrong, rubbish.

---

It leads to an interesting practical question though, how to write

template< T >
void sort_any_container( T& c );

without doing those overloads.



Cheers & thanks,

- Alf
 
M

Michael Doubez

* Pete Becker:



Sorry for the confusion in that article (note the brain-to-keyboard errors also,
which I had to post follow-up to correct).

I was confusing sort with swap.

Or in other words, the whole article was mostly wrong, rubbish.

It leads to an interesting practical question though, how to write

    template< T >
    void sort_any_container( T& c );

without doing those overloads.

The usual meta-programming tricks:

// predicate to test the presence of member
typedef char (&no_tag)[1];
typedef char (&yes_tag)[2];

template <typename T, void (T::*)()>
struct ptmf_helper {};

template<typename T>
no_tag has_member_sort_helper(...);

template<typename T>
yes_tag has_member_sort_helper(ptmf_helper<T, &T::sort>* p);

// helper

template< typename T
, bool has_sort
= sizeof(has_member_sort_helper said:
struct type_has_sort{};

// if has sort member
template< class T >
void sort_any_container_helper(T& c , const type_has_sort<T,true>&)
{
c.sort();
}

// otherwise rely on algorithm
template< class T >
void sort_any_container_helper(T& c , const type_has_sort<T,false>&)
{
std::sort(c.begin(), c.end());
}

// main template

template< class T >
void sort_any_container( T& c )
{
sort_any_container_helper(c,type_has_sort<T>());
}

main()
{
using namespace std;
vector<int> vec;
list <int> lis;

sort_any_container(vec);
sort_any_container(lis);
}
 
B

Bas

this is a nice one.
I can use that too!

Thanks,
Bas from Holland

* Pete Becker:



Sorry for the confusion in that article (note the brain-to-keyboard errors
also,
which I had to post follow-up to correct).

I was confusing sort with swap.

Or in other words, the whole article was mostly wrong, rubbish.

It leads to an interesting practical question though, how to write

template< T >
void sort_any_container( T& c );

without doing those overloads.

The usual meta-programming tricks:

// predicate to test the presence of member
typedef char (&no_tag)[1];
typedef char (&yes_tag)[2];

template <typename T, void (T::*)()>
struct ptmf_helper {};

template<typename T>
no_tag has_member_sort_helper(...);

template<typename T>
yes_tag has_member_sort_helper(ptmf_helper<T, &T::sort>* p);

// helper

template< typename T
, bool has_sort
= sizeof(has_member_sort_helper said:
struct type_has_sort{};

// if has sort member
template< class T >
void sort_any_container_helper(T& c , const type_has_sort<T,true>&)
{
c.sort();
}

// otherwise rely on algorithm
template< class T >
void sort_any_container_helper(T& c , const type_has_sort<T,false>&)
{
std::sort(c.begin(), c.end());
}

// main template

template< class T >
void sort_any_container( T& c )
{
sort_any_container_helper(c,type_has_sort<T>());
}

main()
{
using namespace std;
vector<int> vec;
list <int> lis;

sort_any_container(vec);
sort_any_container(lis);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,807
Members
48,797
Latest member
PeterSimpson
Top