2015年5月25日月曜日

マニピュレータ iomanip (C++)

C++の入出力で書式を設定するのに マニピュレータ を使うと便利である。iosヘッダまたはiomanipヘッダに定義された機能を利用できる。

//
// iomanip.cpp
// CplusplusPractice
//
// Created by masai on 2015/05/25.
// Copyright (c) 2015年 masai. All rights reserved.
//
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char* argv[]){
// 8進数で表示
cout << oct << 10 << endl;
// 1度設定した値は引き継がれることに注意
cout << 10 << endl;
// 10進数表示に変更
cout << dec << 10 << endl;
// + をつける
cout << showpos << 10 << endl;
// -は通常どおりやればつく
cout << -10 << endl;
// フィールド幅を10桁に設定(上記では、マニピュレータに()がついていない。引数をとらない場合は()をつけない。なぜなら、オーバーロードされたオペレータ<<に渡されるのがマニピュレータのアドレスであるため。)
cout << setw(10) << 100 << endl;
// 16進数
cout << hex << 100 << endl;
// 基数をつける
cout << showbase << 100 << endl;
}
view raw iomanip.cpp hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿