vendredi 29 juillet 2016

std::set find behavior with char * type

I have below code line-

const char *values[] = { "I", "We", "You", "We"};
std::set<const char*> setValues;

for( int i = 0; i < 3; i++ ) {

    const char *val = values[i];
    std::set<const char*>::iterator it = setValues.find( val );

    if( it == setValues.end() ) {
        setValues.insert( val );
    }
    else {
        cout << "Existing value" << endl;
    }
}

With this I am trying to inser non-repeated values in a set, but somehow code is not hitting to print for existing element and duplicate value is getting inserted.

what is wrong here?

Aucun commentaire:

Enregistrer un commentaire