String.size() & String.length() - Whats the difference??

Started by
3 comments, last by Tonic151 22 years ago
String.size() and String.length() - Whats the difference?? i''ve been working with a lot of string conversion lately and i''m not excatly sure what the best function to use for determing the number of characters a string has... Can Anyone Explance the differences between these two.. C++ =)
Advertisement
Are you talking about std::string? or just some random string class?
For std::string, there is absolutely no difference, the two functions are the same.

quote:Stroustrup, p586
The length() of a string is simply a synonym for its size(); both functions return the number of characters in the string. Note that they do not count a C-string-style, zero-terminator character (§20.4.1). An implementation of basic_string stores its length rather than relying on a terminator.


[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by Fruny
For std::string, there is absolutely no difference, the two functions are the same.

Stroustrup, p586
The length() of a string is simply a synonym for its size(); both functions return the number of characters in the string. Note that they do not count a C-string-style, zero-terminator character (§20.4.1). An implementation of basic_string stores its length rather than relying on a terminator.


Ahh Thank you!! Seems pretty dumb though. I guess i was thinking two different functions should have 2 differences.. at least 1 of them should maybe count the NULL at the end or something special.. but two of the same seems a bit pointless..
=)
People talking about strings tend to think about the length of the string. Algorithms working on containers tend to refer to the size of the container.

And yes, strings are STL containers;

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement