0

Erasing characters from string

Please can anyone tell me how did this work? #include<iostream> #include<algorithm> using namespace std; main() { string my_str = "ABAABACCABA"; cout << "Initial string: " << my_str << endl; my_str.erase(remove(my_str.begin(), my_str.end(), 'A'), my_str.end()); //remove A from string cout << "Final string: " << my_str; }

24th Oct 2020, 1:03 PM
Md. Tayebur Rahim
Md. Tayebur Rahim - avatar
2 Réponses
+ 2
std::remove() does not remove the value, but shift values. Value that's not going to be removed is shift to left, the other is right. It returns an iterator to the element that follows the last element not to be removed. std::remove() can be used in any containers. In your case, string for example: Original: ABAABACCABA std::remove(): BBCCBAAAAA iterator ^ string::erase() erases a position (1 iterator or value) or a range (2 iterators) from a string. If for the start iterator, we use the iterator std::remove() returns, and the second use std::string::end(), then BBCCBAAAAA ^ ^ ^ ^ ^ will be removed.
24th Oct 2020, 1:53 PM
你知道規則,我也是
你知道規則,我也是 - avatar