Home  >  Article  >  Backend Development  >  Remove occurrences of all characters from string using STL

Remove occurrences of all characters from string using STL

WBOY
WBOYforward
2023-08-28 17:49:071306browse

Remove occurrences of all characters from 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 by Ming Lee and Alexander Stepanov in the early 1990s.

It mainly consists of three components: container, algorithm and iterator. Containers are objects that store and manipulate data, such as lists, vectors, sets, maps, and stacks. Algorithms are functions that operate on data stored in containers, such as searching, sorting, and manipulating data. Iterators are objects that make it easy to navigate through the elements of a container.

STL has become an important part of competitive programming and it also 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 characters to be deleted. Then store them in variables.

  • Loop through each character in the string.

  • Check whether the current character matches the character to be deleted.

  • Repeat the above two processes until all occurrences of characters are removed.

  • Print the modified string.

method

  • Method 1 - Use remove() and erase() functions.

  • Method 2 - Use remove_if() and erase() functions.

  • Method 3 - Use find() and erase() functions.

There are several ways to remove all occurrences of characters from a string using STL. Some possible approaches are listed below -

Method 1: Use remove() and erase() functions

Remove() The algorithm is defined in the header file. It removes the value from the range, in this case it will be the character you want to remove, and returns the iterator to the new end of the sequence. 算法>

This function only moves the elements to the end of the range and provides an iterator for the new end, it does not actually remove them from the container.

The

Erase() function in C STL is used to delete an element from a container. It takes two parameters, depending on the type of container (vector or string).

erase() function deletes "count" characters from the starting index. The first parameter is an optional index, equal to 0 by default. If "count" is not specified, it will remove all characters in the container from the index to the end of the string.

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!

Method 2: Use remove_if( ) and erase( ) functions

'remove_if()' in C STL Similar to the remove() function, but it only removes characters from the container when the specified conditions are met.

remove_if() Method removes all elements in the range [first, last) if the condition p is met. The unary predicate p is a function or function object that takes a single argument from an element of the container and returns a Boolean value indicating whether the element should be removed.

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!

Method 3: Using loops and erase() function

In this method, the idea is to use a loop to iterate over the string and remove each occurrence of characters one by one.

In this method, use a for loop to loop through the entire string, checking each character individually to see if it matches the character that needs to be removed. If there is a match, the character is removed from the string; otherwise, it proceeds to the next one.

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!

in conclusion

In summary, the C STL library provides a fast and simple procedure to eliminate every instance of a character in a string. With just a few lines of code, we can eliminate all occurrences of a specific character from a string using STL's erase(), remove(), and remove_if() functions.

There are many benefits to using STL in C, including ease of use, efficiency, and reuse. Overall, it is a powerful library that helps generate reliable and efficient code.

The above is the detailed content of Remove occurrences of all characters from string using STL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete