C++ 参考手册

位置:首页 > C++ 参考手册 >具名要求 > C++ 具名要求: 字符特征 (CharTraits)

字符特征 (CharTraits) 是特性类,它抽象给定字符类型的基本字符和字符串操作。大多数标准库字符串类和输入/输出类要求伴随对应字符模板类型形参的字符特征 (CharTraits) 模板类型。

要求

字符特征 (CharTraits) 上的操作均不可抛出异常。

给定

  • CharT ,字符类型
  • XCharT字符特征 (CharTraits) 类型
  • c, dCharT 类型值
  • p, qconst CharT* 类型值
  • sCharT* 类型值
  • n, i, jstd::size_t 类型值
  • e, fX::int_type 类型值
  • posX::pos_type 类型值
  • stateX::state_type 类型值
  • rCharT 类型左值
表达式 返回类型 语义 复杂度
X::char_type CharT 用于指代字符类型 编译时
X::int_type 能保有 X::char_type 的所有合法值加 X::eof() 的类型 编译时
X::off_type X 为输入/输出类中的特性模板形参时,若此类型非 std::streamoff 则引起实现定义行为。 编译时
X::pos_type
  • 输入/输出类中的函数返回此类型,以 X::pos_type(X::off_type(-1)) 为提示错误的非法值
  • 以此类型的非法值为任何接收此类型值的 std::istreamstd::ostreamstd::streambuf 成员的实参是未定义行为
  • 在以 X 为输入/输出类中的特性模板形参时,若此类型非 std::streampos 则引起实现定义行为
编译时
X::state_type 可析构 (Destructible) 可复制赋值 (CopyAssignable) 可复制构造 (CopyConstructible) 可默认构造 (DefaultConstructible) 编译时
X::eq(c,d) bool 返回:是否把 c 当做等于 d 常数
X::lt(c,d) bool 返回:是否把 c 当做小于 d 常数
X::compare(p,q,n) int 返回:
  • 0 ,若对 [0,n) 中的每个 iX​::​eq(p[i], q[i]) 为 true
  • 否则为负值,若
    • [0,n) 中的某个 jX​::​lt(p[j], q[j]) 为 true ,且
    • [0,j) 中的每个 iX​::​eq(p[i], q[i]) 为 true
  • 否则为正值
线性
X::length(p) std::size_t 返回:使得 X​::​eq(p[i], CharT()) 为 true 的最小 i 线性
X​::​find(p,n,c) const X​::​char_type* 返回:
  • [p, p+n) 中的最小 q ,使得 X​::​eq(*q, c) 为 true
  • 否则为零
线性
X​::​move(s,p,n) X​::​char_type*
  • [0,n) 中的每个 i 进行 X​::​assign(s[i], p[i])
  • 即使范围 [p,p+n)[s,s+n) 重叠也能正确复制
  • 返回: s
线性
X​::​copy(s,p,n) X​::​char_type*
  • 要求: p 不在 [s,s+n)
  • 返回: s
  • [0,n) 中每个 i 进行 X​::​assign(s[i], p[i])
线性
X​::​assign(r,d) (不使用) 赋值 r = d 常量
X​::​assign(s,n,c) X​::​char_type*
  • [0,n) 中每个 i 进行 X​::​assign(s[i], c)
  • 返回: s
线性
X​::​not_eof(e) X​::​int_type 返回:
  • e ,若 X​::​eq_int_type(e, X​::​eof()) 为 false
  • 否则为使得 X​::​eq_int_type(f, X​::​eof()) 为 false 的值 f
常数
X​::​to_char_type(e) X​::​char_type 返回:
  • X​::​eq_int_type(e, X​::​to_int_type(c)) 对某个 c 为 true 则为 c
  • 否则为某未指定值
常数
X​::​to_int_type(c) X​::​int_type 返回: X::to_char_typeX::eq_int_type 的定义所制约的某值 e 常数
X​::​eq_int_type(e,f) bool
  • 对于所有 cdX​::​eq(c,d) 等于 X​::​eq_int_type(X​::​to_int_type(c), X​::​to_int_type(d))
  • 返回:
    • X​::​eq(c,d) ,若对于某 cde == X::to_int_type(c)f == X::to_int_type(d)
    • 否则为 true ,若 ef 都是 X​::​eof() 的副本
    • 否则为 false ,若 ef 之一为 X​::​eof() 的副本而另一者不是
    • 否则值未指定
常数
X​::​eof() X​::​int_type 返回:使得 X​::​eq_int_type(e, X​::​to_int_type(c)) 对于所有值 c 为 false 的值 e 常量

标准库

下列标准库类模板要求字符特征 (CharTraits) 作为模板类型形参:

存储并操作字符序列
(类模板)
只读的字符串视图
(类模板)
管理任意流缓冲
(类模板)
包装给定的抽象设备(std::basic_streambuf
并提供高层输入接口
(类模板)
实现高层文件流输入操作
(类模板)
实现高层字符串流输入操作
(类模板)
包装给定的抽象设备(std::basic_streambuf
并提供高层输出接口
(类模板)
实现高层文件流输出操作
(类模板)
实现高层字符串流输出操作
(类模板)
同步输出流的包装
(类模板)
包装给定的抽象设备(std::basic_streambuf
并提供高层输入/输出接口
(类模板)
实现高层文件流输入/输出操作
(类模板)
实现高层字符串流输入/输出操作
(类模板)
std::basic_istream 读取的输入迭代器
(类模板)
写入 std::basic_ostream 的输出迭代器
(类模板)
抽象原生设备
(类模板)
抽象原生文件设备
(类模板)
实现原生字符串设备
(类模板)
同步输出设备的包装
(类模板)
std::basic_streambuf 读取的输入迭代器
(类模板)
写入 std::basic_streambuf 的输出迭代器
(类模板)


下列标准库类满足字符特征 (CharTraits)

template<> class char_traits<char>;

template<> class char_traits<wchar_t>;
template<> class char_traits<char8_t>;
template<> class char_traits<char16_t>;

template<> class char_traits<char32_t>;


(C++20 起)
(C++11 起)
(C++11 起)