Nochmal: Sortieren mit map funktioniert nicht.



  • Hallo,

    also ich verwende nun STL Port, das ändert aber leider nichts daran, dass die sortierten Map-Elemente nicht in der richtigen Reihenfolge steht.
    Also nochmal:

    struct compLen
    {
    	bool operator()(const std::string& a, const std::string& b) const 
    	{
    		return a.length() > b.length();
    	}
    };
    
    typedef std::map<std::string, std::string, compLen> varMap;
    

    So ist das ganze definiert.
    Aber wenn ich iteriere, dann ist das erste Element in der map auch immer das Element, das als Erstes eingefügt worden ist und nicht das Längste oder Kürzeste.
    Einfügen tu ich simplerweise durch den []-Operator.

    MfG MAV



  • Folgendes Programm funktioniert bei mir ohne Probleme:

    #include <string>
    #include <iostream>
    #include <map>
    
    using namespace std;
    
    struct compLen 
    { 
        bool operator()(const std::string& a, const std::string& b) const  
        { 
            return a.length() > b.length(); 
        } 
    }; 
    
    typedef std::map<std::string, std::string, compLen> varMap;
    
    int main()
    {
    	varMap MyMap;
    
    	MyMap["mittel"] = "jo";
    	MyMap["langeswort"] = "dumdidumm";
    	MyMap["k"] = "blubso" ;
    
    	for(varMap::iterator i = MyMap.begin(); i != MyMap.end(); ++i)
    		cout << '[' << i->first  << "]:" << i->second << endl;
    
    	cin.get();
    }
    


  • Exakt, bei mir auch. oO

    Hm, sehr seltsam, werde wohl einiges testen...


Anmelden zum Antworten