擦除用户从读输入文件中输入的字符串

Erase a string of characters entered by user from a read input file

本文关键字:输入 字符串 文件 擦除 用户      更新时间:2023-10-16

假设我有一个输入文件和一个输出文件。当我打开输入文件时,它的内容是这样的:

Happy birthday to everyone!

然后我输入一个字符串,我想从读取的数据中删除该字符串,并将其写入输出文件。例如,如果我删除'at',我将得到这个:

Hppy birhdy o everyone!

我如何使用str.erase或其他字符串方法来做到这一点?

#include <string>
#include <algorithm>
static bool is_a_t(char c) { return c == 'a' || c == 't'; }
std::string in("Happy birthday to everyone!");
in.erase(std::remove_if(in.begin(), in.end(), is_a_t), in.end());
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
std::string get_processed_copy(const std::string& src, const std::string& remove) {
    std::string dst;
    std::remove_copy_if(src.begin(), src.end(), std::back_inserter(dst),
        [&](char c) {
            return remove.find(c) != remove.npos;
        });
    return dst;
}
int main() {
    std::cout << get_processed_copy("Happy birthday to everyone!", "at") << std::endl;
}