###前提・実現したいこと
abc
という文字列があります。
'a'
を'b'
に置換してbbc
に置換する事はできました。
'a'
を' '
に置換して bc
に置換する事はできました。
'a'
を''
に置換してbc
に置換する事はできませんでした。
どのようにプログラムを書けばbc
の結果を得ることが出来るのでしょうか?
また、(char)NULL
としている部分は''
とするとコンパイルエラーになるからです。
C2137 空の文字定数
というコンパイルエラーが発生します。
プログラム
cpp
1#include <iostream> 2#include <string> 3#include <algorithm> 4 5int main() 6{ 7 std::string str("abc"); 8 std::replace(str.begin(), str.end(), 'a', 'b'); 9 std::cout << "aをbに置換した結果: [" << str << "]" << std::endl; 10 11 std::string str2("abc"); 12 std::replace(str2.begin(), str2.end(), 'a', ' '); 13 std::cout << "aを に置換した結果: [" << str2 << "]" << std::endl; 14 15 std::string str3("abc"); 16 std::replace(str3.begin(), str3.end(), 'a', (char)NULL); 17 std::cout << "aを''に置換した結果: [" << str3 << "]" << std::endl; 18 19 std::cout << "欲しい置換結果: [" << "bc" << "]" << std::endl; 20 return 0; 21}
実行結果
txt
1aをbに置換した結果: [bbc] 2aを に置換した結果: [ bc] 3aを''に置換した結果: [ bc] 4欲しい置換結果: [bc]
- Windows 10 64bit 1703
- Visual Studio 2017 (v141)

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/27 12:43 編集
2017/09/27 12:41
2017/09/27 12:43