Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Strings in Memory | Text Data Type
course content

Contenido del Curso

C++ Data Types

Strings in MemoryStrings in Memory

As shown in the video, strings have length and capacity. They can be accessed using .length() and .capacity() methods.

cpp

main.cpp

There are also two methods to work with capacity, .reserve(n) and shrink_to_fit().

  • .reserve(n) – set capacity to at least n;
  • .shrink_to_fit() – set capacity the same as the length.

So the .shrink_to_fit() is useful when you know you will not extend your string, but the capacity is greater than the length. And you can use .reserve(n) if your string is likely to be appended multiple times.

cpp

main.cpp

¿Todo estuvo claro?

Sección 3. Capítulo 7
course content

Contenido del Curso

C++ Data Types

Strings in MemoryStrings in Memory

As shown in the video, strings have length and capacity. They can be accessed using .length() and .capacity() methods.

cpp

main.cpp

There are also two methods to work with capacity, .reserve(n) and shrink_to_fit().

  • .reserve(n) – set capacity to at least n;
  • .shrink_to_fit() – set capacity the same as the length.

So the .shrink_to_fit() is useful when you know you will not extend your string, but the capacity is greater than the length. And you can use .reserve(n) if your string is likely to be appended multiple times.

cpp

main.cpp

¿Todo estuvo claro?

Sección 3. Capítulo 7
some-alt