Results 1 to 6 of 6

Thread: What is the difference between length() and size() in C++?

  1. #1
    Join Date
    Dec 2009
    Posts
    14

    What is the difference between length() and size() in C++?

    I am beginner level programmer in the programming world. Right now i am learning the programming languages like C, C++, Core Java, PHP and DBMS. I got the project to do in the C++ language. While doing the project, i came across a query that what is difference between the Length and Size function? And is it length() is substitute of size()? Please help on this topic.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: What is the difference between length() and size() in C++?

    My knowledge about both the length and size function is that they both are identical and all most the same behavior function. According to me, the length() function will return the number of the elements in the current string, and this same role that is performed by the size() function. Hope you will be able understand both the length and size function.

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: What is the difference between length() and size() in C++?

    Nice question asked by you. I would say that the length() function does not require <iostream> or <string.h>, but the size() function requires the <string.h> library. The std::string have both size() and length() methods, and it does return the same value. The std::iostream does not have either size() or length() methods (neither does std::fstream). Hope you got enough information about both the function you told.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: What is the difference between length() and size() in C++?

    Both the function (i.e length() and size()) are useful in the programming manner and mainly used during array introduction in the class. The Length() meant to refer to the contiguous elements. Example- the length of the string. While the Size() meant to refer to the size of the collection, and mostly it will be different from the length function, in case like vectors (or strings), there may be 25 characters in a string, but storage will be reserved for 50.

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: What is the difference between length() and size() in C++?

    According to me, the function length() and size() are one and same. They both same will return the length of the string(i.e they will return the count of the number of characters in the string). There are no parameters in both the function. Example of both function:
    #include <iostream>
    #include <string>
    using namespace demo

    int main ()
    {
    string demo ("Demo string");
    cout << "The length of demo string is " << demo.length() << " characters.\n";
    cout << "The size of demo string is " << demo.size() << " characters.\n"
    return 0;
    }
    Output :
    The length of demo string is 11 characters.
    The size of demo string is 11 characters.

  6. #6
    Join Date
    Apr 2008
    Posts
    1,948

    Re: What is the difference between length() and size() in C++?

    I would say that the when you are dealing with the C++ strings (std::string), then you should be looking for length() or size(). And this both must give you the same value. Also note that size function is consider to be little bit discouragement, because i think that in some implementations it will return the "length() + 1" (because it includes the null terminator). So, i would recommend you to use length() as it is cleaner and more portable.

Similar Threads

  1. Is it screen size of monitor make difference in FPS rate?
    By Ecchumati in forum Monitor & Video Cards
    Replies: 4
    Last Post: 25-11-2011, 02:38 PM
  2. Difference between capacity and size functions of string class
    By DimitrisLiatsos in forum Software Development
    Replies: 5
    Last Post: 25-10-2008, 05:07 PM
  3. Replies: 1
    Last Post: 24-04-2007, 03:14 PM
  4. Replies: 3
    Last Post: 18-04-2007, 04:26 PM
  5. Replies: 1
    Last Post: 04-04-2007, 05:11 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,752,116,083.96095 seconds with 16 queries