| string::operator[] |
public member function |
const char& operator[] ( size_t pos ) const;
char& operator[] ( size_t pos ); |
|
Get character in string
Returns a reference the character at position pos in the string.
The function actually returns data()[ pos ].
The at member function has the same behavior as this operator function, except that at also performs a range check.
Parameters
- pos
- Position within the string of the character to be retrieved. Notice that the first character in the string has a position of 0, not 1.
size_t is an unsigned integral type.
Return value
The character at the specified position in the string.
Example
// string::operator[]
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Test string");
int i;
for (i=0; i < str.length(); i++)
{
cout << str[i];
}
return 0;
}
|
This code prints out the content of a string character by character using the offset operator on the string object str.
Basic template member declaration
( basic_string<charT,traits,Allocator> )
typedef typename Allocator::const_reference const_reference;
typedef typename Allocator::reference reference;
typedef typename Allocator::size_type size_type;
const_reference operator[] ( size_type pos ) const;
reference operator[] ( size_type pos );
|
See also
| string::at | Get character in string (public member function) |
| string::find | Find content in string (public member function) |