# does STL vector has a function to sort?

This is just an example.
I have a vector of apples. An apple has 2 integers(int a, int b). Is there a STL library to sort the vector automatically by specified member(int a)..
thanks
###### Who is Participating?

Commented:
#include <iostream>
#include <vector>

using std::vector;
using std::ostream;

class A
{
public:
A(int a, int b, int c) : _a(a), _b(b), _c(c)
{}
bool operator<(const A& a) const{ return _b < a._b;}
void print() {std::cout << _a << ", " << _b << ", " <<  _c << std::endl;}
private:
int _a;
int _b;
int _c;
};

int main(int argc, char * argv[])
{

A a1(1,2,3);
A a2(2,3,1);
A a3(8,1,9);

vector<A> v;

v.push_back(a1);
v.push_back(a2);
v.push_back(a3);

sort(v.begin(), v.end());
v[0].print();
v[1].print();
v[2].print();

}
0

Commented:
you can use

there is
1.  sort()
2.  stable_sort ()
3. partial_sort()

and you may have to write a function to compare two vectors.

check :  http://www.sgi.com/tech/stl/sort.html

/abhijit/
0

Commented:
You have algorithms

#include <algorithm>

There you have template versions of sort supporting stl containers.

0

Commented:
template<class RandomAccessIterator>
void sort(
RandomAccessIterator _First,
RandomAccessIterator _Last
);
template<class RandomAccessIterator, class Pr>
void sort(
RandomAccessIterator _First,
RandomAccessIterator _Last,
BinaryPredicate _Comp
);
0

Commented:
You may use template sort functions like this:

void main()
{
vector<string> words;
string word;
ifstream ifs("xx.txt");

while (!ifs.eof())
{
ifs >> word;
if (ifs.fail())
break;
words.push_back(word);
}

sort(words.begin(), words.end());
}

Note, while there is a std::list::sort() member function, sort() isn't a member of std::vector.

Regards, Alex
0

Author Commented:
But how these examples fit with my example because my classs Apple has 2 members-id and quality and i want to sort the vector only by id?
0

Commented:
output is ofcourse

8,1,9
1,2,3
2,3,1

And this becaus the operator< compares the _b member
0

Author Commented:
Hi  Mafalda,
There was an error in the program when i compiled. I assumed that i have to use #include <algorithm> but it didnt work eaither. I am using VS c++. Below is the error message.

error C2065: 'sort' : undeclared identifier
warning C4508: 'main' : function should return a value; 'void' return type assumed
0

Commented:
you should have

using namespace std;

somewhere at top. Otherwise you have to use std::sort(..) instead of sort(..)

Regards, Alex
0

Commented:
Yes sorry,

for main

#include <algorithm>
using std::sort;

and omit
using std::ostream;
as it is not used

well you can always use once

using namespace std;

and then you do not need to specify each class used like:

using std::cout;
using std::endl;
using std::sort;

0

Commented:
You can also use the second version of the sort() command (the one with three parameters). The advantage of this is that your class does not have to define the < operator:

class CompareApples
{
public: // operator must be public
bool operator()(const apple arg1, const apple arg2) const
{ return arg1._b < arg2._b; }
};

Then you call the sort function like this:
sort(appleVector.begin(), appleVector.end(), CompareApples);
0

Author Commented:
it compiles. So basically this program orders the vector by _b  ?
0

Author Commented:
bool operator<(const A& a) const{ return _b < a._b;}

bool operator<(const A& a) const{ return _a< a._a;}

bool operator<(const A& a) const{ return _c < a._c;}

in this way i can choose any member..
0

Commented:
You can have only one ... but you can have a condition in the function to decide which comparisson to do

In example (remember this is only an example !!!)

class A
{
...
void set_sort(int sorted_by) { _sort_by = sorted_by;}
bool operator<(const A& a) const{ if (_sort_by == 1)
return _b < a._b;
else
return _c < a._c;}

...

private:
int _sort_by;
...

}
0

Commented:
By the way,  you can have a different operator< for each class you have !
For each type (class) you will decide how to sort
0

Commented:
class Orange : public Fruit
{
...
bool operator<(const Orange& a) const{ return _b < a._b;}
...
}

class Apple : public Fruit
{
...
bool operator<(const Apple& a) const{ return _a< a._a;}
...
}
0

Commented:
If you want to sort your vector in different ways, use something like this:

class CompareApples_a
{
public: // operator must be public
bool operator()(const apple arg1, const apple arg2) const
{ return arg1._a < arg2._a; }
};

class CompareApples_b
{
public: // operator must be public
bool operator()(const apple arg1, const apple arg2) const
{ return arg1._b < arg2._b; }
};

sort(appleVector.begin(), appleVector.end(), CompareApples_a);
sort(appleVector.begin(), appleVector.end(), CompareApples_b);
0

Author Commented:
Mafalda  thanks, i think that was enough information for me. I am going to try to implement it on my own program. Is there any way i can reach you in future in case i have any additional problems?
0

Author Commented:
khkremer  thank you as well..
0

Commented:
You're welcome.
0

Commented:
I would of course appreciate if you accept my answer ... ;o)

You can always post here any aditional questions as I am still "hooked" to it
I will be happy to help "free of charge" ;o)

Cheers,
Mafalda
0

Commented:
For new issues it would be better to new questions so other experts can help you as well and benfit the points reward
0

Author Commented:
Ok, i split the points in way i found each answer most helpful for me..
0

Author Commented:
just one last question, is it possible to ask question through e-mail ?
0

Commented:
It is not advisable for several reasons, the main one in my opinion is that others can not benefit from the correspondance.
It is explained somewhere in the EE policies documents
0

Commented:
My comment about the points reward didn't sound like I wanted it to sound ... I am not a points freak ... it was a general comment.
Splitting the points was a good idea ;o)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.