c++ - C++ 把一个词换成另一个词
问题描述
我想写一个工具来把这个词换成另一个,我给!它已经匹配了某天,并且一直存在我的话被切断或有问题的问题。然后我想要一个使用 std :: regex 的更好的程序,但我不能再处理这个问题了..
#include <iostream>
#include <fstream>
void display_members()
{
std::string oldWord;
std::string newWord;
std::string tmp;
std::string getcontent;
std::ifstream openfile ("test", std::ios::in | std::ios::app);
std::cin >> oldWord;
std::cin >> newWord;
// if(openfile.is_open())
// {
while(! openfile.eof())
{
getline(openfile,tmp);
while((tmp.find(oldWord)) != std::string::npos)
{
tmp.replace(tmp.find(oldWord),newWord.length(),newWord);
}
std::cout << ","<<tmp << " " << " ";
// openfile >> getcontent;
// std::cout << getcontent<< " ";
}
// }
openfile.close();
}
int main()
{
display_members();
}
解决方案
尝试更多类似的东西:
#include <iostream>
#include <fstream>
void display_members()
{
std::string oldWord, newWord, tmp;
std::string::size_type pos;
std::cin >> oldWord;
std::cin >> newWord;
std::ifstream openfile("test");
while (getline(openfile, tmp))
{
pos = 0;
while ((pos = tmp.find(oldWord, pos)) != std::string::npos)
{
tmp.replace(pos, oldWord.length(), newWord);
pos += newWord.length();
}
std::cout << "," << tmp << " " << " ";
}
}
int main()
{
display_members();
}
推荐阅读
- data-analysis - 如何在python中标记编码?
- java - Toast.makeText 和 Log 未显示定位服务
- python - KeyError试图将字典中的元组中的项目逐项相乘?
- wpf - PasswordBox PasswordHelper 多重绑定
- jenkins - Jenkins 是否支持增量管道构建?
- ruby-on-rails - Rails 6 的 attr_protected 等效项?gem protected_attributes 需要 activemodel < 5.0,因此与 rails 6 不兼容
- rest - 我可以在没有 onBoard 的情况下获得计费协议 ID 吗?
- c++ - 运算符 C++ 的模棱两可的重载
- javascript - 使用dayjs的两次时间之间的差异
- r - 创建一个反应式 R 项目,该项目将根据 UI 中文本框中的选择更改使用数据集中的哪个变量