Both at() and operator can be used to access an element in the string. But there exists one difference between them on how to hadle exceptional condition when pos>=size.
- std::basic_string::at throws std::out_of_range if pos >= size().
- std::bsic_string::operator throws no exception and produces undefined result.
Below is the C++ implementation showing exception handling property of std::basic_string::at –
Exception caught basic_string::at: __n (which is 13) >= this->size() (which is 13)
Below is the C++ implementation showing no bounds checking in std::basic_string::operator –
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.