我有一个脚本,它从输入文件中获取一个单词并在输出文件中输出自定义单词。
例如,在输入文件中我有单词 ccccccaaaaaannnnndlllllleeeee
,我需要将它编辑为看起来像蜡烛并在其他文件中输出。那么你能给我举个例子吗?
到目前为止我有:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string vards;
std::ifstream input_file("virkne3.in");
input_file >> vards;
std::ofstream output_file("virkne3.out");
output_file << vards.erase(std::unique(vards.begin(), vards.end()), vards.end());
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}
最佳答案
假设您的意思是连续 重复的字母,std::unique
就是这样做的。
vards.erase(std::unique(vards.begin(), vards.end()), vards.end());
关于c++ - 用一个字母替换字符串中每个重复字母的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7895383/