operator []: Subscript access to the data contained in the string.
This operator access the character at the specified position in the string if that position exists in the string.
This returns read/write reference to the character.
This operator allows for easy, array-style, data access.
This operator is available in the below file.
File: basic_string.h
Parameters:
__pos – The index of the character to access.
Note that data access with this operator is unchecked and out_of_range lookups are not defined.
Syntax:
str[__pos]
For Example:
str = "abc"
str[0] => It returns reference to the character 'a'.
Approach
C++
#include <bits/stdc++.h>using namespace std;int main(){string str = "abc";cout << str[0] << "\n";return 0;}
No comments:
Post a Comment