struct toto_t
{
int a;
int b;
fiber_t( int A, int B ) : a( A ), b( B )
{
}
bool operator<( const toto_t & T ) const
{
return this->a < T.a;
}
};
je l'utilise ainsi :
std::vector< toto_t > totos;
// remplissage de totos
std::sort( totos.begin(), totos.end() );
Ca marche, pour trier selon a (normal).
J'aimerais maintenant pouvoir trier selon a ou b.
Quelle est la meilleur façon de faire ?
Merci pour votre aide.
:~/prog/test$ g++ test.cpp -o test && ./test Tri selon A a = 1, b = 4 a = 2, b = 3 a = 3, b = 2 a = 4, b = 1 Tri selon B a = 4, b = 1 a = 3, b = 2 a = 2, b = 3 a = 1, b = 4
A+
Christophe
Aurélien REGAT-BARREL wrote:
Bonjour,
Bonjour,
J'ai une struct de ce style :
[snip]
J'aimerais maintenant pouvoir trier selon a ou b.
Quelle est la meilleur façon de faire ?
Fournir à std::sort une fonction (ou un foncteur) de tri.
cdevienne@asticot:~/prog/test$ g++ test.cpp -o test && ./test
Tri selon A
a = 1, b = 4
a = 2, b = 3
a = 3, b = 2
a = 4, b = 1
Tri selon B
a = 4, b = 1
a = 3, b = 2
a = 2, b = 3
a = 1, b = 4
:~/prog/test$ g++ test.cpp -o test && ./test Tri selon A a = 1, b = 4 a = 2, b = 3 a = 3, b = 2 a = 4, b = 1 Tri selon B a = 4, b = 1 a = 3, b = 2 a = 2, b = 3 a = 1, b = 4
A+
Christophe
Christophe de VIENNE
Petite rectification pour un peu plus de propreté :
Christophe de VIENNE wrote: [snip]
std::ostream & operator<<(std::ostream & o, toto_t const & t) { o << "a = " << t.a << ", b = " << t.b << std::endl; devient: