これはできません。あなたの代わりに何ができるか、それはより便利そうでない場合よりも、物事を行うことができ、すなわちマニピュレータがしか糖衣構文いわゆるある、すなわち
std::cout << toggle(str);
std::cin >> toggle(str);
引数として文字列を取るマニピュレータは、あります。例えば、
std::cout << std::setw(5) << x <<;
は
std::cout.width(5);
std::cout << x;
と同じ操作を行うが、それは他の<<
の操作と一緒に連鎖させることを可能にするように、より便利であるだろう。
これで、必要なもの(大文字と小文字を入れ替える)のフォーマットがサポートされていないため、そのための構文的な砂糖を提供する方法もありません。
しかし、マニピュレータがあなたの文字列を引数として取ることができれば、もちろん、マニピュレータの標準的な実装方法で実装したいものを実現できます。例えば、
struct toggle_output
{ std::string const&str; }
inline toggle_output toggle(std::string const&str)
{ return {str}; }
inline std::ostream& operator<<(std::ostream&out, toggle_output const&t)
{
for(auto c:t.str)
if (std::islower(c)) out<<std::toupper(c);
else if(std::isupper(c)) out<<std::tolower(c);
else out<<c;
return out;
}
struct toggle_input
{ std::string &str; }
inline toggle_input toggle(std::string&str)
{ return {str}; }
inline std::istream& operator>>(std::istream&in, toggle_input &t)
{
in >> t.str;
for(auto&c:t.str)
if (std::islower(c)) c=std::toupper(c);
else if(std::isupper(c)) c=std::tolower(c);
return in;
}
はまた
(混乱を避けるために)必要があるかもしれない
inline std::ostream& operator<<(std::ostream&out, toggle_input const&t)
{ return out<<toggle_output(t.str); }
http://stackoverflow.com/questions/21358150/c-changing-lower-case-letters-in-a-string-function-againおよびhttp://stackoverflow.com/questions/313970をご覧ください。/how-to-convert-stdstring-to-lower-case –
@payamsbr、リンクは私の質問に答えません。私が知りたいのは、マニピュレータを作成する方法です。私はちょうど例としてトグル文字列を言及しました。 – user7140484