c++ - streampos 和 pos_type、streamoff 和 off_type 有什么区别?

标签 c++

streampos 之间有什么区别?和 pos_type , streamoffoff_type , 除了它们的定义不同。我应该将 basic_stream<>::seek 与什么一起使用?的功能?

最佳答案

std::basic_istreamstd::basic_ostream两者都采用两种模板类型,CharTTraits .给定一个派生自基本流之一的 A 类,Traits数据类型可以检索为

A::traits_type

根据C++标准的§21.2,该数据类型必须提供以下成员类型:

char_type // must be identical to CharT of the basic-stream
off_type
pos_type

(以及一些与当前问题无关的其他数据类型)。鉴于 way the std::basic_istream<>::seekg() method is defined , off_type 的预期含义和 pos_type是:

  • pos_type用于流中的绝对位置
  • off_type用于相对位置

所以如果你想使用绝对版本的seekg() ,您应该声明的数据类型是 A::pos_type (与 A::traits_type::pos_type 相同)。对于相对版本,它是 A::off_type .

关于 std::streamposstd::streamoff : 这些也由标准定义为用于 traits_type默认 版本的数据类型.换句话说,如果您没有明确指定 Traits模板参数,A::pos_type 事实上会是std::streampos , 和 A::off_type 事实上会是std::streamoff .

如果您创建自己的 Traits 版本 并希望将它与标准库模板一起使用,例如 std::basic_istream<>等等,您必须包含 pos_type 的类型定义和 off_type (以及许多其他数据类型),并确保它们符合标准的 §27.2.2 和 §27.3。

关于c++ - streampos 和 pos_type、streamoff 和 off_type 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10133680/

相关文章:

c++ - 在类中声明自定义比较器时类型/值不匹配

c++ - 如何在 Visual Studio 2015 调试中将 '%' 符号作为命令行参数 (argv) 传递?

c++ - 指定容器类型的迭代器类型的部分特化

c++ - std::vector 内存处理

c++ - 嵌套的 Switch 语句 - 返回到 switch 的开头

C++如何避免使用自制的侵入式列表的友元模板函数

c++ - Turbo C++ 时间分辨率高达毫秒

c++ - 删除某些节点

c++ - 二维 CUDA 中值滤波器优化

c++ - 为什么我收到错误 : interrupt service routine should have ‘unsigned long int’ as the second argument?