日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

有关替换字符的代码问题

發布時間:2024/6/30 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 有关替换字符的代码问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


代碼用來使用“,”替換空格。

#include <iostream> #include <fstream> #include <string> #include <stdlib.h> using namespace std; void main() {string file_path = "test.txt";//文件路徑string out_path = "ttttt.txt";//輸出路徑string str;string::size_type pos = 0;ifstream instream;ofstream outstream;instream.open(file_path);if (!instream)cout << "error" << endl;outstream.open(out_path);while (getline(instream,str)) {pos = str.find(" ");//查找字符在string中第一次出現的位置while (pos != string::npos)//判斷是否存在“hyu”這個字符 {str.replace(pos, 1, ",");//用,替換 . pos = str.find(" ", pos + 1);//查找剩余字符串 }outstream << str << endl;}instream.close();outstream.close();system("pause");}

?此代碼可行。

接下來的代碼則不可行。

void main() {string str=" ";string line;string::size_type pos = 0;string target=",";ifstream instream;ofstream outstream;instream.open("test.txt");outstream.open("ttttt.txt");while (getline(instream, line)) {pos = str.find(str);//查找字符在string中第一次出現的位置while (pos != string::npos)//?判斷有沒找到{line.replace(pos, str.size(), target);//替換字符串pos = line.find(str, pos + 1);//查找剩余匹配字符}outstream << str << endl;}instream.close();outstream.close();

?這一段代碼 會導致死循環 ,原因 個人猜測可能是應為空格符比較特殊的原因。

轉載于:https://www.cnblogs.com/Zerozzx/p/7436901.html

總結

以上是生活随笔為你收集整理的有关替换字符的代码问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。