Sizeof vs Length vs Capacity in C++

  • Thread starter Thread starter Arash_89
  • Start date Start date
A

Arash_89

Guest
Hello,

What is the differences between Sizeof vs Length vs Capacity in C++?

In the below example:

Length() is 12.

Sizeof() is 28 and capacity is 15.

1512819.png


#include <iostream>
#include<string>
using namespace std;

int main()
{
string s;
getline(cin, s);
int sl = s.length();
int so = sizeof(s);
char* p = new char[sl];
for (int i = 0; i < sl; i++)
{
p = s;
}
for (int i = 0; i < sl; i++)
{
cout << p << " ";
}
return 0;
}

Continue reading...
 
Back
Top