首页 > 编程语言 >c++标准IO 中的string流

c++标准IO 中的string流

时间:2022-08-15 14:23:53浏览次数:76  
标签:word IO istringstream c++ words line string

c++标准IO 中的string流

sstream头文件

  • sstream头文件定义了三个类型来支持内存和string之间的IO,在用户看来,string类就像是一个IO流一样。

istringstream

  • 处理行内的多个单词(用空格分开),可以使用istringstream。以下代码将输入的一行单词缓冲,最后一并处理到vector中。
// 输入格式为: word1 word2 word3
void istringstream_test () {
    vector<string> words;
    string line, word;
    getline(cin, line);
    istringstream is(line); // 将istringstream绑定到line对象
    while (is >> word) {
        words.push_back(word);
    }
    vector<string>::iterator it = words.begin();
    while (it != words.end()) {
        cout << (*it) << endl;
        it++;
    }
}

ostringstream

  • 逐步构造输出,最后一起打印。以下代码对输入的多个单词进行判断分流,最后输出。
// 输入格式为: word1 word2 word3
void ostringstream_test () {
    vector<string> words;
    string line, word;
    getline(cin, line);
    istringstream is(line); // 将istringstream绑定到line对象
    while (is >> word) {
        words.push_back(word);
    }
    ostringstream os, boomOs;
    for (const string& entry: words) {
        if (entry == "123") {
            boomOs << "123boom" << ", ";
        }
        else {
            os << entry << ", ";
        }
    }
    cout << "os: " << os.str() << endl;
    cout << "boomOs: " << boomOs.str() << endl;
}

stringstream

  • 既可以读string数据,也可以向string写数据。

标签:word,IO,istringstream,c++,words,line,string
From: https://www.cnblogs.com/LeisureLak/p/16588145.html

相关文章

  • python3读csv文件,出现UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in
    使用csv.reader(file)读csv文件时,出现如下错误:UnicodeDecodeError:‘utf-8’codeccan’tdecodebyte0xd0inposition0:invalidcontinuationbyte出现原因:文件不是......
  • cookie,localStorage和sessionStorage的区别?
    区别一:存储数据大小不同1.cookie的存储数据大小在不能超过4kb,每个页面最多存储20个cookie2.localStorage能达到10mb,sessionStorage能达到5mb,虽然容量比cookie大,但是local......
  • conjunction
    junction[fromjungere;JOIN]aplacewhereoneroad,tracketcjoinsanother=intersectioncon-[Origin:com-]together;withConjunctionmayreferto:Con......
  • 前端好用API之MutationObserver
    前情一直以来都没有好的方式可以监听元素变化,Mutationevents虽然可以监听DOM树结构变化,但是因性能问题和差的兼容问题(Webkit内核不支持)并不推荐使用。MutationObserver......
  • 基于C++的OpenGL 10 之光照贴图
    1.引言本文基于C++语言,描述OpenGL的光照贴图前置知识可参考:基于C++的OpenGL09之材质-当时明月在曾照彩云归-博客园(cnblogs.com)笔者这里不过多描述每个名词......
  • Linux异常-java.io.IOException: 打开的文件过多
     异常报错如下09-Oct-201915:37:51.923严重[http-nio2-8080-Acceptor-0]org.apache.tomcat.util.net.Nio2Endpoint$Acceptor.runSocketacceptfailedjava.......
  • Option键使用技巧详解,提升Mac工作效率!
    众所周知快捷键可以提升日常效率,本文重点讲解Mac电脑上Option键是使用,进而加快自己电脑操作的速度。Option+访达当我们谈到macOS的时候,总是会下意识地把它与Windows......
  • 接口测试经典面试题:Session、cookie、token有什么区别?
    原文链接HTTP是一个没有状态的协议,这种特点带来的好处就是效率较高,但是缺点也非常明显,这个协议本身是不支持网站的关联的,比如https://ceshiren.com/和https://ceshiren.co......
  • Request.QueryString 的用法
    https://blog.csdn.net/dragon_ton/article/details/49464413?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogComme......
  • C++学习day1
    1.1 HelloWord的实现#include<iostream>usingnamespacestd;intmain(){cout<<"HelloWord"<<endl;system("pause");return0;} 1.2注释符号单行注......