Click here to Skip to main content
15,885,214 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
In the following code how can we differentiate between size() and length() of string ?

C++
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
   char a[5]="abc";
    
    cout<<sizeof(a);
    cout<<endl;
    
    string s;
    s=a;
    cout<<s.length();
    cout<<endl;
    cout<<s.size();
    cout<<endl;
    
    string b[3];
    cout<<"Size of string :"<<s.size()<<endl;
    b[0]="abcd";
    cout<<"Size of string :"<<s.length();
    getch();
    return 0;
}
Posted
Updated 5-Sep-12 6:32am
v2
Comments
[no name] 5-Sep-12 12:34pm    
size_t size() const;Return length of string Returns a count of the number of characters in the string. string::length is an alias of string::size, returning both the exact same value. http://www.cplusplus.com/reference/string/string/size/
Philip Stuyck 5-Sep-12 13:02pm    
counts as an answer if you ask me ;-)
Albert Holguin 5-Sep-12 13:36pm    
I think that pretty much is the answer... think Wes must've been doubting something.
Sergey Alexandrovich Kryukov 5-Sep-12 14:09pm    
I think this is the Wes's style. He post a great number of very good comments to the questions, and many would make good answers. I wish I could up-voted them.
--SA
[no name] 5-Sep-12 14:35pm    
No need. Wes finds it hard to care about too much these days. Points are not important.

As Wes answered in the comments... it's just an alias...

See here[^]

There's probably some sort of historical reason why there's two methods that do the same exact thing, but it's really irrelevant at this point. I'd recommend you use only one version in your code since it'll make it more readable and easier to support in the long run (if they decide to eliminate one or the other in the future it'll be easier to fix).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Sep-12 14:07pm    
5ed, but Wes does deserve 5, too.
--SA
Albert Holguin 5-Sep-12 14:28pm    
Of course... He's eternally stuck on comments... :)
[no name] 5-Sep-12 14:19pm    
Of course, 5
Albert Holguin 5-Sep-12 14:28pm    
Thanks Wes.. Of course, you deserve the credit for this one. Post more answers and less comments... :p
Sergey Alexandrovich Kryukov 5-Sep-12 18:50pm    
@Wes: I agree. I wish I could up-vote many of your comments; they are very good.
--SA
Maybe not a strictly formal answer, but I've seen the convention that 'size' refers to the number of bytes, and 'length' refers to the number of elements. So for a char array, the size and length are the same, but for arrays of other data types the size would be greater.
 
Share this answer
 
Comments
enhzflep 6-Jan-15 0:32am    
That may well be the case for other data-types, but don't forget - we're dealing with the std::string class here. This answer really doesn't add anything to the topic, in my opinion.

Realizing that std::wstring is the wide-char version of a std::string, we can use them with the same syntax, yet different input data. If you try to compare the size and length of a string with the size and length of a wstring, you get the exact same answer. In each case, the returned value is the number of characters in the string.

An example: (sorry for the lack of formatting)

---------------------------8<--------------------------
#include <iostream>

using namespace std;

int main()
{
wstring mWideString = L"This is a string";
string mCharString = "This is a string";

wcout << mWideString << endl;
cout << mCharString << endl;

cout << "Wide String:" << endl;
cout << "Size: " << mWideString.size() << endl;
cout << "Length: " << mWideString.length() << endl;
cout << endl;

cout << "Narrow String:" << endl;
cout << "Size: " << mCharString.size() << endl;
cout << "Length: " << mCharString.length() << endl;
cout << endl;
}
---------------------------8<--------------------------

Output:
=======
This is a string
This is a string
Wide String:
Size: 16
Length: 16

Narrow String:
Size: 16
Length: 16

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900