大文字小文字変換

概要:std::stringの大文字小文字変換 std::toupper(), std::tolower()
ワンポイント:std::use_facetは呼び出しに負担がかかる場合がある

ソース:インターフェース
1
2
3
4
void uppercase(const std::ctype<TChar>& ct, std::string& source);
std::string uppercase_copy(const std::ctype<TChar>& ct, const std::string& source);
void lowercase(const std::ctype<TChar>& ct, std::string& source);
std::string lowercase_copy(const std::ctype<TChar>& ct, const std::string& source);

C++11

概要:

std::toupper(), std::tolower()

include:
  • <string>
  • <locale>
  • <algorithm>

ソース:実装例
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// @brief 大文字に変更
template< class TChar, class TTraits, class TAllocator > inline
void uppercase(const std::ctype<TChar>& ct, std::basic_string<TChar, TTraits, TAllocator>& source)
{
    std::for_each(std::begin(source), std::end(source), [&ct](TChar& c){ c = ct.toupper(c); });
}
/// @brief 大文字に変更したコピーを作成
template< class TChar, class TTraits, class TAllocator > inline
std::basic_string<TChar, TTraits, TAllocator> uppercase_copy(const std::ctype<TChar>& ct, const std::basic_string<TChar, TTraits, TAllocator>& source)
{
    std::basic_string<TChar, TTraits, TAllocator> destination;
    destination.resize(source.size());
    std::transform(std::cbegin(source), std::cend(source), destination.begin(), [&ct](TChar c){ return ct.toupper(c); });
    return std::move(destination);
}
/// @brief 小文字に変更
template< class TChar, class TTraits, class TAllocator > inline
void lowercase(const std::ctype<TChar>& ct, std::basic_string<TChar, TTraits, TAllocator>& source)
{
    std::for_each(std::begin(source), std::end(source), [&ct](TChar& c){ c = ct.tolower(c); });
}
/// @brief 小文字に変更したコピーを作成
template< class TChar, class TTraits, class TAllocator > inline
std::basic_string<TChar, TTraits, TAllocator> lowercase_copy(const std::ctype<TChar>& ct, const std::basic_string<TChar, TTraits, TAllocator>& source)
{
    std::basic_string<TChar, TTraits, TAllocator> destination;
    destination.resize(source.size());
    std::transform(std::cbegin(source), std::cend(source), destination.begin(), [&ct](TChar c){ return ct.tolower(c); });
    return std::move(destination);
}
ソース:使用例
1
2
3
4
5
6
7
8
9
void sample(const std::vector<std::string>& vstr)
{
     const std::locale& loc = std::locale();
     const std::ctype<char>& ct = std::use_facet< std::ctype<char> >(loc);
     for (e : vstr) {
             std::cout << uppercase(e, ct) << std::endl;
             std::cout << lowercase(e, ct) << std::endl;
     }
}

C++98

概要:

std::toupper(), std::tolower()

include:
  • <string>
  • <locale>
  • <algorithm>
  • <functional>

ソース:実装例
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/// @brief 大文字に変更するファンクター
template< class TChar >
class toupper_fun_t : public std::unary_function<TChar&, void>
{
public:
    explicit toupper_fun_t(const std::ctype<TChar>& ct) : ct_(ct){}
    void operator()(TChar& c) const
    {
        c = ct.toupper(c);
    }
private:
    const std::ctype<TChar>& ct_;
};
/// @brief 大文字に変更したコピーを返すファンクター
template< class TChar >
class toupper_copy_fun_t : public std::unary_function<TChar, TChar>
{
public:
    explicit toupper_copy_fun_t(const std::ctype<TChar>& ct) : ct_(ct){}
    TChar operator()(TChar c) const
    {
        return ct.toupper(c);
    }
private:
    const std::ctype<TChar>& ct_;
};
/// @brief 小文字に変更するファンクター
template< class TChar >
class tolower_fun_t : public std::unary_function<TChar&, void>
{
public:
    explicit tolower_fun_t(const std::ctype<TChar>& ct) : ct_(ct){}
    void operator()(TChar& c) const
    {
        c = ct.tolower(c);
    }
private:
    const std::ctype<TChar>& ct_;
};
/// @brief 小文字に変更したコピーを返すファンクター
template< class TChar >
class tolower_copy_fun_t : public std::unary_function<TChar, TChar>
{
public:
    explicit tolower_copy_fun_t(const std::ctype<TChar>& ct) : ct_(ct){}
    TChar operator()(TChar c) const
    {
        return ct.tolower(c);
    }
private:
    const std::ctype<TChar>& ct_;
};
/// @brief 大文字に変更
template< class TChar, class TTraits, class TAllocator > inline
void uppercase(const std::ctype<TChar>& ct, std::basic_string<TChar, TTraits, TAllocator>& source)
{
    std::for_each(source.begin(), source.end(), toupper_fun_t<TChar>(ct)());
}
/// @brief 大文字に変更したコピーを作成
template< class TChar, class TTraits, class TAllocator > inline
std::basic_string<TChar, TTraits, TAllocator> uppercase_copy(const std::ctype<TChar_>& ct, const std::basic_string<TChar, TTraits, TAllocator>& source)
{
    std::basic_string<TChar, TTraits, TAllocator> destination;
    destination.resize(source.size());
    std::transform(source.begin(), source.end(), destination.begin(), toupper_copy_fun_t<TChar>(ct)());
    return destination;
}
/// @brief 小文字に変更
template< class TChar, class TTraits, class TAllocator > inline
void lowercase(const std::ctype<TChar>& ct, std::basic_string<TChar, TTraits, TAllocator>& source)
{
    std::for_each(source.begin(), source.end(), tolower_fun_t<TChar>(ct)());
}
/// @brief 小文字に変更したコピーを作成
template< class TChar, class TTraits, class TAllocator > inline
std::basic_string<TChar, TTraits, TAllocator> lowercase_copy(const std::ctype<TChar_>& ct, const std::basic_string<TChar, TTraits, TAllocator>& source)
{
    std::basic_string<TChar, TTraits, TAllocator> destination;
    destination.resize(source.size());
    std::transform(source.cbegin(), source.cend(), destination.begin(), tolower_copy_fun_t<TChar>(ct)());
    return destination;
}

C99

概要:

toupper(), tolower()

include:
  • <ctype.h>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/// @brief 大文字に変更
void uppercase(char* psz)
{
    for (; *psz != NULL; ++psz) {
        *psz = toupper((int)psz);
    }
}
/// @brief 小文字に変更
void lowercase(char* psz)
{
    for (; *psz != NULL; ++psz) {
        *psz = tolower((int)psz);
    }
}