Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (ANSI-Standard) » Kurze Frage zu Stl / vector-sort

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
31.01.2004, 17:51 Uhr
mike
Pinguinhüpfer
(Operator)


Hi!
Ich habe einen vector, welcher ein struct speichert:

C++:
typedef struct{
    std::string artikel;
    std::string bezeichnung;
    float vp;
    int stk;
    unsigned group;
} ARTIKEL;

vector<ARTIKEL> vec;
vector<ARTIKEL>::iterator cur;


Nun möchte ich das Ganze in einem Baum ausgeben. Der Baum besteht aus 2 Ebenen.
+ Gesamt Gruppe 1
|- Artikel (Gruppe 1)
|- Artikel (Gruppe 1)
+Gesamt Gruppe 2
|- Artikel (Gruppe 2)
Da die Items durcheinander eingefügt werden, hab ich mir gedacht, den vector nach der Gruppe zu sortieren. Ich hab gelesen, dass das über den 3ten Parameter von sort geht, indem man den sortier-operator selbst definiert und, im meinem Fall, die Gruppen vergleicht.
Aber wie muss so ein Operator ausschaun?

Danke im Voraus!!!!
mfg
--
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
31.01.2004, 20:50 Uhr
virtual
Sexiest Bit alive
(Operator)


Im Prinzip musst Du dem sort einfach nur eine Funktion als dritten parameter übergeben, die in Deinem Fall folgende Signatur haben sollte:

C++:
bool dein_less(const ARTIKEL& a, const ARTIKEL& b)
{
...
  // Gebe true zurück, wenn a<b
}



Ich habe das mal anhand eines einfachen String arrays gemacht, wobei das alternative Sortierkriterium ist, die Strings rückwärts zu vergleichen:


C++:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>


bool custom_less(const std::string& a, const std::string& b)
{
    std::string::const_reverse_iterator ra = a.rbegin();
    std::string::const_reverse_iterator rb = b.rbegin();

    while (ra!=a.rend() && rb!=b.rend())
    {
        int diff = int(*rb++) - int(*ra++);
        if (diff<0) return false;
        if (diff>0) return true;
    }
    return ra!=a.rend() || rb==b.rend();
}


int main()
{
    std::string array[] = { "mike", "windalf", "0xdeadbeef", "ao", "virtual" };

/* Zur erinnerung, wie es normal geht */
    // Normale Sortierung
    std::sort(array, array+sizeof(array)/sizeof(*array));
    // Ausgabe
    std::copy(array, array+sizeof(array)/sizeof(*array), std::ostream_iterator<std::string>(std::cout, "\n"));
    std::cout<<std::endl;

/* Jetzt wird interessant für mike */
    // Custom sort
    std::sort(array, array+sizeof(array)/sizeof(*array), custom_less);
    // Ausgabe
    std::copy(array, array+sizeof(array)/sizeof(*array), std::ostream_iterator<std::string>(std::cout, "\n"));
}


--
Gruß, virtual
Quote of the Month
Ich eß' nur was ein Gesicht hat (Creme 21)

Dieser Post wurde am 31.01.2004 um 20:51 Uhr von virtual editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
002
01.02.2004, 11:29 Uhr
mike
Pinguinhüpfer
(Operator)


Moin
Vielen Danke für das Bsp.!!! Jetzt funtzts!!!

C++:
typedef struct{
    std::string artikel;
    std::string bezeichnung;
    float vp;
    int stk;
    unsigned group;
} PART;

struct custom_less{
    bool operator()(const PART& a, const PART& b) const
    {
        return a.group < b.group ? true : false;
    }
};

vector<PART> vec;
vector<PART>::iterator cur;
//.....
std::sort(vec.begin(), vec.end(), custom_less());


Ich hab das leider nicht geschafft, "array, array+sizeof(array)/sizeof(*array)" zu kompilieren. Wie muss das bei einem vector ausschaun?

Eine kleine Frage hätte ich noch. Ich habe zuerst versucht less und greater selbst zu "definieren":

C++:
namespace std
{
    struct less<PART&>
    {
        bool operator()(const PART& a, const PART& b) const {
            return a.group < b.group;
        }
    };

    struct greater<PART&>
    {
        bool operator()(const PART& a, const PART& b) const {
            return a.group > b.group;
        }
    };
}


Was stimmt an dem Code nicht? Er behauptet, dass struct less<PART&> ein ungültiger Typ oder so ist.

Danke nochmals
mfg
--

Dieser Post wurde am 01.02.2004 um 11:35 Uhr von mike editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
003
01.02.2004, 13:14 Uhr
virtual
Sexiest Bit alive
(Operator)


Das sollte einige der fragen beantworten:

C++:
#include <algorithm>
#include <vector>
#include <string>

typedef struct{
    std::string artikel;
    std::string bezeichnung;
    float vp;
    int stk;
    unsigned group;
} ARTIKEL;


namespace std
{

template<>
struct std::less<ARTIKEL>
{
    bool operator()(const ARTIKEL& a, const ARTIKEL& b) const {
                return a.group < b.group;
   }
};

}


int main()
{
    std::vector<ARTIKEL> artikel;

    std::sort(artikel.begin(), artikel.end()); // gelcibedeutend mit:
    std::sort(artikel.begin(), artikel.end(), std::less<ARTIKEL>());
}


--
Gruß, virtual
Quote of the Month
Ich eß' nur was ein Gesicht hat (Creme 21)
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
004
01.02.2004, 14:38 Uhr
mike
Pinguinhüpfer
(Operator)


Yepp, jetzt ist alles klar
Danke!!!!!
mfg
--
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ C / C++ (ANSI-Standard) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: