mardi 6 août 2019

C++ negative string index

When I want to print the whole string after editting it by neg index access, I cannot get anything and the program just park still.

And I notice the the param pos in operator [] is the size_t, which means an unsigned integral type.

Here's the code.

int main() {
    string str = "abc";
    str[-1] = 'c';
    cout << str;
}

So, I guess -1 present max in unsigned int. And after str[-1] = 'c', it need a huge string to print out. But I'm not so clear its accurate explanation. Can anyone tell me more about the implementation mechanism of string?

Aucun commentaire:

Enregistrer un commentaire