Character access
There are two almost identical ways to access characters in a string. The easier to use and faster version is the overloaded operator[]:
|
char& string::operator[] (size_type nIndex) const char& string::operator[] (size_type nIndex) const
Sample code:
string sSource("abcdefg");
cout << sSource[5] << endl;
sSource[5] = 'X';
cout << sSource << endl;
Output: f abcdeXg |
There is also a non-operator version. This version is slower since it uses exceptions to check if the nIndex is valid. If you are not sure whether nIndex is valid, you should use this version to access the array:
|
char& string::at (size_type nIndex) const char& string::at (size_type nIndex) const
Sample code:
string sSource("abcdefg");
cout << sSource.at(5) << endl;
sSource.at(5) = 'X';
cout << sSource << endl;
Output: f abcdeXg |
Conversion to C-style arrays
Many functions (including all C functions) expect strings to be formatted as C-style strings rather than std::string. For this reason, std::string provides 3 different ways to convert std::string to C-style strings.
const char* string::c_str () const
Sample code:
string sSource("abcdefg");
cout << strlen(sSource.c_str());
Output: 7 |
const char* string::data () const
Sample code:
string sSource("abcdefg");
char *szString = "abcdefg";
// memcmp compares the first n characters of two C-style strings and returns 0 if they are equal
if (memcmp(sSource.data(), szString, sSource.length()) == 0)
cout << "The strings are equal";
else
cout << "The strings are not equal";
Output: The strings are equal |
|
size_type string::copy(char *szBuf, size_type nLength) const size_type string::copy(char *szBuf, size_type nLength, size_type nIndex) const
Sample code:
string sSource("sphinx of black quartz, judge my vow");
char szBuf[20];
int nLength = sSource.copy(szBuf, 5, 10);
szBuf[nLength]='\0'; // Make sure we terminate the string in the buffer
cout << szBuf << endl;
Output: black |
Unless you need every bit of efficiency, c_str() is the easiest and safest of the three functions to use.
|
Index
|
17.3 — std::string length and capacity
|

Index
17.3 — std::string length and capacity
Amazing tutorial, thank you! When studying a C++ course at university where pedagogy is not prioritized it’s a bless to have a tutorial like this to learn the basics.
Even though I know it is not your intent to cover the standard library some examples covering things like iterators, vectors and such would be much appreciated in the future.
Thank you for the great tutorial,
It really helped me learn to program in c++, and I believe it helped me to write better programs in general.
The tutorial was really clear and easy to understand, and I found it quite complete already.
I’m sure I will check back to skim-read one of the harder topics or see if you put up a new chapter for this awesome tutorial.
Awesome tutorial. I was looking to learn C++ for some game programing. I’m pretty bad at math, but it seems pretty managable the way you present it. Thanks so much.
can anybody write me a programme to open the url using C++ please give the complete programme