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