View Single Post
Old Feb 3rd, 2007, 6:02 AM   #1
v0id
Hobbyist Programmer
 
Join Date: Apr 2006
Posts: 155
Rep Power: 3 v0id is on a distinguished road
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
v0id is offline   Reply With Quote