jeudi 27 septembre 2018

C++ string index can be negative?

I have a working code in Ideone here that does not return a failure and my expectation was that an index of -1 in std string should crash the program. Is this a valid access index ?

https://ideone.com/gg7P6M

#include <iostream>
using namespace std;

int main() {
    string h = "hiu";
    std::cout << h[h.size()] << ":" << h[-1] << std::endl; 
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire