如果访问错误的字符串索引,c ++不会抛出错误

Sae*_*ani 0 c++ arrays string

我有以下代码:

string s = "haha";
cout << s << endl; //haha
cout << s.length() << endl; //4
cout << (s[25] == 'h' ? "is h" : "is not h") << endl; //is not h
cout << s[0] << endl; // h
cout << s[25] << endl; // nothing...blank
Run Code Online (Sandbox Code Playgroud)

似乎指向任何索引,即使它超出范围也不会抛出任何错误......这是什么原因?

For*_*veR 8

效率.您可以使用at函数进行边界检查.