sorting objects by members

  • Thread starter kasthurirangan.balaji
  • Start date
K

kasthurirangan.balaji

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.
 
B

Barry

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);
}

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.
 
J

James Kanze

(e-mail address removed) wrote:

[...]
[...]
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.
 

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

Forum statistics

Threads
473,780
Messages
2,569,614
Members
45,290
Latest member
JennaNies

Latest Threads

Top