samedi 18 avril 2015

How does c++ compare two string using ==?

I came across a very simple but confusing problem today.



#include <iostream>
#include <string>
using namespace std;

int main(){
string str = "123";
string a = "1";
string b = "1";

cout << ((str[0]+"") == a) << endl;
cout << (a==str.substr(0,1)) << endl;
cout << (a==b) << endl;

}


The output is: 0 1 1


Why the first compare statement is false? How does c++ compare two string when using == operator?


Aucun commentaire:

Enregistrer un commentaire