String Class in C++ Standard Template Library (STL)

The Standard Template Library (STL) in C++ gives us a wide range of powerful tools and data structures to simplify our programming tasks. String class is one of the most commonly used components in the STL. 

In this blog post, we will go through String class in STL, exploring its features, functionalities, and demonstrating code examples to highlight its usefulness.


Overview of the String Class:

String class in the STL provides a convenient way to handle and manipulate strings of characters in C++. It offers a rich set of member functions and operators to perform various functions on strings like concatenation, comparison, searching, and more.

1. The string class is a specialization of a more general template class called basic_string.

2. Since defining a class in C++ is creating a new datatype, string is derived datatype.

3. This means operators can be overloaded for the class.

4. String is safer than char array. It is container class in STL.

5. In order to use string class, you have to include string header in the header section of the programme. #include<string>


Creating String Objects:

#include<iostream>
#include<string>
using namespace std;
int main() {
  string myString = "Hello, world!";
  return 0;
}

Basic Operations:

The String class supports various basic operations for manipulating strings. Here are a few examples:

Concatenation: Strings can be concatenated using operators like '+' or  '+='. 
'+=' is called compound assignment operator.

Length: The length() or size() member functions provide the length of the string.

Substring Extraction: The substr() function allows extracting a portion of the string.

String Comparisons:

The string class enables comparison operations between strings using operators like '==', '!=', '<', '>', '<=', and '>='. We will demonstrate how to compare strings using these operators and the member function 'compare()'. These functions return a value based on the comparison of the strings.


Input and Output:

The String class seamlessly integrates with input and output streams. The 'cin' object is used to read strings from the user and the 'cout' object is used to display strings to the user. For example:
#include<iostream>
using namespace std;
int main() {
  string myString;
  cout << "Enter your name: ";
  cin >> myString;
  cout << "Hello, " << myString << "!" << endl;
  return 0;
}

String Initialization and Assignment:

The string class provides multiple ways to initialize and assign values. Examples include initializing with a string literal, another string object, or a character array. This blog will demonstrate different initialization methods that are used in string class.


String Operations:

The string class provides a multitude of operations for string manipulation. Below are some commonly used operations of string:

  1. Appending and concatenating strings.
  2. Accessing individual characters.
  3. Modifying strings using insertions, deletions, and replacements.
  4. Finding substrings and searching for specific characters.
  5. Converting strings to uppercase or lowercase.
  6. Getting the length and capacity of a string.


String Input and Output:

The string class seamlessly integrates with input/output streams, allowing easy input and output operations. This blog will show how to read strings from the user and write strings to the console or file using standard stream objects.


Performance Considerations:

We will briefly touch upon the performance considerations when using the string class, such as avoiding unnecessary string copies and utilizing move semantics when appropriate.


Constructors in String class in STL in C++

String class supports many constructors, some of them are given below:
  1. string()
  2. string(const char *str)
  3. string(const string &str)
Below is an example on how to use constructor in string:
#include<iostream>
#include<string>
using namespace std;
int main() {
 string S1;
 string S2 = "HELLO";
 
 // string S2("HELLO");
 // string S2 = string("HELLO");
 
 string S3(S2);      // string S3 = S2;
 return 0;
}

Operators in String:

  1. = : assignment operator
  2. + : concatenation operator
  3. += : concatenation assignment operator
  4. == : equality operator
  5. != : inequality operator
  6. < : less than operator
  7. <= : less than or equal operator
  8. > : greater than operator
  9. >= : greater than or equal operator
  10. [] : subscripting operator
  11. << : insertion (used for output) operator
  12. >> : extraction (used for input) operator
Given below is an example how you can use these operators in program:
#include<iostream>
#include<string>
using namespace std;
int main() {
  string myStr;
  myStr = "Hello ";  //assignment operator
  
  string myStr2 = "Students";
  myStr2 = myStr+myStr2;     //concatenation operator
  
  myStr += myStr2;     //concatenation assignment operator
  //myStr = myStr+myStr2;
  
  if (myStr == myStr2)
    return 0;
    
  cout << myStr << myStr2 << endl;
    
  cout << "Enter your Name";
  string myStr3;
  cin >> myStr3;
  cout << "Hello," << myStr3;
  
  char string1[] = "Students";
  string myStr4 = myStr1 + string1;    //mixed operations
  cout << myStr4;     // Output: Hello Students
  return 0;
}

Useful Methods of String class in C++:

  1. assign() : used to assign new value to a string
  2. begin() : used to return first character's reference
  3. append() : appends the first string to the second string
  4. insert() : used to insert a new character
  5. replace() : used to replace a portion of string
  6. erase() : used to remove the characters
  7. find() : used to find the string or character
  8. rfind() : used to search for the string's last occurence
  9. compare() : used to compare two strings
  10. c_str() : used to return pointer to an array
  11. size() : used to find the string length in terms of bytes

#include<iostream>
#include<string>
using namespace std;
int main() {
  string S1;
  S1.assign("Hello");
  cout << S1;  //Output: Hello
  
  S1.append("Students");
  cout << S1;     //Output: Hello Students
  
  string S2 = "Hello";
  S2.insert(2, "123");
  cout << S2;    //Output: He123llo
  
  S2.replace(2,2,"N");
  cout << S2;   //Output: HeA3llo
  S2.erase();         //delete all strings element
  
  string M = "Hello Online Students";
  int i = M.find("online");
  cout << i;      //Output: 6
  cout M.find("NKS");   //Output: -1 (it means not found)
  
  string N = "Hello online online students";
  cout << N.rfind("online");     //Output: 13
  
  string som = "Alexander";
  string suo = "Felarus";
  int i = som.compare(suo);
  cout << i;      //Output: -1
  
  string N1 = "Hello";
  char str[20];
  strcpy(str, N1.c_str());
  cout << str;      //Output: Hello
  
  int i = N1.size();
  cout << i;         //Output: 5
  return 0;
}

The string class in the C++ Standard Template Library (STL) provides a powerful and efficient way to handle string manipulation in C++.

Its intuitive member functions and operators simplify common string operations. By leveraging the features and functionalities of the string class, programmers can efficiently work with strings in their C++ projects.


Note: Remember to include the <string> header in your code to access the String class and its functionalities. By mastering the String class, you will have a powerful tool at your disposal for working with strings in C++ with ease.



Previous
Next Post »