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.
17.5 — std:string assignment and swapping
|
Index
|
17.3 — std::string length and capacity
|
17.5 — std:string assignment and swapping
Index
17.3 — std::string length and capacity
[...] 17.4 — std::string character access and conversion to C-style arrays [...]
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
Well, what a journey! Simply amazing tutorial. The best, by far. Clear and concise, exposing exactly what we want to know and mentioning the more advanced or lesser known/used stuff (instead of just not bringing them up at all). You should write a book! I would like to see more chapters added that explore some of the classes that others have suggested throughout the tutorials. I’m sure the information exists somwhere already is merely a Google away, but the way you present information is the best I’ve seen.
Oh, is there a way to get all of this tutorial in a pre-archived format so that I can download it all as a whole package? Would save me heading online all the time.
Anyway, thanks and bye.
I LOVED YOUR TUTORIAL. Thank you so much.
I’ve been trying to learn C++ for years, but I always got stucked in the pointers. Finally I understood them.
I wanted to make games, so I started with BASIC (like 13 years ago), then I learned Visual Basic 6 (bad idea), so when I tried to learn C++ I just cried xD.
Thanks again!!!!
[...] 17.4 — std::string character access and conversion to C-style arrays [...]
[...] 17.4 — std::string character access and conversion to C-style arrays [...]