std::string als Schlüssel in std::map mit einem Vergleichsoperator

Ich versuche, einen std::string als Schlüssel in eine std::map allerdings bin ich nicht in der Lage zu finden() richtig. Mein code ist etwas kompliziert und groß, so das ist ein kleines Programm, das zeigt ein problem, das ich habe. Wenn jemand mir sagen könnte warum das nicht funktioniert, wäre ich sehr dankbar.

Dank.

#include <stdio.h>
#include <string>
#include <map>

struct comparer
{
    public:
    bool operator()(const std::string x, const std::string y)
    {
         return x.compare(y)==0;
    }
};

int main(int argc, char *argv[])
{
    std::map<std::string, int, comparer> numbers;
    numbers.insert(std::pair<std::string,int>("One",1));
    numbers.insert(std::pair<std::string,int>("Two",2));
    numbers.insert(std::pair<std::string,int>("Three",3));
    numbers.insert(std::pair<std::string,int>("Four",4));
    numbers.insert(std::pair<std::string,int>("Five",5));


    std::map<std::string, int, comparer>::iterator it=numbers.find("Three");
    if(it!=numbers.end())
        printf("The number is %d\n",(*it).second);
    else
        printf("Error, the number is not found\n");
}

InformationsquelleAutor Chromex | 2011-12-04

Schreibe einen Kommentar