how to remove special characters from a string

#include <iostream>
#include <string>
#include <cctype>

int main() {
    std::string str = "Hello, World! This is a string with special characters.";

    for (int i = 0; i < str.size(); i++) {
        if (std::isalnum(str[i]) || str[i] == ' ') {
            std::cout << str[i];
        }
    }

    return 0;
}