- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove all the occurrences of a character from a string using STL
STL basically stands for Standard Template Library, which is a collection of pre-written code frequently used in Data structures and Algorithms. It was developed in the early 1990s by Meng Lee and Alexander Stepanov.
It consists of mainly three components known as containers, algorithms and iterators. Containers are objects that stores and manipulate data such as list, vector,set, map and stack. Algorithm are functions which works on the data stored in the containers such as searching, sorting and also manipulating data. Iterators are object that navigates through the elements of a container easily.
STL has became an significant part of competitive programming, also it provides efficient and robust code.
#include <iostream> #include <string> using namespace std; int main() { string a = "Hello, world!"; cout << a << endl; return 0; }
Output
Hello, world!
Algorithm
Declare a string and a character you want to remove. Then store them in a variable.
Loop through each character in a string.
Check if the current character matches to the character you want to remove.
Repeat the above two processes until all occurrences of the character are removed.
Print the modified string.
Approach
Approach 1 − Using remove() and erase() function.
Approach 2 − Using remove_if() and erase() function.
Approach 3 − Using find() and erase() function.
There are several approaches to remove all occurrences of a character in a string using STL. Here some of the possible ways are listed below -
Approach 1: Using remove( ) and erase( ) function
Remove() algorithm is defined in
The function only moves the elements to the range's end and provides an iterator to the new end, it does not actually remove them from the container.
Erase() function in C++ STL is used to remove elements from the container. It takes two arguments depending on the type of container either vector or string.
The erase() function removes ‘count’ characters from the starting index. The first argument is an optional index, which is by default equal to 0. If ‘count’ is not specified it will erase all the characters starting from the index to the end of a string from the container.
Example
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string str = "hello world!"; char ch = 'l'; // Use remove() to remove all occurrences of the character. str.erase(remove(str.begin(), str.end(), ch), str.end()); cout << str << endl; return 0; }
Output
heo word!
Approach 2: Using remove_if( ) and erase( ) function
The ‘remove_if()’ in C++ STL is similar to the remove() function but it only removes the character from a container if it satisfies the specified condition.
remove_if() method removes all elements from the range [first, last) if it satisfies the condition p. The unary predicate p is a function or function object which takes a single argument from the elements of the container and returns a Boolean value indicating if the element should be deleted.
Example
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string str = "hello world!"; char ch = 'l'; str.erase(remove_if(str.begin(), str.end(), [=](char c) { return c == ch; }), str.end()); cout << str << endl; return 0; }
Output
heo word!
Approach 3: Using a loop and erase( ) function
In this approach, the idea is to use a loop to iterate through a string and remove each occurrence of a character one by one.
In this method, a for loop is used to iterate through the entire string, checking each character individually to see if it matches the character that needs to be deleted. If it matches, it will remove that character from the string; otherwise, it will move on to the next.
Example
#include <iostream> #include <string> using namespace std; int main() { string str = "hello world!"; char ch = 'o'; // Use a loop to remove all occurrences of the character for (int i = 0; i < str.length(); ) { if (str[i] == ch) { str.erase(i, 1); } else { ++i; } } cout << str << endl; return 0; }
Output
hell wrld!
Conclusion
In conclusion, the C++ STL library offers quick and simple procedures to eliminate every instance of a certain character from a string. With just a few lines of code, we can eliminate all occurrences of a specific character from a string using the STL's erase(),remove(), and remove_if() functions.
Utilising STL in C++ has many benefits, including ease of usage, efficiency, and reuse. Overall, it's a strong library that aids in producing reliable, efficient code.