69,371
社区成员
发帖
与我相关
我的任务
分享
#include <io ->STR<- eam>
#include <f ->STR<- eam>
using namespace std;
int main()
{
f ->STR<- eam fin("main.cpp", f ->STR<- eam::in);
->STR<- ing src = " ->STR<- ";
->STR<- ing target = " ->STR<- ";
->STR<- ing line;
while (getline(fin, line))
{
->STR<- ing::size_type pos = line.find(src); // 查找源字符串
while (pos != ->STR<- ing::npos) // 判断有没找到
{
line.replace(pos, src.size() , target); // 替换字符串
pos = line.find(src, pos + 1); // 查找剩余源字符串
}
cout << line << endl; // 输出替换后的
}
return 0;
}
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fin("main.cpp", fstream::in);
string src = "str";
string target = " ->STR<- ";
string line;
while (getline(fin, line))
{
string::size_type pos = line.find(src); // 查找源字符串
while (pos != string::npos) // 判断有没找到
{
line.replace(pos, src.size() , target); // 替换字符串
pos = line.find(src, pos + 1); // 查找剩余源字符串
}
cout << line << endl; // 输出替换后的
}
return 0;
}