Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   std::string is not empty? (http://www.programmingforums.org/showthread.php?t=12497)

v0id Feb 3rd, 2007 6:02 AM

std::string is not empty?
 
Is the std::string empty - or not?
If we check a string like this
:

std::string myStr;

if(myStr.empty())
    std::cout << "The string is empty" << std::endl;
else
    std::cout << "The string is not empty" << std::endl;

It of course returns the first message, but if we do like this
:

for(int i = 0; i <= 50; i++)
{
    if(static_cast<int>(myStr[i]) != 0)
        std::cout << "Index[" << i << "] = " << static_cast<int>(myStr[i]) << std::endl;
}

Then there's is some of them with content. On my computer I get the output
Quote:

Originally Posted by Output
Index[4] = 7
Index[8] = 8
Index[12] = 11
Index[16] = 6
Index[20] = 4
Index[24] = 5
Index[28] = 14
Index[32] = 3
Index[36] = 12
Index[40] = 13
Index[44] = 9
Index[48] = 10

As we can see it jumps four, before it's contains content, why?
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...
// myStr[4] = 0;
myStr[4]++;
// myStr[4] = 1;

// This is how it is now...
// myStr[4] = 7;
myStr[4]++;
// myStr[4] = 8;


v0id Feb 3rd, 2007 11:25 AM

I've found another solution.
Instead of using an std::string for data, I now use a std::vector.

Eoin Feb 3rd, 2007 11:36 AM

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 :)

v0id Feb 3rd, 2007 1:22 PM

Quote:

Originally Posted by Eoin
In addition I suspect trying to access index's outside the strings length is very dangerous.

It was exactly because of this reason I choosed a container instead. I'd been on the wrong track all the time, because it was only data I should store, not a specific string - to output, eg.

Thanks for your time and answer. :-)

pegasus001 Feb 4th, 2007 2:40 PM

Whats the size() for?

DaWei Feb 4th, 2007 5:04 PM

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