lundi 1 juillet 2019

In C++ string, why after the last character, behavior is different when accessed by index and at()?

string ss("test");
cout << ss[sizeof(ss)] ;

Output is null , program terminates normally . I think this is the right behavior but when run this program with at()

string ss("test");
cout << ss.at(sizeof(ss)) ;

throws an exception . My question is , shouldn't both behavior be the same either both( accessing by index and at()) give abnormal termination or exit normally?

Aucun commentaire:

Enregistrer un commentaire