![]() |
std::string is not empty?
Is the std::string empty - or not?
If we check a string like this :
std::string myStr;:
for(int i = 0; i <= 50; i++)Quote:
How can I competely clear the string? I've tried clear() but it again returns some strange content. I've to clear the string competely, because I've to do actions like myStr[2]++ on empty strings, and it's hard if it contains something... :
// This is how it should be... |
I've found another solution.
Instead of using an std::string for data, I now use a std::vector. |
Hi v0id, the internal format of a std::string is not specified specifically so you cannot rely on something like say an empty string being 'zero'ed unfortunately. In addition I suspect trying to access index's outside the strings length is very dangerous.
I would advise designing your own simple string class based on your needs, perhaps one which you could convert to and from std::strings as you need. You could use something like std::vector<char> for the memory which would mean you'd know exactly what is going on beneath the surface. [edit] you beat me to the answer :) |
Quote:
Thanks for your time and answer. :-) |
Whats the size() for?
|
size () is for the size of the string, in characters. It has nothing to do with the size of the class, per se, which has a number of controlling entities. A study (or design) of containers would probably clear up some unwarranted misconceptions.
|
| All times are GMT -5. The time now is 1:52 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC