std::basic_string::operator[] in C++ Last Updated : 14 Jul, 2017 Comments Improve Suggest changes Like Article Like Report Returns a reference to the character at specified location pos. No bounds checking is performed. If pos > size(), the behavior is undefined. Syntax : reference operator[] (size_type pos); const_reference operator[] (size_type pos) const; Parameters : pos - position of the character to return Return value : Reference to the requested character Exceptions : No exception is thrown CPP /* CPP program to access a character through std::basic_string::operator[] */ #include <iostream> //Driver code int main() { //String with valid indices from 0 to 2 std::string str = "abc"; //Printing size of string std::cout << "string size = " << str.size() << '\n'; //Accessing element at index 2 std::cout << "Element : " << str[2]; } Output string size = 3 Element : c Comment More infoAdvertise with us Next Article std::basic_string::operator[] in C++ R Rohit Thapliyal Improve Article Tags : Misc C++ DSA STL cpp-strings-library +1 More Practice Tags : CPPMiscSTL Similar Reads std::basic_string::at in C++ Returns a reference to the character at the specified location pos. The function automatically checks whether pos is the valid position of a character in the string (i.e., whether pos is less than the string length), throwing an out_of_range exception if it is not. Syntax: reference at (size_type po 1 min read std::basic_string::max_size in C++ std::basic_string::max_size is used to compute the maximum number of elements the string is able to hold due to system or library implementation limitations. size_type max_size() const; Parameters : None Return value : Maximum number of characters Exceptions : None CPP // CPP program to compute the 1 min read std::string::push_back() in C++ The std::string::push_back() method in C++ is used to append a single character at the end of string. It is the member function of std::string class defined inside <string> header file. In this article, we will learn about std::string::push_back() method in C++.Example:C++// C++ Program to ill 2 min read std::string::assign() in C++ The member function assign() is used for the assignments, it assigns a new value to the string, replacing its current contents. Syntax 1: Assign the value of string str. string& string::assign (const string& str) str : is the string to be assigned. Returns : *this CPP // CPP code for assign 5 min read std::basic_string::at vs std::basic_string::operator[] std::basic_string::at, std::basic_string::operator[]Both at() and operator[] can be used to access an element in the string. But there exists one difference between them on how to handle exceptional condition when pos>=size. std::basic_string::at throws std::out_of_range if pos >= size().std:: 2 min read Like