吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 914|回复: 11
收起左侧

[学习记录] C++输入输出格式控制

  [复制链接]
Bachelor硕 发表于 2022-11-21 08:01
初学c++语言,感觉有点难。根据c++ STL学习的c++输入输出控制。分享顺便记录下。
#include <complex>
#include <limits>
#include <iostream> //输入输出流
#include <fstream>  //文件流
#include <strstream>//字符串流
#include <string>
#include <stdio.h>
#include <ios>

/*
* 小数尾追补0用showpoint
* 输出正整数时想带正负号使用showpos
* 输入隔离格式值skipws  作用:跳过分隔键(即空格或tab)
*/

using namespace std;

void example_setw(){
    char *f = "2345", *g = "678";
    cout << f << setw(6) << g << setw(4) <<f <<g << endl;
    cout << setw(10) << setfill('.') << "1234" << endl;
}

void all_example(){
    int i = 63;
    float pai = 3.14;
    cout.setf(ios::hex,ios::basefield);
    cout.setf(ios::showbase);
    cout << i << endl;
    cout.unsetf(ios::showbase);
    cout << i << endl;
    cout.setf(ios::uppercase);
    cout << i << endl;
    cout.setf(ios::showpoint);
    cout << pai << endl;
    cout.setf(ios::scientific,ios::floatfield);
    cout << pai << endl;
    cout.setf(ios::left,ios::adjustfield);
    cout.width(10);
    cout.fill('$');
    cout << "%%%" << endl;
}


int main(){
    example_setw();
    cout.setf(ios::hex);//设置为10进制输出
    cout.unsetf(ios::hex);//取消设置16进制输出
    float e = 2.71823;
    cout << setprecision(3) << e << endl;//设置float输出位数
    cout << setw(9) <<hex<<setiosflags(ios::showbase|ios::internal|ios::uppercase) << 15045<<endl;
    //showbase显示前缀符0x  internal产生中间对齐标志   uppercase将字符x变为大写
    all_example();
    return 0;
}
Library.cpp.zip (1.19 KB, 下载次数: 3)

免费评分

参与人数 4吾爱币 +4 热心值 +2 收起 理由
HWinZnieJ + 1 我很赞同!
ARKyuyan + 1 + 1 热心回复!
zshuim + 1 谢谢@Thanks!
luozi1653 + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
tangguo520 发表于 2022-11-21 08:52
提示: 作者被禁止或删除 内容自动屏蔽
Janling 发表于 2022-11-21 09:09
yangqing7145 发表于 2022-11-21 09:14
hutopower 发表于 2022-11-21 09:15
感谢分享,收藏了
神棍德 发表于 2022-11-21 09:17

感谢分享,
HWinZnieJ 发表于 2022-11-21 09:54
学习了,谢谢楼主~
apull 发表于 2022-11-21 10:02
感谢分享
zhongwenmingma 发表于 2022-11-21 10:59
感谢分享
lhp462 发表于 2022-11-21 11:05
谢谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-3-29 00:25

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表