The STL String Class - Approaching the end
(Page 4 of 4 )
In addition, you also need to know how to delete items from a string. This can be done using the erase method. If you pass a single argument (that is of type int) to the erase member function, all of the string's content will be deleted until the end. When you pass a second argument, you will also indicate the number of characters to delete from the string. On the other hand, you can delete everything inside it with the clear() member function.
std::string text("Yeti Developer");
text.erase(4); //delete all until the end from char at pos 5 text.erase(0,2); //delete the first two characters
cout << text;
ti
Press any key to continue . . .
As good as the string class is, sometimes you may need to get a char* pointer to your data so you can call an already existing function with this as an argument. For this reason, and to maintain some backward compatibility with C, the c_str() and data() member functions have been introduced. This will return a pointer like that for you. With it, you can call functions from the old C world (still, I do not recommend this habit).
strstr(alfa.c_str(), beta.data()); // this will return for //both a char* pointer allowing you to use the function
With this, you know enough of the string container so you can use it if you came from the world of C or just saw it for the first time. Another important question remains unanswered. How we put an int value inside a string? The answer is in the world of streams and will be covered in an upcoming article titled "Using Stringstreams."
I thank you for investing your time in developing yourself by reading this article, and remember that you can post your remarks in the blog section over here or join the DevHardware forums and do the same over there. Until next time, remember to: Live with Passion!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |