C++ 格式化IO

cout举例

  • 可以通过配置一些标志位来格式化IO
  • 下面的代码中,改变了标志位之后,程序关闭之前,标志位都存在,要恢复默认状态,需要取消该标志位,如boolalphanoboolalpha
  • 代码

    #include<iostream>
    #include<exception>
    #include<fstream>
    #include<sstream>
    #include<string>
    #include<vector>
    #include<iterator>
    #include<list>
    #include<deque>
    #include<stack>
    #include<queue>
    #include<concurrent_priority_queue.h>
    #include<algorithm>
    #include<numeric>
    #include<functional>  //bind
    #include<map>
    #include<set>
    #include<unordered_map>
    #include<memory>
    #include<assert.h>
    #include<tuple>
    #include<regex>
    
    using namespace std;
    
    int main()
    {
        cout << "start!" << endl;
    
        cout << "default bool : " << true << ", " << false << endl;
        cout << "alpha bool : " << boolalpha << true << ", " << false << endl;
        cout << "after change bool : " << true << ", " << false << endl;
        cout << "default bool : " << noboolalpha << true << ", " << false << endl;
    
        cout << "end!" << endl;
        system("pause");
        return EXIT_SUCCESS;
    }
    

cin输入举例

  • noskipws可以忽略输入的空格以及后面的内容
  • 代码

    #include<iostream>
    #include<exception>
    #include<fstream>
    #include<sstream>
    #include<string>
    #include<vector>
    #include<iterator>
    #include<list>
    #include<deque>
    #include<stack>
    #include<queue>
    #include<concurrent_priority_queue.h>
    #include<algorithm>
    #include<numeric>
    #include<functional>  //bind
    #include<map>
    #include<set>
    #include<unordered_map>
    #include<memory>
    #include<assert.h>
    #include<tuple>
    #include<regex>
    
    using namespace std;
    
    int main()
    {
        cout << "start!" << endl;
    
        string str;
        while (cin >> str)
        {
            cout << str << endl;
        }
        //Ctrl+Z可以跳出此次输入的循环
    
        cin.clear(); //清空cin的状态
        cin >> noskipws;  //忽略cin中的空格
        while (cin >> str)
        {
            cout << str << endl;
        }
    
    
    
        cout << "end!" << endl;
        system("pause");
        return EXIT_SUCCESS;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

littletomatodonkey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值