In C + +, subscript operations do not add elements, and for any use of subscript operations, such as string types, vector types, and so on, must be existing elements to be indexed with subscript operators. If the type is empty, pass
When the subscript operation is assigned, no element is added and does not work at all, and the validation code is as follows:
1#include <iostream>2#include <vector>3 using namespacestd;4 intMain ()5 {6 stringRes;7res[0] ='H';8res[1] ='e';9res[2] ='L';Tenres[3] ='L'; Oneres[4] ='o'; Acout << Res <<"\ n"; - return 0; -}
The result of the final output is null. If you want to do this, the code looks like this:
1#include <iostream>2#include <vector>3 using namespacestd;4 intMain ()5 {6 stringRes5,"');7res[0] ='H';8res[1] ='e';9res[2] ='L';Tenres[3] ='L'; Oneres[4] ='o'; Acout << Res <<"\ n"; - return 0; -}
The principle is that existing elements use subscript operators to make sense.
C + + subscript operation considerations