std::basic_string::at in C++ Last Updated : 19 Sep, 2023 Comments Improve Suggest changes Like Article Like Report 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 pos); const_reference at (size_type pos) const; Parameters : pos - position of the character to return Return value : Reference to the requested character Exceptions : Throws std::out_of_range if pos >= size(). CPP // CPP program to access a character through // std::basic_string::at #include <stdexcept> #include <iostream> 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 out of bounds index try { str.at(4) = 't'; } // If error is generated, it is caught catch (std::out_of_range const& error) { std::cout << error.what() << '\n'; } } Output: string size = 3 basic_string::at: __n (which is 4) >= this->size() (which is 3) Comment More infoAdvertise with us Next Article std::basic_string::at in C++ R Rohit Thapliyal Improve Article Tags : Misc C++ DSA STL cpp-strings-library +1 More Practice Tags : CPPMiscSTL Similar Reads 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::operator[] in C++ 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 Retu 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::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::data() in C++ The data() function writes the characters of the string into an array. It returns a pointer to the array, obtained from conversion of string to the array. Its Return type is not a valid C-string as no '\0' character gets appended at the end of array. Syntax: const char* data() const; char* is the po 2 min read Like