深入输入输出流(续)

深入输入输出流续

一、相关日志

输入输出流(一)

http://blog.163.com/zhoumhan_0351/blog/static/39954227201002945157577/

输入输出流(二)

http://blog.163.com/zhoumhan_0351/blog/static/39954227201003005237697/

输入输出流(三)

http://blog.163.com/zhoumhan_0351/blog/static/39954227201003095620780/

C++ I/O流技术

http://blog.163.com/zhoumhan_0351/blog/static/3995422720103171303182/

C++流类库与C的I/O操作优势比较,文件与流

http://blog.163.com/zhoumhan_0351/blog/static/399542272010030103055465/

C++基础概念(基本语法)

http://blog.163.com/zhoumhan_0351/blog/static/39954227201002192139554/

深入输入输出流

http://blog.163.com/zhoumhan_0351/blog/static/39954227201032392545410/

二、深入输入输出流

1、成员函数

如下是流格式控制表:

表1

表2

表3

表4

表5

2、操作算子(Manipulators)

表6 不带参数的

表7 带参数的

可以用 | 来迭加使用。

When using setw( ) with an output stream, the output expression is formatted into a 

temporary string that is padded with the current fill character if needed, as determined 

by comparing the length of the formatted result to the argument of setw( ). In other 

words, setw( ) affects the result string of a formatted output operation. Likewise, using setw( ) with input streams only is meaningful when reading strings,

#include <cassert>

#include <cmath>

#include <iomanip>

#include <limits>

#include <sstream>

#include <string>

#include "iostream"

using namespace std;

int main() {

  istringstream is("one 2.34 five");

  string temp;

  is >> setw(2) >> temp;

  assert(temp == "on");

  is >> setw(2) >> temp;//空格读取截止

  assert(temp == "e");

  double x;

  is >> setw(2) >> x;

  double relerr = fabs(x - 2.34) / x;

  assert(relerr <= numeric_limits<double>::epsilon());

  cout<<numeric_limits<double>::epsilon();//在该类型下所能描述的最小数

} ///:~

    由上可见,读取数字时,不能用setw来设置读取字符的个数。对于输入流,setw只能用于字符串的读取。

3、创建操作算子

A zero-argument manipulator such as endlis simply a function that takes 

as its argument an ostream reference and returns an ostream reference. 

The declaration for endl is

ostream& endl(ostream&);

应用算子(将一个函数应用到一个流类)来负责函数地址的调用,如下例:

ostream& ostream::operator<<(ostream& (*pf)(ostream&)) {

  return pf(*this);

}

When a function such as *pf (that takes a stream parameter and returns a 

stream reference) is inserted into a stream, this applicator function is 

called, which in turn executes the function to which pf points. Applicators 

for ios_base, basic_ios, basic_ostream, and basic_istream are 

predefined in the Standard C++ library.

#include <iostream>

using namespace std; 

ostream& nl(ostream& os) {

  return os << '\n';

}

int main() {

  cout << "newlines" << nl << "between" << nl

       << "each" << nl << "word" << nl;

} ///:~

这样仅仅换行,而不涮新流。

4、效用算子

An effector is a simple class whose constructor formats a string representing the 

desired operation, along with an overloaded operator<< to insert that string into a 

stream. 

5、宽字符流

template<class charT, class traits = char_traits<charT> >

class basic_istream {...};

typedef basic_istream<char> istream;

typedef basic_istream<wchar_t> wistream;

typedef basic_ifstream<char> ifstream;

typedef basic_ifstream<wchar_t> wifstream;

typedef basic_istringstream<char> istringstream;

typedef basic_istringstream<wchar_t> wistringstream;

但是系统提供给char类型和wchar_t类型的字符处理函数的名称不相同。如strcmp和wcscmp函数。如何无差别的调用相应的函数呢?

解决方法是:

The solution is to factor out the differences into a new abstraction. 

The operations you can perform on characters have been abstracted into 

the char_traits template, which has predefined specializations for char 

and wchar_t. To compare two strings, then, basic_string just calls 

traits::compare( ) (remember that traits is the second template 

parameter), which in turn calls either strcmp( ) or wcscmp( ), depending 

on which specialization is being used (transparent to basic_string).

Since you don’t know when you’re writing the template which type 

of stream you have, you need a way to automatically convert character 

literals to the correct size for the stream. This is the job of the widen( ) 

member function. The expression widen('-'), for example, converts its argument to L’-’ (the literal syntax equivalent to the conversion wchar_t(‘-’)) if the stream is a wide stream and leaves it alone otherwise. There is also a narrow( ) function that converts to a char if needed.

前面nl的通用版本:

template<class charT, class traits>

basic_ostream<charT,traits>&

nl(basic_ostream<charT,traits>& os) {

  return os << charT(os.widen('\n'));

}

6、区域性字符流

各个区域的国家的输出显示分别定义不同的格式。这次的抽象是locale.

流成员函数imbue()改变一个流使用的区域。

#include <iostream>

#include <locale>

#include <string>

using namespace std;

int main() {

  // Change to French/France

  locale loc("french");

  cout.imbue(loc);

  string currency =

    use_facet<moneypunct<char> >(loc).curr_symbol();

  char point =

    use_facet<moneypunct<char> >(loc).decimal_point();

  cout << "I made " << currency << 12.34 << " today!"

       << endl;

} ///:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值