std::char_traits
来自cppreference.com
|
|
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
| 在头文件 <string> 中定义
|
||
| template< class CharT |
||
。
char_traits类定义的字符类型,如用于操作为给定的字符类型的字符和字符的字符串,以及所有的共同操作的类型的流和字符串操作属性.原文:
The
char_traits class defines the stream and string operation properties of a character type, such as the types used for manipulating the characters and character strings, as well as all the common operations for the given character type.
。有类定义的模板
char_traits,作为显式实例化的基础。它满足所有要求的Traits概念.原文:
There is class template
char_traits defined, which serves as a basis for explicit instantiations. It fulfills all requirements of Traits concept.。此外,一些专业被定义为最常见的字符类型,必须指定以下成员。
原文:
Also, several specializations are defined for most common character types which which has to specify the following members:
| Instantiation | char_type
|
int_type
|
off_type
|
pos_type
|
state_type
|
|---|---|---|---|---|---|
| char_traits<char> | char | int | streamoff | streampos | mbstate_t |
| char_traits<wchar_t> | wchar_t | wint_t | wstreamoff | wstreampos | mbstate_t |
| char_traits<char16_t> (C++11) | char16_t | int_least16_t | streamoff | u16streampos | mbstate_t |
| char_traits<char32_t> (C++11) | char32_t | int_least32_t | streamoff | u32streampos | mbstate_t |
| 本章尚未完成 原因:simplify the description, emphasize that char_traits can be user defined |
目录 |
[编辑] 。会员类型。
| 。类型。
|
Definition |
char_type
|
CharT
|
int_type
|
。整数类型,可以容纳所有值
char_type加EOF。原文: an integer type that can hold all values of char_type plus EOF |
off_type
|
。 “实现自定义”。
|
pos_type
|
。 “实现自定义”。
|
state_type
|
。 “实现自定义”。
|
[编辑] 。成员函数。
| [静态的]</div></div>
|
指定一个字符 (公共静态成员函数) |
| [静态的]</div></div>
|
比较两个字符 (公共静态成员函数) |
| [静态的]</div></div>
|
到另一个移动一个字符序列 原文: moves one character sequence onto another (公共静态成员函数) |
| [静态的]</div></div>
|
复制的字符序列 (公共静态成员函数) |
| [静态的]</div></div>
|
按字典顺序比较两个字符的序列 原文: lexicographically compares two character sequences (公共静态成员函数) |
| [静态的]</div></div>
|
返回一个字符序列的长度 原文: returns the length of a character sequence (公共静态成员函数) |
| [静态的]</div></div>
|
finds a character in a character sequence (公共静态成员函数) |
| [静态的]</div></div>
|
转换为等效的 int_typechar_type原文: converts int_type to equivalent char_type(公共静态成员函数) |
| [静态的]</div></div>
|
转换为等效的 char_typeint_type原文: converts char_type to equivalent int_type(公共静态成员函数) |
| [静态的]</div></div>
|
比较两个 int_type值(公共静态成员函数) |
| [静态的]</div></div>
|
返回一个“EOF”值 (公共静态成员函数) |
| [静态的]</div></div>
|
检查字符是否是“EOF”值 原文: checks whether a character is eof value (公共静态成员函数) |
[编辑] 。为例。
。用户定义的性格特征,可用于提供case-insensitive comparison。
原文:
User-defined character traits may be used to provide case-insensitive comparison
#include <string> #include <iostream> #include <cctype> struct ci_char_traits : public std::char_traits<char> { static bool eq(char c1, char c2) { return std::toupper(c1) == std::toupper(c2); } static bool ne(char c1, char c2) { return std::toupper(c1) != std::toupper(c2); } static bool lt(char c1, char c2) { return std::toupper(c1) < std::toupper(c2); } static int compare(const char* s1, const char* s2, size_t n) { while( n-- != 0 ) { if( std::toupper(*s1) < std::toupper(*s2) ) return -1; if( std::toupper(*s1) > std::toupper(*s2) ) return 1; ++s1; ++s2; } return 0; } static const char* find(const char* s, int n, char a) { while( n-- > 0 && std::toupper(*s) != std::toupper(a) ) { ++s; } return s; } }; typedef std::basic_string<char, ci_char_traits> ci_string; std::ostream& operator<<(std::ostream& os, const ci_string& str) { return os.write(str.data(), str.size()); } int main() { ci_string s1 = "Hello"; ci_string s2 = "heLLo"; if(s1 == s2) std::cout << s1 << " and " << s2 << " are equal\n"; }
输出:
Hello and heLLo are equal
[编辑] 。另请参阅。
| 存储和处理的字符序列 原文: stores and manipulates sequences of characters (类模板) | |