当前位置: 首页 > news >正文

c++11 标准模板(STL)(std::basic_istream)(九)

定义于头文件 <istream>
template<

    class CharT,
    class Traits = std::char_traits<CharT>

> class basic_istream : virtual public std::basic_ios<CharT, Traits>

 类模板 basic_istream 提供字符流上的高层输入支持。受支持操作包含带格式的输入(例如整数值或空白符分隔的字符与字符串)和无格式输入(例如未处理字符和字符数组)。此功能以通过 basic_ios 基类访问的底层 basic_streambuf 类所提供的接口实现。大多数库实现中, basic_istream 有一个非继承数据成员:用于存储 basic_istream::gcount() 所返回的值。


寻位

返回输入位置指示器

std::basic_istream<CharT,Traits>::tellg

pos_type tellg();

返回当前关联的 streambuf 对象的输入位置指示器。

表现为无格式输入函数 (UnformattedInputFunction) ,除了不影响 gcount() 。构造并检查 sentry 对象后,若 fail() == true ,则返回 pos_type(-1) 。否则,返回 rdbuf()->pubseekoff(0, std::ios_base::cur, std::ios_base::in) 。

参数

(无)

返回值

成功时为获取指针的当前位置,失败时为 pos_type(-1) 。

异常

若出现错误(错误状态标志不是 goodbit )并且设置了 exceptions() 为对该状态抛出则为 failure 。

若内部操作抛出异常,则捕获它并设置 badbit 。若对 badbit 设置了 exceptions() ,则重抛该异常。

调用示例

#include <iostream>
#include <string>
#include <sstream>int main()
{std::string str = "Hello, world";std::istringstream in(str);std::string word;in >> word;std::cout << "After reading the word \"" << word<< "\" tellg() returns " << in.tellg() << '\n';
}

输出

设置输入位置指示器

std::basic_istream<CharT,Traits>::seekg

basic_istream& seekg( pos_type pos );

basic_istream& seekg( off_type off, std::ios_base::seekdir dir);

设置当前关联 streambuf 对象的输入位置指示器,失败的情况下调用 setstate(std::ios_base::failbit) 。

进行任何其他动作前, seekg 清除 eofbit

(C++11 起)

seekg 表现为无格式输入函数 (UnformattedInputFunction) ,除了不影响 gcount() 。构造并检查 sentry 对象后,

1) 设置输入位置指示器为绝对(相对于文件起始)值 pos 。具体而言,执行 rdbuf()->pubseekpos(pos, std::ios_base::in) 。

2) 设置输入位置指示器为相对于 dir 所定义位置的 off 。具体而言,执行 rdbuf()->pubseekoff(off, dir, std::ios_base::in) 。

参数

pos-设置输入位置指示器到的绝对位置。
off-设置输入位置指示器到的相对位置。
dir-定义应用相对偏移到的基位置。它能为下列常量之一:
常量解释
beg流的开始
end流的结尾
cur流位置指示器的当前位置

返回值

*this

异常

若出现错误(错误状态标志不是 goodbit )并且设置了 exceptions() 为对该状态抛出则为 failure 。

若内部操作抛出异常,则捕获它并设置 badbit 。若对 badbit 设置了 exceptions() ,则重抛该异常。

调用示例

#include <iostream>
#include <string>
#include <sstream>int main()
{std::string str = "Hello, world";std::istringstream in(str);std::string word1, word2;in >> word1;in.seekg(0); // 回溯in >> word2;std::cout << "word1 = " << word1 << '\n'<< "word2 = " << word2 << '\n';return 0;
}

输出

 

http://www.lryc.cn/news/94895.html

相关文章:

  • OpenSource - Spring Startup Ananlyzer
  • ES6迭代器、Set、Map集合和async异步函数
  • mac android studio设置跟mac系统一样的快捷键
  • Java-通过IP获取真实地址
  • Java代码实现word转PDF
  • Java设计模式-简单工厂(Simple Factory)模式
  • 微软所有业务线梳理
  • SDN系统方法 | 1. 概述
  • 【数据分享】1929-2022年全球站点的逐日平均压力数据(Shp\Excel\12000个站点)
  • Profibus DP主站转Modbus TCP网关profibus从站地址范围
  • MySQL子查询
  • 学IT上培训班有用吗?
  • BI如何对接金蝶云星空数据源?奥威BI SaaS平台有绝招
  • 鼎镁科技冲刺A股上市失败,董事长涂季冰三年薪水超过6000万元
  • PostgreSQL【应用 02】扩展SQL之C语言函数(编写、编译、载入)实例分享
  • day37-框架
  • 基于STM32单片机的智能家居烟雾温度火灾防盗报警的设计与实现
  • jenkins 采用ssh方式连接gitlab连接不上
  • 前缀和模板算法
  • SpringBoot 启动输出 Git 版本信息(2023/07/11)
  • SSH客户端连接远程服务器
  • “深入理解Redis:高性能缓存与数据存储的秘密“
  • 【论文阅读笔记】Attack-Resistant Federated Learning with Residual-based Reweighting
  • DevOps B站学习版(二)
  • MySQL(一)基本架构、SQL语句操作、试图
  • MySQL事务基础知识
  • form表单禁止浏览器自动填充密码
  • ios oc button 设置
  • 山西电力市场日前价格预测【2023-07-17】
  • vue3功能实现