-----------------------------------A Tour of C++ Chapter8-------------------------------------------

--------------------------Chapter8 I/O流------------------------

1、I/O流提供了文本和数值的输入输出功能,这种输入输出是带缓冲的,可以是格式化的,也可以是未格式化的。

2、ostream对象将有类型的对象转换为一个字符(字节)流;istream对象将一个字符(字节)转换为有类型的对象。

3、可以用getline()来读取一整行(包括结束的换行符)。

   例:

void hello_line()
{
    cout << "Please input your name\n";
    string str;
    getline(cin, str);
    cout << "Hello, " << str << "!\n";}

4、用户自定类型的I/O

  例:先自定义一个简单类型

struct Entry
{
    string name;
    int number;
}

运算符重载:

ostream& operator<<(ostream& os, const Entry& e)
{
    return os << "{\"" << e.name << "\","  << e.number << "}";
}

一个用户自定义的输出运算符接受它的输出流(的引用)作为第一个参数,输出完毕后,返回此流的引用。

5、文件流:在fstream中,标准库提供了从文件读取数据以及向文件写入数据的流

  • ifstream用于从文件读取数据。
  • ofstream用于向文件写入数据。
  • fstream用于读写文件

6、字符串流:在<sstream>中,标准库提供了从string读取数据以及向string写入数据的流:

  • istringstream用于从string读取数据。
  • ostringstream用于向string写入数据。
  • stringstream用于读写string。

例:

void test()
{
    ostringstream oss;
    oss << "{temprature," << scientific << 123.456789 << "}";
    cout << oss.str() << endl;

istringstream中的内容可以通过调用str()成员函数来获取。

时间: 2024-10-13 09:04:41

-----------------------------------A Tour of C++ Chapter8-------------------------------------------的相关文章

F - Free DIY Tour(动态规划)

这道题也可以用深搜做,可以深搜本来就不熟,好久没做早忘了,明天看看咋做的 Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free

HDU 1853 Cyclic Tour(最小费用最大流)

Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1879    Accepted Submission(s): 938 Problem Description There are N cities in our country, and M one-way roads connecting them. Now L

UVALive 4848 Tour Belt

F - Tour Belt Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 4848 Description Korea has many tourist attractions. One of them is an archipelago (Dadohae in Korean), a cluster of small islands sca

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

Codeforces 490F Treeland Tour(离散化 + 线段树合并)

题目链接 Treeland Tour 题目就是让你求树上LIS 先离散化,然后再线段树上操作.一些细节需要注意一下. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i) #define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N =

hdoj 3488 Tour 【最小费用最大流】【KM算法】

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2299    Accepted Submission(s): 1151 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 3000

HDU3488 Tour [有向环覆盖 费用流]

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3159    Accepted Submission(s): 1525 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000

POJ 1637 Sightseeing tour (混合图欧拉回路)

Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tour by bus in Lund, so that tourists can see every corner of the beautiful city. They want to construct the tour so that every street in the city is visit

A Swifr Tour

Tradition suggests that the first program in a new language should print the words "Hello ,world!" on the screen. In Swift , this can be done in a single line :  print("Hello world") If you gave written code in C otr Objective - C , th

Codeforces Round #279 (Div. 2) F. Treeland Tour(lis+dfs)

题目链接: huangjing 题意:告诉一个无向无环图,然后求在联通的路上的lis. 思路:枚举起点求lis 复杂度是n^2logn,貌似这复杂度对时间有点玄,估计是数据有点弱... 首先枚举起点,然后求lis,主要是用dfs求的,要用到回溯的思想,我觉得是只要更新了,就要保存那个操作,然后一旦这一次的搜索完成,那么就要立即回复g数组的值,因为有很多不同的路线,所以一条路走完后,就要回复以前的状态哦,免得影响另外一条路..我觉得尽管他们都说是暴力,但是我觉得这个题还是蛮好的... 题目: F.