The length() function

In c++, length() is just used to get the length of a string.

For example, string STR = “asDFGHJkl”, str.length() = 9.

Size (

In c++, the size() function is used in the same way as the length() function to get the length of a string. In addition, the size() function also gets the length of a vector.

Vector < int> num(15,2); num.size() = 15.

For example: string STR = “d1da”; Then, cout < < STR. The size ();

Sizeof () operator

The sizeof() operator is used to determine how much memory an object occupies.

char c[] = “asdsds”;

char* cc = c;

char cn[40] = “asdsds”;

Int a [] = {6};

int* aa = a;

cout << sizeof(c) << sizeof(cc) << sizeof(*cc) << sizeof(cn);

cout << sizeof(a) << sizeof(aa) << sizeof(*aa);

Result output:

Sizeof (c) =7 //c is an array, evaluated to position ‘\0’, resulting in 6*1+1=7

Sizeof (cc) = 8 //cc is a pointer type of 8 size

Sizeof (*cc) = 1 //*cc refers to the first character of c and is of size 1

Sizeof (cn) =40 // Create 40 char Spaces, 40*1=40

Sizeof (a) =24 //a is an array, but does not count to ‘\0’. The result is 6*4=24

Sizeof (aa) = 8 //aa is a pointer type with a sizeof 8

Sizeof (*aa) = 4 //*aa refers to the first number of a and is of size 4

Note that if we do not pass a Vector as an array, then we need to pass a reference to the array. Otherwise, we cannot calculate the size of the array from the start address in the function.