std::basic_string::operator[] in C++
Last Updated :
14 Jul, 2017
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
#include <iostream>
int main()
{
std::string str = "abc" ;
std::cout << "string size = " << str.size() << '\n' ;
std::cout << "Element : " << str[2];
}
|
Output
string size = 3
Element : c
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...