std:🧵:length, std:🧵:capacity, std:🧵:size in C++ STL (original) (raw)

Last Updated : 24 Mar, 2023

Prerequisite: String in C++

String class is one of the features provided by the Standard template library to us, So it comes up with great functionality associated with it. With these Functionalities, we can perform many tasks easily. Let's see a few of the functionalities string class provides.

Header File

String Functionalities

There are many functionalities associated with string class but we will be focusing on only three of them, these are:

All three functions are public member functions of the Class String. To know more about other functionalities in string class refer to a String in STL.

1. std:🧵:length

Returns the length of a string. This returns the exact no. of characters or bytes used and not the memory that is allocated at the backend. length member function never throws exceptions.

Syntax:

size_t std:🧵:length( )

Note:

  1. size_t is a typedef version of unsigned int, as sizes cannot be negative.
  2. No parameters are passed, the string is identified with this pointer.

2. std:🧵:size

Returns the length of the string, in terms of bytes. This returns the exact no. of characters or bytes used and not the memory that is allocated at the backend. Both length and size are the same and return the same value but size can be used in any container like vector, list, etc whereas length is more associated with the string. size member function never throws exceptions.

Syntax:

size_t std:🧵:size( )

3. std:🧵:capacity

Syntax:

size_t std:🧵:capacity( );

Code:

C++ `

// C++ Program to demonstrate use of // std:🧵:capacity // std:🧵:size // std:🧵:length #include #include

using namespace std;

int main() { // empty str string str; cout << "str is : " << str << "\n"; cout << "size: " << str.size() << " length: " << str.length() << " capacity: " << str.capacity() << "\n";

// H - only one character
str = "H";
cout << "str is : " << str << "\n";
cout << "size: " << str.size()
     << " length: " << str.length()
     << " capacity: " << str.capacity() << "\n";

// Hello
str = "Hello";
cout << "str is : " << str << "\n";
cout << "size: " << str.size()
     << " length: " << str.length()
     << " capacity: " << str.capacity() << "\n";

// Hello with space added
str = "Hello ";
cout << "str is : " << str << "\n";
cout << "size: " << str.size()
     << " length: " << str.length()
     << " capacity: " << str.capacity() << "\n";

// one more modification
str = "Hello GFG Reader";
cout << "str is : " << str << "\n";
cout << "size: " << str.size()
     << " length: " << str.length()
     << " capacity: " << str.capacity() << "\n";

// Back to Hello
str = "Hello";
cout << "str is : " << str << "\n";
cout << "size: " << str.size()
     << " length: " << str.length()
     << " capacity: " << str.capacity() << "\n";

// reserve is basically request to compiler to change
// the capacity decrease capacity
str.reserve(7);
cout << "str is : " << str << "\n";
cout << "size: " << str.size()
     << " length: " << str.length()
     << " capacity: " << str.capacity() << "\n";

// increase capacity
str.reserve(35);
cout << "str is : " << str << "\n";
cout << "size: " << str.size()
     << " length: " << str.length()
     << " capacity: " << str.capacity() << "\n";

return 0;

}

`

Output

str is : size: 0 length: 0 capacity: 0 str is : H size: 1 length: 1 capacity: 1 str is : Hello size: 5 length: 5 capacity: 5 str is : Hello size: 6 length: 6 capacity: 10 str is : Hello GFG Reader size: 16 length: 16 capacity: 20 str is : Hello size: 5 length: 5 capacity: 20 str is : Hello size: 5 length: 5 capacity: 7 str is : Hello size: 5 length: 5 capacity: 35

The above code demonstrates all the possible scenarios. The facts have also proven that Capacity >= size/length, then the capacity increases automatically as the size of the string crosses the previous capacity, then the capacity is never reduced back when the size goes down, but that can be done explicitly by the programmer using reserve function.

Time and Space Complexity

Functions Time Extra Space
str.size() O(1) O(1)
str.length() O(1) O(1)
str.capacity() O(1) O(1)

4. std:🧵:reserve

Syntax:

void std:🧵:reserve(size_t new_cap);

Example:

C++ `

#include #include

int main() { std::string str; std::cout << "Before reserve(), capacity = " << str.capacity() << std::endl; str.reserve(100); // Reserve memory for 100 characters std::cout << "After reserve(), capacity = " << str.capacity() << std::endl; return 0; }

`

Output

Before reserve(), capacity = 0 After reserve(), capacity = 100

5. std:🧵:shrink_to_fit

Syntax:

void std:🧵:shrink_to_fit();

Example:

C++ `

#include #include

int main() { std::string str = "Hello, World!"; std::cout << "Before shrink_to_fit(), capacity = " << str.capacity() << std::endl; str.reserve(100); // Reserve memory for 100 characters std::cout << "After reserve(), capacity = " << str.capacity() << std::endl; str.shrink_to_fit(); // Shrink the capacity to fit the // actual length std::cout << "After shrink_to_fit(), capacity = " << str.capacity() << std::endl; return 0; }

`

Output

Before shrink_to_fit(), capacity = 13 After reserve(), capacity = 100 After shrink_to_fit(), capacity = 13