string[meta header]
std[meta namespace]
class template[meta id-type]
namespace std {
template <class charT > struct char_traits ;
template <> struct char_traits <char >;
template <> struct char_traits <char8_t >; // C++20から
template <> struct char_traits <char16_t >; // C++11から
template <> struct char_traits <char32_t >; // C++11から
template <> struct char_traits <wchar_t >;
}
char_traits
は、basic_string
文字列クラスのおける、各要素の特性を使用するためのクラスである。
このクラスと同じインタフェースのクラスを用意してbasic_string
クラスのテンプレート引数として指定することにより、ユーザー独自の処理に切り替えることが可能である。たとえば、大文字・小文字を区別しない比較のためのカスタマイズに利用できる。
テンプレートパラメータは以下を意味する:
charT
: basic_string
クラスが扱う文字型
名前
説明
対応バージョン
char_type
文字コンテナの実装で使用される文字型
int_type
文字に対応する値を表す数値型
off_type
文字の差を表す型
pos_type
位置を表す型
state_type
ストリームのマルチバイト文字の変換の状態を表す型
comparison_category
比較カテゴリ
C++20
名前
説明
対応バージョン
char_type
char
int_type
int
off_type
streamoff
pos_type
streampos
state_type
mbstate_t
comparison_category
strong_ordering
C++20
char8_tによる特殊化のメンバ型定義 (C++20)
名前
説明
対応バージョン
char_type
char8_t
C++20
int_type
unsigned int
C++20
off_type
streamoff
C++20
pos_type
u8streampos
C++20
state_type
mbstate_t
C++20
comparison_category
strong_ordering
C++20
char16_tによる特殊化のメンバ型定義 (C++11)
名前
説明
対応バージョン
char_type
char16_t
C++11
int_type
uint_least16_t
C++11
off_type
streamoff
C++11
pos_type
u16streampos
C++11
state_type
mbstate_t
C++11
comparison_category
strong_ordering
C++20
char32_tによる特殊化のメンバ型定義 (C++11)
名前
説明
対応バージョン
char_type
char32_t
C++11
int_type
uint_least32_t
C++11
off_type
streamoff
C++11
pos_type
u32streampos
C++11
state_type
mbstate_t
C++11
comparison_category
strong_ordering
C++20
名前
説明
対応バージョン
char_type
wchar_t
int_type
wint_t
off_type
streamoff
pos_type
wstreampos
state_type
mbstate_t
comparison_category
strong_ordering
C++20
#include < iostream>
#include < string>
int main ()
{
// std::stringと同じ
std::basic_string<char , std::char_traits<char >> a = " aaa" ;
std::basic_string<char , std::char_traits<char >> b = " bbb" ;
// char_traits::compare()で比較が行われる
if (a < b) {
std::cout << " then" << std::endl;
}
else {
std::cout << " else" << std::endl;
}
}
std::char_traits[color ff0000]
std::basic_string[link basic_string.md]
大文字・小文字を区別しない文字列比較をするbasic_string
を定義 (C++20)
#include < string>
#include < compare>
#include < cctype>
#include < algorithm>
template <class T >
class case_insensitive_char_traits {
using base_traits = std::char_traits<T>;
public:
using char_type = char ;
using int_type = int ;
using off_type = std::streamoff;
using pos_type = std::streampos;
using state_type = std::mbstate_t ;
using comparison_category = std::weak_ordering;
static constexpr void assign (char_type& c1, const char_type& c2) noexcept {
base_traits::assign (c1, c2);
}
static constexpr bool eq (char_type c1, char_type c2) noexcept {
return base_traits::eq (std::tolower (c1), std::tolower (c2));
}
static constexpr bool lt (char_type c1, char_type c2) noexcept {
return base_traits::lt (std::tolower (c1), std::tolower (c2));
}
static constexpr int compare (const char_type* s1, const char_type* s2, size_t n) {
auto order = std::lexicographical_compare_three_way (s1, s1 + n, s2, s2 + n,
[](char_type c1, char_type c2) -> std::weak_ordering {
return std::tolower (c1) <=> std::tolower (c2);
});
return order == std::weak_ordering::equivalent ? 0 :
order == std::weak_ordering::greater ? 1 :
-1 ;
}
static constexpr size_t length (const char_type* s) {
return base_traits::length (s);
}
static constexpr const char_type* find (const char_type* s, size_t n,
const char_type& a) {
return std::find_if (s, s + n, [a](char_type c) {
return std::tolower (c) == a;
});
}
static constexpr char_type* move (char_type* s1, const char_type* s2, size_t n) {
return base_traits::move (s1, s2, n);
}
static constexpr char_type* copy (char_type* s1, const char_type* s2, size_t n) {
return base_traits::copy (s1, s2, n);
}
static constexpr char_type* assign (char_type* s, size_t n, char_type a) {
return base_traits::assign (s, n, a);
}
};
#include < iostream>
int main () {
using case_insensitive_string =
std::basic_string<char , case_insensitive_char_traits<char >>;
case_insensitive_string a = " AbC" ;
case_insensitive_string b = " aBc" ;
if (a == b) {
std::cout << " equal" << std::endl;
}
else {
std::cout << " not equal" << std::endl;
}
}
std::weak_ordering[link /reference/compare/weak_ordering.md]
std::lexicographical_compare_three_way[link /reference/algorithm/lexicographical_compare_three_way.md]
std::find_if[link /reference/algorithm/find_if.md]