char_traits struct 初步学习

本文介绍了C++中的char_traits结构体,它定义了字符的特性并为字符操作提供语义。char_traits用于basic_string和iostream类,提供如eq、lt、compare等方法,允许重载以改变字符串的属性,如大小写敏感性。此外,char_traits隐藏了不同字符集的细节,如EOF值。通过traits,模板编程可以更加通用和灵活。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在学习文件读取时碰到文件尾字符 EOF 的问题,顺藤摸瓜找到 char_traits

msdn 上关于 EOF 的说明,就一句话:

Returns the end-of-file (EOF) character.

在注意中提到一句:

A value that represents end of file (such as EOF or WEOF).

意思是 EOF 表示的是文件尾的一个值,对于不同的字符集会对应不同的数值,但对用户来说就隐藏了这种细节。在给出的示例中也说明了这一点:

// char_traits_eof.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main()
{
    using namespace std;

    char_traits<char>::char_type ch1 = 'x';
    char_traits<char>::int_type int1;
    int1 = char_traits<char>::to_int_type(ch1);
    cout << "char_type ch1 is '" << ch1 << "' and corresponds to int_type "
         << int1 << "." << endl << endl;

    char_traits<char>::int_type int2 = char_traits<char>::eof();
    cout << "The eof marker for char_traits<char> is: " << int2 << endl;

    char_traits<wchar_t>::int_type int3 = char_traits<wchar_t>::eof();
    cout << "The eof marker for char_traits<wchar_t> is: " << int3 << endl;
}
输出:

char_type ch1 is 'x' and corresponds to int_type 120.

The eof marker for char_traits<char> is: -1
The eof marker for char_traits<wchar_t> is: 65535

可以看到 char_traits 是模板类,在 cplusplus 上给出的解释是:

Character traits classes specify character properties and provide specific semantics for certain operations on characters and sequences of characters.

就是说 character traits 实例化了字符的各种性质(包括 eof 的数值,int_type, off_type, pos_type, state_type 等的 typedef),并提供对某些在字符和字符序列上特定操作的语义(语法?)。

简而言之,就是其中的 char_traits<char> 既是对字符集的实例化。

msdn 上给出的说明

template <
   class CharType
> struct char_traits;

The char_traits struct describes attributes associated with a character.

The template struct describes various character traits for type CharType. The template class basic_string as well as several iostream template classes, including basic_ios, use this information to manipulate elements of type CharType.

模板构造描述了 CharType 类型的各种特性,得到这种类型特性之后,就可以用这些信息来操作 CharType 类的元素。


还有相关博文:

char_traits

摘要:c++标准库中,有一个string,这个相信大家都知道,其实它是一个basic_string的一个typedef,其实在 msdn 这些地方,查 string 查不到什么太多东西,需要了解内部接口,得查basic_string
至于char_traits,是base_string的一个模板参数,它主要负责关于字符的属性和方法,譬如 eq、lt、compare、find 这些,比较字符大小,查找字符等等,这个类有什么用呢?
可以重载来改变string的一些字符相关的内部属性,譬如大小写敏感、字符串比较这些,具体的接口,而且,里面的接口基本上都是 static ,可以类似 strcpy、strlen 这样的接口直接使用(这个好像意义不大:P)

C++的Char traits模板类

摘要:字符特性模板,目的是提供最基本的字符特性的统一的方法函数。

C++ traits学习笔记(一)

摘要:traits是服务于泛型编程的,其目的是让模板更加通用,同时把一些细节向普通的模板用户隐藏起来。当用不同的类型去实例化一个模板时,不可避免有些类型会存在一些与众不同的属性,若考虑这些特性的话,可能会导致形成的模板不够“泛型”或是过于繁琐,而traits的作用是把这些特殊属性隐藏起来,从而实现让模板更加通用。


如有错误,恳请指正

boost::property_tree::read_xml(strTmp, pt);加入这个严重性 代码 说明 项目 文件 行 禁止显示状态 详细信息 错误 LNK2019 无法解析的外部符号 "void __cdecl boost::throw_exception(class std::exception const &,struct boost::source_location const &)" (?throw_exception@boost@@YAXAEBVexception@std@@AEBUsource_location@1@@Z),函数 "void __cdecl boost::property_tree::xml_parser::read_xml<class boost::property_tree::basic_ptree<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class boost::property_tree::basic_ptree<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &,int,class std::locale const &)" (??$read_xml@V?$basic_ptree@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@property_tree@boost@@@xml_parser@property_tree@boost@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEAV?$basic_ptree@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@12@HAEBVlocale@4@@Z) 中引用了该符号 D:\SAMA\sama\build\msvc-Debug\sama D:\SAMA\sama\build\msvc-Debug\copidfunction.cpp.obj 1 错误 LNK1120 1 个无法解析的外部命令 D:\SAMA\sama\build\msvc-Debug\sama D:\SAMA\sama\out\MSVC-Debug\sama.exe 1
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值