string p="a";
cout<<p[4];
This piece of code doesn't give any error. Does this mean the string data type in C++ is also not bounded as are the arrays?
string p="a";
cout<<p[4];
This piece of code doesn't give any error. Does this mean the string data type in C++ is also not bounded as are the arrays?
 
    
     
    
    Your code has undefined behavior.
std::string::operator[] does not perform any bound checks.
std::string::at() on the other hand performs bound checks and throws an exception of type std::out_of_range when you access data that is out of bounds.
Does this means the string "data type" in C++ is also not bounded as are the arrays?
Of course std::string has bounds. The way you access its contents is where the difference comes in whether bound checks will be performed or not.
 
    
    See std::string::at(), it will throw an std::out_of_range on error. The operator [] does not check bounds.
The same is true for standard containers, which provide random access (std::array, std::vector, std::deque, std::map, std::unordered_map): normally, in C++ you are supposed to always ensure no out-of-range access occurs. This allows for a greater performance of the program. But in some specific cases, it could be beneficial to use exception handling (via try and catch) instead, sacrificing some performance.
