PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > sorting objects by members
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
sorting objects by members

Réponse
 
LinkBack Outils de la discussion
Vieux 16/01/2008, 08h51   #1
kasthurirangan.balaji@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 16/01/2008, 10h47   #2
Barry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sorting objects by members

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
  Réponse avec citation
Vieux 16/01/2008, 19h55   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sorting objects by members

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h03.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12344 seconds with 11 queries