C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.
String vs Character Array
String
|
Char Array
|
A string is a class that defines objects that be represented as a stream of characters. |
A character array is simply an array of characters that can be terminated by a null character. |
In the case of strings, memory is allocated dynamically. More memory can be allocated at run time on demand. As no memory is preallocated, no memory is wasted. |
The size of the character array has to be allocated statically, more memory cannot be allocated at run time if required. Unused allocated memory is also wasted |
As strings are represented as objects, no array decay occurs. |
There is a threat of array decay in the case of the character array. |
Strings are slower when compared to implementation than character array. |
Implementation of character array is faster than std:: string. |
String class defines a number of functionalities that allow manifold operations on strings. |
Character arrays do not offer many inbuilt functions to manipulate strings. |
Operations on Strings
1) Input Functions
Function |
Definition |
getline() |
This function is used to store a stream of characters as entered by the user in the object memory. |
push_back() |
This function is used to input a character at the end of the string. |
pop_back() |
Introduced from C++11(for strings), this function is used to delete the last character from the string. |
Example:
CPP
#include <iostream>
#include <string> // for string class
using namespace std;
int main()
{
string str;
getline(cin, str);
cout << "The initial string is : " ;
cout << str << endl;
str.push_back( 's' );
cout << "The string after push_back operation is : " ;
cout << str << endl;
str.pop_back();
cout << "The string after pop_back operation is : " ;
cout << str << endl;
return 0;
}
|
Output
The initial string is :
The string after push_back operation is : s
The string after pop_back operation is :
Time Complexity: O(1)
Space Complexity: O(n) where n is the size of the string
2) Capacity Functions
Function |
Definition |
capacity() |
This function returns the capacity allocated to the string, which can be equal to or more than the size of the string. Additional space is allocated so that when the new characters are added to the string, the operations can be done efficiently. |
resize() |
This function changes the size of the string, the size can be increased or decreased. |
length() |
This function finds the length of the string. |
shrink_to_fit() |
This function decreases the capacity of the string and makes it equal to the minimum capacity of the string. This operation is useful to save additional memory if we are sure that no further addition of characters has to be made. |
Example:
CPP
#include <iostream>
#include <string> // for string class
using namespace std;
int main()
{
string str = "geeksforgeeks is for geeks" ;
cout << "The initial string is : " ;
cout << str << endl;
str.resize(13);
cout << "The string after resize operation is : " ;
cout << str << endl;
cout << "The capacity of string is : " ;
cout << str.capacity() << endl;
cout << "The length of the string is :" << str.length()
<< endl;
str.shrink_to_fit();
cout << "The new capacity after shrinking is : " ;
cout << str.capacity() << endl;
return 0;
}
|
Output
The initial string is : geeksforgeeks is for geeks
The string after resize operation is : geeksforgeeks
The capacity of string is : 26
The length of the string is :13
The new capacity after shrinking is : 13
Time Complexity: O(1)
Space Complexity: O(n) where n is the size of the string
3) Iterator Functions
Function |
Definition |
begin() |
This function returns an iterator to the beginning of the string. |
end() |
This function returns an iterator to the next to the end of the string. |
rbegin() |
This function returns a reverse iterator pointing at the end of the string. |
rend() |
This function returns a reverse iterator pointing to the previous of beginning of the string. |
cbegin() |
This function returns a constant iterator pointing to the beginning of the string, it cannot be used to modify the contents it points-to. |
cend() |
This function returns a constant iterator pointing to the next of end of the string, it cannot be used to modify the contents it points-to. |
crbegin() |
This function returns a constant reverse iterator pointing to the end of the string, it cannot be used to modify the contents it points-to. |
crend() |
This function returns a constant reverse iterator pointing to the previous of beginning of the string, it cannot be used to modify the contents it points-to. |
Algorithm:
- Declare a string
- Try to iterate the string using all types of iterators
- Try modification of the element of the string.
- Display all the iterations.
Example:
CPP
#include <iostream>
#include <string> // for string class
using namespace std;
int main()
{
string str = "geeksforgeeks" ;
std::string::iterator it;
std::string::reverse_iterator it1;
cout<< "Str:" <<str<< "\n" ;
cout << "The string using forward iterators is : " ;
for (it = str.begin(); it != str.end(); it++){
if (it == str.begin()) *it= 'G' ;
cout << *it;
}
cout << endl;
str = "geeksforgeeks" ;
cout << "The reverse string using reverse iterators is "
": " ;
for (it1 = str.rbegin(); it1 != str.rend(); it1++){
if (it1 == str.rbegin()) *it1= 'S' ;
cout << *it1;
}
cout << endl;
str = "geeksforgeeks" ;
cout<< "The string using constant forward iterator is :" ;
for ( auto it2 = str.cbegin(); it2!=str.cend(); it2++){
cout<<*it2;
}
cout<< "\n" ;
str = "geeksforgeeks" ;
cout<< "The reverse string using constant reverse iterator is :" ;
for ( auto it3 = str.crbegin(); it3!=str.crend(); it3++){
cout<<*it3;
}
cout<< "\n" ;
return 0;
}
|
Output
Str:geeksforgeeks
The string using forward iterators is : Geeksforgeeks
The reverse string using reverse iterators is : Skeegrofskeeg
The string using constant forward iterator is :geeksforgeeks
The reverse string using constant reverse iterator is :skeegrofskeeg
Time Complexity: O(1)
Space Complexity: O(n) where n is the size of the string
4) Manipulating Functions:
Function |
Definition |
copy(“char array”, len, pos) |
This function copies the substring in the target character array mentioned in its arguments. It takes 3 arguments, target char array, length to be copied, and starting position in the string to start copying. |
swap() |
This function swaps one string with another |
Example:
CPP
#include <iostream>
#include <string> // for string class
using namespace std;
int main()
{
string str1 = "geeksforgeeks is for geeks" ;
string str2 = "geeksforgeeks rocks" ;
char ch[80];
str1.copy(ch, 13, 0);
cout << "The new copied character array is : " ;
cout << ch << endl;
cout << "The 1st string before swapping is : " ;
cout << str1 << endl;
cout << "The 2nd string before swapping is : " ;
cout << str2 << endl;
str1.swap(str2);
cout << "The 1st string after swapping is : " ;
cout << str1 << endl;
cout << "The 2nd string after swapping is : " ;
cout << str2 << endl;
return 0;
}
|
Output
The new copied character array is : geeksforgeeks
The 1st string before swapping is : geeksforgeeks is for geeks
The 2nd string before swapping is : geeksforgeeks rocks
The 1st string after swapping is : geeksforgeeks rocks
The 2nd string after swapping is : geeksforgeeks is for geeks
Must Read: C++ String Class and its Applications
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Feb, 2023
Like Article
Save Article