• Advertisement

Archived

This topic is now archived and is closed to further replies.

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

This topic is 5847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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++ =)

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Are you talking about std::string? or just some random string class?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
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.



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..
=)

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

  • Advertisement