Search⌘ K

STL

Understand how to manipulate strings with C++ STL by learning methods like append, erase, and substr. Explore time complexities for operations to improve performance, and prepare for solving common string problems in contests.

We'll cover the following...

C++

Though strings can be represented as an array of characters, most languages have a separate type for strings.

Include statement: #include <string>

Let’s see the string type in C++ and some of its methods that we will be using frequently.

The entire documentation can be found here.

string s; // new empty string

s.size(); // get size()
s[i]; // access character 

Time complexity

The string data type is an array of characters and hence the operations are very similar to that on a vector; memory, time and name-wise. Points to note are

  • Inserting at the end - O(1)O(1)
  • Inserting in between - O(N)O(N)
...