|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
kasthurirangan.balaji@gmail.com 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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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ée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |
|
![]() |
| Outils de la discussion | |
|
|