2009年5月25日月曜日

文字列の大文字と小文字を変換する遊び

case.cc
#include <locale>
#include <string>

template<typename Elem>
std::basic_string<Elem> tolower(const std::basic_string<Elem>& x, const std::locale& loc = std::locale())
{
std::basic_string<Elem> r;
r.reserve(x.size());

typedef typename std::basic_string<Elem>::const_iterator iterator;
iterator itr = x.begin();
iterator end = x.end();
for(; itr != end; ++itr)
r.push_back(std::tolower(*itr, loc));

return r;
}

template<typename Elem>
std::basic_string<Elem> toupper(const std::basic_string<Elem>& x, const std::locale& loc = std::locale())
{
std::basic_string<Elem> r;
r.reserve(x.size());

typedef typename std::basic_string<Elem>::const_iterator iterator;
iterator itr = x.begin();
iterator end = x.end();
for(; itr != end; ++itr)
r.push_back(std::toupper(*itr, loc));

return r;
}

0 件のコメント:

コメントを投稿