![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 155
Rep Power: 3
![]() |
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;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;
}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... // myStr[4] = 0; myStr[4]++; // myStr[4] = 1; // This is how it is now... // myStr[4] = 7; myStr[4]++; // myStr[4] = 8;
__________________
-- v0id
|
|
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 155
Rep Power: 3
![]() |
I've found another solution.
Instead of using an std::string for data, I now use a std::vector.
__________________
-- v0id
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#4 | |
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 155
Rep Power: 3
![]() |
Quote:
Thanks for your time and answer. :-)
__________________
-- v0id
|
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |