Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old Feb 3rd, 2007, 11:25 AM   #2
v0id
Hobbyist Programmer
 
Join Date: Apr 2006
Posts: 155
Rep Power: 3 v0id is on a distinguished road
I've found another solution.
Instead of using an std::string for data, I now use a std::vector.
__________________
-- v0id
v0id is offline   Reply With Quote
Old Feb 3rd, 2007, 11:36 AM   #3
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
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
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Feb 3rd, 2007, 1:22 PM   #4
v0id
Hobbyist Programmer
 
Join Date: Apr 2006
Posts: 155
Rep Power: 3 v0id is on a distinguished road
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. :-)
__________________
-- v0id
v0id is offline   Reply With Quote
Old Feb 4th, 2007, 2:40 PM   #5
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3 pegasus001 is on a distinguished road
Whats the size() for?
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Feb 4th, 2007, 5:04 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
code check whether a folder is empty myName C++ 6 Jun 22nd, 2006 9:58 AM
Please Fix My Dll Kilo C++ 12 May 25th, 2006 12:21 PM
Testing for a palindrome using std::string Jessehk C++ 9 May 3rd, 2006 2:55 AM
empty array brad sue Other Programming Languages 3 Apr 22nd, 2006 9:48 PM
change the empty function from the old format to the new format powah Sed and Awk 0 Jun 23rd, 2005 12:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC