###前提・実現したいこと
c++のstd::string型の文字列に含まれる改行コードのみを高速で取り除きたい
c++11, c++14の機能は使用せず実現したい
###発生している問題・エラーメッセージ
改行を含む文字ファイルをstd::stringに読み込み、findメソッドで改行コードを先頭から検索→見つかるたびにreplaceメソッドで空文字に置換するような処理を作成しました。
元データが10万行を超えるような長いものになるととても時間がかかるのですが、より良い方法はないでしょうか。
よろしくお願いいたします。
###該当のソースコード
c++
1//あらかじめstd::string targetStrに対象の文字列が読み込まれているものとする。 2const std::string CRLF = "\r\n"; 3const std::string CR = "\r"; 4const std::string LF = "\n"; 5std::string::size_type pos = 0; 6while(pos = targetStr.find(CRLF, pos), pos != std::string::npos) { 7 targetStr.replace(pos,CRLF.length(), ""); 8 pos += CRLF.length(); 9} 10pos = 0; 11while(pos = targetStr.find(CR, pos), pos != std::string::npos) { 12 targetStr.replace(pos,CR.length(), ""); 13 pos += CR.length(); 14} 15pos = 0; 16while(pos = targetStr.find(LF, pos), pos != std::string::npos) { 17 targetStr.replace(pos,LF.length(), ""); 18 pos += LF.length(); 19}
###補足情報(言語/FW/ツール等のバージョンなど)
cまたはc++
ただしc++11, c++14の機能は使用せず実現したい
回答6件
あなたの回答
tips
プレビュー
2016/05/08 15:08