| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
16 Jan 2008 02:51:22 AM |
| Object: |
sorting objects by members |
Hi,
Pls look at the following program
#include <vector>
#include <algorithm>
struct A
{
int age;
int dept;
A(const int& a, const int& b)
:age(a),dept(b) {}
};
main()
{
typedef std::vector<A> ACollection;
ACollection acoll;
acoll.push_back(A(3,3));
acoll.push_back(A(1,3));
acoll.push_back(A(2,1));
acoll.push_back(A(3,1));
acoll.push_back(A(2,3));
acoll.push_back(A(3,2));
acoll.push_back(A(1,2));
//need to know to replace ???
std::transform(acoll.begin(),acoll.end(),std::back_inserter(acoll.begin()), ???);
}
1)I need to sort A.age in ascending order and A.dept in descending
order. I know we can use std::sort, but do not know how to apply it on
two members simultaneously. Do i need to write a custom functor to
handle this??
2)Is back_inserter really needed here since the vector would already
allocated for that number of objects?? Can i use the same container or
need to have a different one?
Thanks,
Balaji.
.
|
|
| User: "Barry" |
|
| Title: Re: sorting objects by members |
16 Jan 2008 04:47:21 AM |
|
|
wrote:
Hi,
Pls look at the following program
#include <vector>
#include <algorithm>
struct A
{
int age;
int dept;
A(const int& a, const int& b)
:age(a),dept(b) {}
};
main()
{
typedef std::vector<A> ACollection;
ACollection acoll;
acoll.push_back(A(3,3));
acoll.push_back(A(1,3));
acoll.push_back(A(2,1));
acoll.push_back(A(3,1));
acoll.push_back(A(2,3));
acoll.push_back(A(3,2));
acoll.push_back(A(1,2));
//need to know to replace ???
std::transform(acoll.begin(),acoll.end(),std::back_inserter(acoll.begin()), ???);
}
1)I need to sort A.age in ascending order and A.dept in descending
order. I know we can use std::sort, but do not know how to apply it on
two members simultaneously. Do i need to write a custom functor to
handle this??
bool operator< (A const& lhs, A const& rhs) {
return lhs.age < rhs.age
|| (lhs.age == rhs.age && lhs.dept > rhs.dept);
}
std::vector<A> V;
std::sort(V.begin(), V.end());
2)Is back_inserter really needed here since the vector would already
allocated for that number of objects?? Can i use the same container or
need to have a different one?
No,
the code will push_back the all the new items into /acoll/.
and the code won't compile at all, since you misuse std::bacK_inserter,
which takes a container as parameter.
--
Thanks
Barry
.
|
|
|
| User: "James Kanze" |
|
| Title: Re: sorting objects by members |
16 Jan 2008 01:55:00 PM |
|
|
On Jan 16, 11:47 am, Barry <dhb2...@gmail.com> wrote:
kasthurirangan.bal...@gmail.com wrote:
[...]
//need to know to replace ???
std::transform(acoll.begin(),acoll.end(),std::back_inserter(acoll.begin(=
)), ???);
}
[...]
2)Is back_inserter really needed here since the vector would already
allocated for that number of objects?? Can i use the same container or
need to have a different one?
No,
the code will push_back the all the new items into /acoll/.
and the code won't compile at all, since you misuse
std::bacK_inserter, which takes a container as parameter.
And if he replaced it with the container, the code will compile,
but will likely core dump on execution, because back_inserter
will invalidate the iterators which transform is using for the
source.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
|
|
|
|
|

|
Related Articles |
|
|