[c++] How to remove all the occurrences of a char in c++ string

The algorithm std::replace works per element on a given sequence (so it replaces elements with different elements, and can not replace it with nothing). But there is no empty character. If you want to remove elements from a sequence, the following elements have to be moved, and std::replace doesn't work like this.

You can try to use std::remove (together with std::erase) to achieve this.

str.erase(std::remove(str.begin(), str.end(), 'a'), str.end());