C++ 标准库之 iomanip 、操作符 ios::fixed 以及 setprecision 使用的惨痛教训经验总结
本菜鸡自从退役之后就再也没怎么敲过 C++ 代码,在 C++ 语言下,求解关于浮点数类型的问题时,之前有碰到类似的情况,但是似乎都没有卡这块的数据,基本上用一个 setprecision 函数保留几位有效数字就 AC 了。但这次在计算任意五个数的平均值时卡在了一组数据上,问题如下:
#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
int main(){
float a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
float ave = (a+b+c+d+e)*1.0/5;
//cout<<setiosflags(ios::fixed)<<setprecision(2)<<ave*1.0<<endl;
cout<<setprecision(2)<<ave*1.0<<endl;
//printf("%.2f",ave);
return 0;
}
/*
* Problem: 连续输入5个数,数的范围为0.00~2.00,输出其平均值,并保留两位小数。
*
**/
/*
用例:
1.82 1.86 1.88 1.65 1.78
对应输出应该为:
1.80
你的输出为:
1.8
*
**/
我们从头到尾来看看这段代码吧。
首先是头文件:#include ,我们可能没太见过,老实说我也是第一次见,以前都是用 C++ 那个总的头文件 #include<bits/stdc++.h> ,包含了全部的C++头文件,所以很多小的头文件可能都不太记得。
关于 bits/stdc++.h 的源代码如下:
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
include 是 I/O 流控制头文件,类似与 C 里面的格式化输出一样,记住就好,具体的一些操作符及作用可以参考下表所示。
| 操作符 | 作用 |
|---|---|
| dec | 设置整数为十进制 |
| hex | 设置整数为十六进制 |
| oct | 设置整数为八进制 |
| setbase(n) | 设置整数为n进制(n=8,10,16) |
| setfill(n) | 设置字符填充,c可以是字符常或字符变量 |
| setprecision(n) | 设置浮点数的有效数字为n位 |
| setw(n) | 设置字段宽度为n位 |
| setiosflags(ios::fixed) | 设置浮点数以固定的小数位数显示 |
| setiosflags(ios::scientific) | 设置浮点数以科学计数法表示 |
| setiosflags(ios::left) | 输出左对齐 |
| setiosflags(ios::right) | 输出右对齐 |
| setiosflags(ios::skipws) | 忽略前导空格 |
| setiosflags(ios::uppercase) | 在以科学计数法输出E与十六进制输出X以大写输出,否则小写 |
| setiosflags(ios::showpos) | 输出正数时显示"+"号 |
| setiosflags(ios::showpoint) | 强制显示小数点 |
| resetiosflags() | 终止已经设置的输出格式状态,在括号中应指定内容 |
浮点数但是我们要记住的一点是,一个浮点数的有效数字位数默认为为 6 位,你可以通过 setprecision(n) 操作符来修改显示有效数字的有效数字的位数。但我们需要注意以下两个重要的易错点:
- 如果有效数少于要显示的数字,则 setprecision 将舍去
- 末尾的零将被省略
那我们如果想要根据我们自己的意愿输出小数点后相应的位数,我们又该怎么办呢?
C++ 在 iostream 头文件中定义了一个 ios::fixed 操作符,它可以使输出数据用小数点的形式打印在屏幕上。这样我们就可以人为的控制输出自己想保留小数点后相应的位数。
setiosflags(ios::fixed) 是定义在 中的函数,该操作符的作用是执行有参数指定区域内的动作,我们传入了参数 ios::fixed ,该参数指定的动作是以带小数点的形式表示浮点数,并且在允许的精度范围内尽可能的把数字移向小数点右侧。
例如我们还是拿上面那个例子来说:
cout<<ave*1.0<<endl; (1)
cout<<setprecision(2)<<ave*1.0<<endl; (2)
cout<<setiosflags(ios::fixed)<<ave*1.0<<endl; (3)
cout<<setiosflags(ios::fixed)<<setprecision(2)<<ave*1.0<<endl; (4)
根据上面所描述的那样,我们很容易得出如下结果:
(1) = 1.798
(2) = 1.8
(3) = 1.798000
(4) = 1.80
C++ 标准库之 iomanip 、操作符 ios::fixed 以及 setprecision 使用的惨痛教训经验总结的更多相关文章
- C++ 标准库之iomanip
C++ 标准库之iomanip istream & istream::get(char *, int, char = '\n');istream & istream::getline( ...
- 一起学习Boost标准库--Boost.texical_cast&format库
今天接续介绍有关字符串表示相关的两个boost库: lexical_cast 将数值转换成字符串 format 字符串输出格式化 首先,介绍下lexical_cast ,闻其名,知其意.类似C中的at ...
- 什么是 C 和 C ++ 标准库?
简要介绍编写C/C ++应用程序的领域,标准库的作用以及它是如何在各种操作系统中实现的. 我已经接触C++一段时间了,一开始就让我感到疑惑的是其内部结构:我所使用的内核函数和类从何而来? 谁发明了它们 ...
- 什么是 C 和 C ++ 标准库?学编程的你应该知道这些知识!
简要介绍编写C/C ++应用程序的领域,标准库的作用以及它是如何在各种操作系统中实现的. 我已经接触C++一段时间了,一开始就让我感到疑惑的是其内部结构:我所使用的内核函数和类从何而来? 谁发明了它们 ...
- [转]什么是 C 和 C ++ 标准库?
转载地址:https://www.cnblogs.com/findumars/p/9000371.html 简要介绍编写C/C ++应用程序的领域,标准库的作用以及它是如何在各种操作系统中实现的.我已 ...
- c++ io标准库2
转自:http://www.2cto.com/kf/201110/109445.html 接下来我们来学习一下串流类的基础知识,什么叫串流类? 简单的理解就是能够控制字符串类型对象进行输入输出的类,C ...
- std 与标准库
1.命名空间std C++标准中引入命名空间的概念,是为了解决不同模块或者函数库中相同标识符冲突的问题.有了命名空间的概念,标识符就被限制在特定的范围(函数)内,不会引起命名冲突.最典型的例子就是st ...
- C++标准库概述
一.C++标准库的主要组件: 1.标准C库 2.I/O流技术(对标准输入输出设备称为标准I/O,对在外磁盘上文件的输入输出称为文件I/O,对内存中指定的字符串存储空间的输入输出称为串I/O) 3.st ...
- 《挑战30天C++入门极限》C++的iostream标准库介绍(3)
C++的iostream标准库介绍(3) C语言提供了格式化输入输出的方法,C++也同样,但是C++的控制符使用起来更为简单方便,在c++下有两中方法控制格式化输入输出. 1.有流对象的成员函 ...
随机推荐
- 图解ARP协议(三)ARP防御篇-如何揪出“内鬼”并“优雅的还手”
一.ARP防御概述 通过之前的文章,我们已经了解了ARP攻击的危害,黑客采用ARP软件进行扫描并发送欺骗应答,同处一个局域网的普通用户就可能遭受断网攻击.流量被限.账号被窃的危险.由于攻击门槛非常低, ...
- 线程的私有领地 ThreadLocal
从名字上看,『ThreadLocal』可能会给你一种本地线程的概念印象,可能会让你联想到它是一个特殊的线程. 但实际上,『ThreadLocal』却营造了一种「线程本地变量」的概念,也就是说,同一个变 ...
- JVM(二)Java虚拟机组成详解
导读:详细而深入的总结,是对知识"豁然开朗"之后的"刻骨铭心",想忘记都难. Java虚拟机(Java Virtual Machine)下文简称jvm,上一篇我 ...
- Centos 7 Puppet之foreman介绍安装测试
一.简介 1.前言(引用一下网上的资料) 随着企业的 Linux 系统数量越来越多,管理问题便成为一个相对麻烦并需要急 迫解决的问题,这里有 2 个 Key Message:1)统一管控体系非常重要, ...
- javascript基础修炼(10)——VirtualDOM和基本DFS
1. Virtual-DOM是什么 Virtual-DOM,即虚拟DOM树.浏览器在解析文件时,会将html文档转换为document对象,在浏览器环境中运行的脚本文件都可以获取到它,通过操作docu ...
- webpack4.0各个击破(4)—— Javascript & splitChunk
目录 一. Js模块化开发 二. Js文件的一般打包需求 三. 使用webpack处理js文件 3.1 使用babel转换ES6+语法 3.2 脚本合并 3.3 公共模块识别 3.4 代码分割 3.5 ...
- vb.net 分割byte数组的方法SplitBytes
以下代码随手写的 并没有大量测试 效率也有待提升 如果需要C#的请自行转换 Function SplitBytes(Data As Byte(), Delimiter As Byte()) As Li ...
- JQuery官方学习资料(译):CSS
JQuery提供了一个处理方法,可以获取或设置元素的CSS属性. // 获取 CSS 属性 $( "h1" ).css( "fontSize" ); / ...
- .NET LINQ 实现跨数据库数据的整合
如果要在不同的数据库之间,要把数据整合到一起,或者对数据进行统计分析的话,实现起来比较麻烦. 一般情况下我们第一时间想到的方法是通过前置机实现,在前置机上安装一个数据库和同步数据程序,定时的把数据同步 ...
- CSS深入理解流体特性和BFC特性下多栏自适应布局
一.块状元素的流体特性与自适应布局 块状元素像放在容器中的水流一样,内容区域会随着margin, padding, border的出现自动填满剩余空间,这就是块状元素的流体特性. 来一个小实验: di ...