Strings in C++ are objects of the std::string class. They are used to represent and manipulate sequences of characters.
- Unlike C-style character arrays (char[]), std::string handles memory management automatically and provides a wide range of built-in functions for ease of use.
- Can automatically grow and shrink as you add or remove characters, unlike fixed-size C-style strings.
- You can easily access characters, join strings, compare them, extract substrings, and search within strings using built-in functions.
OutputUsing index: Hello Geeks
Using range-based for loop: Hello Geeks
Using iterator: Hello Geeks
Syntax
The string container is defined as std::string class inside the <string> header file.
string str;
where,
- string: Class provided by STL to handle sequences of characters.
- str: Name assigned to the string object.
Basic Operations in String
Basic operations on Strings are shown below:
Initializing a String
- Initialization of a string assigns characters to the string at the time of creation.
- A string can be initialized directly using = or constructor syntax with text inside quotes.
Accessing Characters
- Characters of a string can be accessed using the [] operator or the .at() function.
- Time complexity for accessing characters is O(1).
OutputFirst character: H
Fifth character: o
Character at index 6: G
String Length
- The number of characters in a string can be found using size() or length().
- Time complexity to find string length is O(1).
Concatenation of Strings
- Strings can be joined using the + operator or the append() function.
- The + operator creates a new string, while append() modifies the existing string in place.
- Time complexity for concatenation is O(n+m), where n is the size of string and m is the size of the string to be concatenated.
OutputConcatenation using + : Hello Geeks
Concatenation using append(): Hello Geeks
Modifying a String
- Characters of a string can be added with .push_back(), removed with .pop_back(), or altered using .insert() and .erase().
- Time complexity for push/pop is O(1) and O(n) for insert/erase.
OutputAfter push_back: Hello Geeks!
After pop_back: Hello Geeks
After insert: Hello C++ Geeks
After erase: Hello Geeks
Substring Extraction
- The .substr(pos,len) is used to extract a part of a string, where pos means the starting position and len means how many characters you want to copy.
- This function creates a new string containing the selected portion, starting at pos and copying len characters.
- Time complexity of extraction is O(len).
OutputSubstring 1: Hello
Substring 2: Geeks
Know more about Substring extraction in C++.
Searching in a String
- The find() function is used to search for a substring inside a string. If found, it returns the index (position) where the substring starts; if not, it returns a special value (npos).
- Time complexity to search is O(n*m), where n is the length of string and m is the substring length.
Output"Geeks" found at index: 6
Learn more about find() in C++
C++ String Functions
C++ provides some inbuilt functions which are used for string manipulation, such as the strcpy() and strcat() functions for copying and concatenating strings. Some of them are:
Function | Description |
|---|
length() | This function returns the length of the string. |
|---|
swap() | This function is used to swap the values of 2 strings. |
|---|
size() | Used to find the size of string |
|---|
resize() | This function is used to resize the length of the string up to the given number of characters. |
|---|
push_back() | This function is used to push the passed character at the end of the string |
|---|
pop_back() | This function is used to pop the last character from the string |
|---|
clear() | This function is used to remove all the elements of the string. |
|---|
find() | This function is used to search for a certain substring inside a string and returns the position of the first character of the substring. |
|---|
replace() | Replaces a portion of the string with another string. |
|---|
substr() | This function is used to create a substring from a given string. |
|---|
compare() | This function is used to compare two strings and returns the result in the form of an integer. |
|---|
erase() | This function is used to remove a certain part of a string. |
|---|
rfind() | This function is used to find the string's last occurrence. |
|---|
These functions are discussed in this article in more detail - String Function in C++