首页 > 编程语言 > 详细

C++标准库中setw的用法

时间:2014-03-03 16:13:04      阅读:749      评论:0      收藏:0      [点我收藏+]

在书中看到一段代码,使用到了std::setw()这个方法,记录一下用法。

 

这个方法用来设定输入或者输出流中的缓冲区的宽度(set field width).这个方法定义在头文件<iomanip>中。

可用来对齐输出的文本流或者截取部分数据。

 

示例1:

参考自cplusplus

www.cplusplus.com/reference/iomanip/setw/

bubuko.com,布布扣View Code

输出:

1
 

  

示例2:

参考自cppreference

http://en.cppreference.com/w/cpp/io/manip/setw

bubuko.com,布布扣
 1 #include <sstream>
 2 #include <iostream>
 3 #include <iomanip>
 4  
 5 int main()
 6 {
 7     std::cout << "no setw:" << 42 << \n
 8               << "setw(6):" << std::setw(6) << 42 << \n
 9               << "setw(6), several elements: " << 89 << std::setw(6) << 12 << 34 << \n;
10     std::istringstream is("hello, world");
11     char arr[10];
12     is >> std::setw(6) >> arr;
13     std::cout << "Input from \"" << is.str() << "\" with setw(6) gave \""
14               << arr << "\"\n";
15 }
View Code

输出:

1
2
3
4
no setw:42
setw(6):    42
setw(6), several elements: 89    1234
Input from "hello, world" with setw(6) gave "hello"

 

以上

C++标准库中setw的用法,布布扣,bubuko.com

C++标准库中setw的用法

原文:http://www.cnblogs.com/pansj/p/3577225.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!