C++
ストリームマニピュレータ
サーチ…
前書き
マニピュレータは、 operator >>またはoperator <<を使用して入力ストリームと出力ストリームを制御するのに役立つ特別なヘルパー関数です。
それらは#include <iomanip>によってすべて含めることができます。
備考
マニピュレータは他の方法で使用することができます。例えば:
-
os.width(n);os << std::setw(n);と等しくなりos << std::setw(n);
is.width(n);is >> std::setw(n);と等しくなりis >> std::setw(n);
-
os.precision(n);os << std::setprecision(n);と等しくなりos << std::setprecision(n);
is.precision(n);is >> std::setprecision(n);と等しくなりis >> std::setprecision(n);
-
os.setfill(c);os << std::setfill(c);と等しくなりos << std::setfill(c);
-
str >> std::setbase(base);またはstr << std::setbase(base);等しい
str.setf(base == 8 ? std::ios_base::oct :
base == 10 ? std::ios_base::dec :
base == 16 ? std::ios_base::hex :
std::ios_base::fmtflags(0),
std::ios_base::basefield);
-
os.setf(std::ios_base::flag);os << std::flag;と等しくなりos << std::flag;
is.setf(std::ios_base::flag);is >> std::flag;等しくなりis >> std::flag;
os.unsetf(std::ios_base::flag);os << std::no ## flag;と等しくなりos << std::no ## flag;
is.unsetf(std::ios_base::flag);is >> std::no ## flag;と等しいis >> std::no ## flag;
( ## - 連結演算子 )
次のflags:boolalpha、showbase、showpoint、showpos、skipws、uppercase。
-
std::ios_base::basefield。
flagsの場合:dec、hex、oct:
-
os.setf(std::ios_base::flag, std::ios_base::basefield);os << std::flag;と等しくなりos << std::flag;
is.setf(std::ios_base::flag, std::ios_base::basefield);is >> std::flag;等しくなりis >> std::flag;
(1) -
str.unsetf(std::ios_base::flag, std::ios_base::basefield);str.setf(std::ios_base::fmtflags(0), std::ios_base::basefield);
(2)
-
std::ios_base::adjustfield。
flagsの場合:left、right、internal:
-
os.setf(std::ios_base::flag, std::ios_base::adjustfield);os << std::flag;と等しくなりos << std::flag;
is.setf(std::ios_base::flag, std::ios_base::adjustfield);is >> std::flag;等しくなりis >> std::flag;
(1) -
str.unsetf(std::ios_base::flag, std::ios_base::adjustfield);str.setf(std::ios_base::fmtflags(0), std::ios_base::adjustfield);
(2)
(1)既に設定されている対応するフィールドのフラグがunsetfによって既に設定解除されているunsetf 。
(2) flagがセットされている場合。
-
std::ios_base::floatfield。
-
os.setf(std::ios_base::flag, std::ios_base::floatfield);os << std::flag;と等しくなりos << std::flag;
is.setf(std::ios_base::flag, std::ios_base::floatfield);is >> std::flag;等しくなりis >> std::flag;
flagのために:fixed、scientific。 -
os.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield);os << std::defaultfloat;と等しくなりos << std::defaultfloat;
is.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield);is >> std::defaultfloat;と等しいis >> std::defaultfloat;
-
str.setf(std::ios_base::fmtflags(0), std::ios_base::flag);str.unsetf(std::ios_base::flag)と同じstr.unsetf(std::ios_base::flag)
flagsの場合:basefield、adjustfield、floatfield
-
os.setf(mask)は、os << setiosflags(mask);と同じos << setiosflags(mask);
is.setf(mask)は次のものis >> setiosflags(mask);同じis >> setiosflags(mask);
os.unsetf(mask)はos << resetiosflags(mask);os.unsetf(mask)と等しくなりos << resetiosflags(mask);
is.unsetf(mask)は次のようになりis >> resetiosflags(mask);
ほぼすべてのstd::ios_base::fmtflagsmaskです。
ストリームマニピュレータ
std::boolalphaとstd::noboolalpha - ブール値のテキスト表現と数値表現を切り替えます。
std::cout << std::boolalpha << 1;
// Output: true
std::cout << std::noboolalpha << false;
// Output: 0
bool boolValue;
std::cin >> std::boolalpha >> boolValue;
std::cout << "Value \"" << std::boolalpha << boolValue
<< "\" was parsed as " << std::noboolalpha << boolValue;
// Input: true
// Output: Value "true" was parsed as 0
std::showbaseとstd::noshowbase - 数値ベースを表すプレフィックスを使用するかどうかを制御します。
std::dec (decimal)、 std::hex (hexadecimal)、 std::oct (octal)は、整数の底を変更するために使用されます。
#include <sstream>
std::cout << std::dec << 29 << ' - '
<< std::hex << 29 << ' - '
<< std::showbase << std::oct << 29 << ' - '
<< std::noshowbase << 29 '\n';
int number;
std::istringstream("3B") >> std::hex >> number;
std::cout << std::dec << 10;
// Output: 22 - 1D - 35 - 035
// 59
デフォルト値はstd::ios_base::noshowbaseとstd::ios_base::decです。
std::istringstream詳細については、< sstream >ヘッダーを参照してください。
std::uppercaseとstd::nouppercase - 浮動小数点と16進整数の出力に大文字を使用するかどうかを制御します。入力ストリームには影響しません。
std::cout << std::hex << std::showbase
<< "0x2a with nouppercase: " << std::nouppercase << 0x2a << '\n'
<< "1e-10 with uppercase: " << std::uppercase << 1e-10 << '\n'
}
// Output: 0x2a with nouppercase: 0x2a
// 1e-10 with uppercase: 1E-10
デフォルトはstd::nouppercaseです。
std::setw(n) - 次の入出力フィールドの幅を正確にnます。
いくつかの関数が呼び出されると、widthプロパティnは0にリセットされ0 (フルリストはここにあります )。
std::cout << "no setw:" << 51 << '\n'
<< "setw(7): " << std::setw(7) << 51 << '\n'
<< "setw(7), more output: " << 13
<< std::setw(7) << std::setfill('*') << 67 << ' ' << 94 << '\n';
char* input = "Hello, world!";
char arr[10];
std::cin >> std::setw(6) >> arr;
std::cout << "Input from \"Hello, world!\" with setw(6) gave \"" << arr << "\"\n";
// Output: 51
// setw(7): 51
// setw(7), more output: 13*****67 94
// Input: Hello, world!
// Output: Input from "Hello, world!" with setw(6) gave "Hello"
デフォルトはstd::setw(0)です。
std::left 、 std::rightおよびstd::internal - std::ios_base::adjustfieldをstd::ios_base::left 、 std::ios_base::rightおよびstd::ios_base::internal設定して、塗りつぶし文字のデフォルト位置を変更するstd::ios_base::internal対応します。 std::leftとstd::rightは任意の出力に適用され、 std::internal - 整数、浮動小数点および金銭出力の場合。入力ストリームには影響しません。
#include <locale>
...
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << std::left << std::showbase << std::setfill('*')
<< "flt: " << std::setw(15) << -9.87 << '\n'
<< "hex: " << std::setw(15) << 41 << '\n'
<< " $: " << std::setw(15) << std::put_money(367, false) << '\n'
<< "usd: " << std::setw(15) << std::put_money(367, true) << '\n'
<< "usd: " << std::setw(15)
<< std::setfill(' ') << std::put_money(367, false) << '\n';
// Output:
// flt: -9.87**********
// hex: 41*************
// $: $3.67**********
// usd: USD *3.67******
// usd: $3.67
std::cout << std::internal << std::showbase << std::setfill('*')
<< "flt: " << std::setw(15) << -9.87 << '\n'
<< "hex: " << std::setw(15) << 41 << '\n'
<< " $: " << std::setw(15) << std::put_money(367, false) << '\n'
<< "usd: " << std::setw(15) << std::put_money(367, true) << '\n'
<< "usd: " << std::setw(15)
<< std::setfill(' ') << std::put_money(367, true) << '\n';
// Output:
// flt: -**********9.87
// hex: *************41
// $: $3.67**********
// usd: USD *******3.67
// usd: USD 3.67
std::cout << std::right << std::showbase << std::setfill('*')
<< "flt: " << std::setw(15) << -9.87 << '\n'
<< "hex: " << std::setw(15) << 41 << '\n'
<< " $: " << std::setw(15) << std::put_money(367, false) << '\n'
<< "usd: " << std::setw(15) << std::put_money(367, true) << '\n'
<< "usd: " << std::setw(15)
<< std::setfill(' ') << std::put_money(367, true) << '\n';
// Output:
// flt: **********-9.87
// hex: *************41
// $: **********$3.67
// usd: ******USD *3.67
// usd: USD 3.67
デフォルトはstd::leftです。
std::fixed 、 std::scientific 、 std::hexfloat [C ++ 11]およびstd::defaultfloat [C ++ 11] - 浮動小数点入出力の書式を変更します。
std::fixedはstd::ios_base::floatfieldをstd::ios_base::fixedに設定します。
std::scientific - std::ios_base::scientific 、
std::hexfloat - std::ios_base::fixed | std::ios_base::scientificおよび
std::defaultfloat - std::ios_base::fmtflags(0)ます。
fmtflags
#include <sstream>
...
std::cout << '\n'
<< "The number 0.07 in fixed: " << std::fixed << 0.01 << '\n'
<< "The number 0.07 in scientific: " << std::scientific << 0.01 << '\n'
<< "The number 0.07 in hexfloat: " << std::hexfloat << 0.01 << '\n'
<< "The number 0.07 in default: " << std::defaultfloat << 0.01 << '\n';
double f;
std::istringstream is("0x1P-1022");
double f = std::strtod(is.str().c_str(), NULL);
std::cout << "Parsing 0x1P-1022 as hex gives " << f << '\n';
// Output:
// The number 0.01 in fixed: 0.070000
// The number 0.01 in scientific: 7.000000e-02
// The number 0.01 in hexfloat: 0x1.1eb851eb851ecp-4
// The number 0.01 in default: 0.07
// Parsing 0x1P-1022 as hex gives 2.22507e-308
デフォルトはstd::ios_base::fmtflags(0)です。
いくつかのコンパイラにはバグがあります
double f;
std::istringstream("0x1P-1022") >> std::hexfloat >> f;
std::cout << "Parsing 0x1P-1022 as hex gives " << f << '\n';
// Output: Parsing 0x1P-1022 as hex gives 0
std::showpointとstd::noshowpoint - 小数点が常に浮動小数点表現に含まれるかどうかを制御します。入力ストリームには影響しません。
std::cout << "7.0 with showpoint: " << std::showpoint << 7.0 << '\n'
<< "7.0 with noshowpoint: " << std::noshowpoint << 7.0 << '\n';
// Output: 1.0 with showpoint: 7.00000
// 1.0 with noshowpoint: 7
デフォルトはstd::showpointです。
std::showposとstd::noshowpos - 非負の出力に+記号の表示を制御します。入力ストリームには影響しません。
std::cout << "With showpos: " << std::showpos
<< 0 << ' ' << -2.718 << ' ' << 17 << '\n'
<< "Without showpos: " << std::noshowpos
<< 0 << ' ' << -2.718 << ' ' << 17 << '\n';
// Output: With showpos: +0 -2.718 +17
// Without showpos: 0 -2.718 17
std::noshowpos場合はデフォルトです。
std::unitbuf 、 std::nounitbuf - すべての操作の後に出力ストリームをフラッシュするように制御します。入力ストリームには影響しません。 std::unitbufはフラッシュを引き起こします。
std::setbase(base) - ストリームの数値ベースを設定します。
std::setbase(8)はstd::ios_base::basefieldをstd::ios_base::octに設定することと等しく、
std::setbase(16) - std::ios_base::hex 、
std::setbase(10) - std::ios_base::decます。
場合base 、他のある8 、 10又は16次にstd::ios_base::basefieldするために設定されてstd::ios_base::fmtflags(0)これは、小数出力と接頭辞依存入力を意味します。
デフォルトのstd::ios_base::basefieldはstd::ios_base::dec 、デフォルトではstd::setbase(10)です。
std::setprecision(n) - 浮動小数点精度を変更します。
#include <cmath>
#include <limits>
...
typedef std::numeric_limits<long double> ld;
const long double pi = std::acos(-1.L);
std::cout << '\n'
<< "default precision (6): pi: " << pi << '\n'
<< " 10pi: " << 10 * pi << '\n'
<< "std::setprecision(4): 10pi: " << std::setprecision(4) << 10 * pi << '\n'
<< " 10000pi: " << 10000 * pi << '\n'
<< "std::fixed: 10000pi: " << std::fixed << 10000 * pi << std::defaultfloat << '\n'
<< "std::setprecision(10): pi: " << std::setprecision(10) << pi << '\n'
<< "max-1 radix precicion: pi: " << std::setprecision(ld::digits - 1) << pi << '\n'
<< "max+1 radix precision: pi: " << std::setprecision(ld::digits + 1) << pi << '\n'
<< "significant digits prec: pi: " << std::setprecision(ld::digits10) << pi << '\n';
// Output:
// default precision (6): pi: 3.14159
// 10pi: 31.4159
// std::setprecision(4): 10pi: 31.42
// 10000pi: 3.142e+04
// std::fixed: 10000pi: 31415.9265
// std::setprecision(10): pi: 3.141592654
// max-1 radix precicion: pi: 3.14159265358979323851280895940618620443274267017841339111328125
// max+1 radix precision: pi: 3.14159265358979323851280895940618620443274267017841339111328125
// significant digits prec: pi: 3.14159265358979324
デフォルトはstd::setprecision(6)です。
std::setiosflags(mask)とstd::resetiosflags(mask) - std::ios_base::fmtflags型のmaskで指定されたフラグをセットしてクリアする。
#include <sstream>
...
std::istringstream in("10 010 10 010 10 010");
int num1, num2;
in >> std::oct >> num1 >> num2;
std::cout << "Parsing \"10 010\" with std::oct gives: " << num1 << ' ' << num2 << '\n';
// Output: Parsing "10 010" with std::oct gives: 8 8
in >> std::dec >> num1 >> num2;
std::cout << "Parsing \"10 010\" with std::dec gives: " << num1 << ' ' << num2 << '\n';
// Output: Parsing "10 010" with std::oct gives: 10 10
in >> std::resetiosflags(std::ios_base::basefield) >> num1 >> num2;
std::cout << "Parsing \"10 010\" with autodetect gives: " << num1 << ' ' << num2 << '\n';
// Parsing "10 010" with autodetect gives: 10 8
std::cout << std::setiosflags(std::ios_base::hex |
std::ios_base::uppercase |
std::ios_base::showbase) << 42 << '\n';
// Output: OX2A
std::skipwsとstd::noskipws - フォーマットされた入力関数による先頭の空白のスキップを制御します。出力ストリームには影響しません。
#include <sstream>
...
char c1, c2, c3;
std::istringstream("a b c") >> c1 >> c2 >> c3;
std::cout << "Default behavior: c1 = " << c1 << " c2 = " << c2 << " c3 = " << c3 << '\n';
std::istringstream("a b c") >> std::noskipws >> c1 >> c2 >> c3;
std::cout << "noskipws behavior: c1 = " << c1 << " c2 = " << c2 << " c3 = " << c3 << '\n';
// Output: Default behavior: c1 = a c2 = b c3 = c
// noskipws behavior: c1 = a c2 = c3 = b
デフォルトはstd::ios_base::skipwsです。
std::quoted(s[, delim[, escape]]) [C ++ 14] - スペースを埋め込んだ引用符付きの文字列を挿入または抽出します。
s - 挿入または抽出する文字列
delim - デリミタとして使用する文字"を指定します。
escape -エスケープ文字として使用する文字\デフォルトでは。
#include <sstream>
...
std::stringstream ss;
std::string in = "String with spaces, and embedded \"quotes\" too";
std::string out;
ss << std::quoted(in);
std::cout << "read in [" << in << "]\n"
<< "stored as [" << ss.str() << "]\n";
ss >> std::quoted(out);
std::cout << "written out [" << out << "]\n";
// Output:
// read in [String with spaces, and embedded "quotes" too]
// stored as ["String with spaces, and embedded \"quotes\" too"]
// written out [String with spaces, and embedded "quotes" too]
詳細は上記のリンクを参照してください。
出力ストリームマニピュレータ
std::ends - 出力ストリームにヌル文字'\0'を挿入します。より正式には、このマニピュレータの宣言は次のようになります
template <class charT, class traits>
std::basic_ostream<charT, traits>& ends(std::basic_ostream<charT, traits>& os);
このマニピュレータは、式で使用されているときにos.put(charT())呼び出して文字を配置します
os << std::ends;
std::endlとstd::flushの両方のフラッシュ出力ストリームout呼び出すことによって、 out.flush()即座に出力が生成されます。しかし、 std::endlは、フラッシングの前に行'\n'末尾に'\n'挿入します。
std::cout << "First line." << std::endl << "Second line. " << std::flush
<< "Still second line.";
// Output: First line.
// Second line. Still second line.
std::setfill(c) - 塗りつぶし文字をc変更しc 。 std::setwよく使用されます。
std::cout << "\nDefault fill: " << std::setw(10) << 79 << '\n'
<< "setfill('#'): " << std::setfill('#')
<< std::setw(10) << 42 << '\n';
// Output:
// Default fill: 79
// setfill('#'): ########79
std::put_money(mon[, intl]) [C ++ 11]です。 out << std::put_money(mon, intl)式では、現在std::money_putされているロケールのstd::money_putファセットで指定されている金額mon ( long doubleまたはstd::basic_string型)をその文字表現にstd::money_putしますout 。 intlがtrue場合は国際通貨文字列を使用し、それ以外の場合は通貨記号を使用します。
long double money = 123.45;
// or std::string money = "123.45";
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << std::showbase << "en_US: " << std::put_money(money)
<< " or " << std::put_money(money, true) << '\n';
// Output: en_US: $1.23 or USD 1.23
std::cout.imbue(std::locale("ru_RU.utf8"));
std::cout << "ru_RU: " << std::put_money(money)
<< " or " << std::put_money(money, true) << '\n';
// Output: ru_RU: 1.23 руб or 1.23 RUB
std::cout.imbue(std::locale("ja_JP.utf8"));
std::cout << "ja_JP: " << std::put_money(money)
<< " or " << std::put_money(money, true) << '\n';
// Output: ja_JP: ¥123 or JPY 123
std::put_time(tmb, fmt) [C ++ 11] - 指定されたフォーマットfmtに従って、日付/時刻値をフォーマットしてstd::tmに出力します。
tmb - localtime()またはgmtime()から取得したカレンダ時間構造const std::tm*へのポインタ。
fmt - ヌルで終了する文字列へのポインタconst CharT*変換の形式を指定します。
#include <ctime>
...
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
std::cout.imbue(std::locale("ru_RU.utf8"));
std::cout << "\nru_RU: " << std::put_time(&tm, "%c %Z") << '\n';
// Possible output:
// ru_RU: Вт 04 июл 2017 15:08:35 UTC
詳細は上記のリンクを参照してください。
入力ストリームマニピュレータ
std::ws - 入力ストリームの先頭の空白を使います。これはstd::skipwsます。
#include <sstream>
...
std::string str;
std::istringstream(" \v\n\r\t Wow!There is no whitespaces!") >> std::ws >> str;
std::cout << str;
// Output: Wow!There is no whitespaces!
std::get_money(mon[, intl]) [C ++ 11]。式中in >> std::get_money(mon, intl)で指定され、金銭的価値などの文字入力を解析std::money_get現在に吹き込まロケールの面in 、及びの値を格納するmonの( long doubleまたはstd::basic_string型)。マニピュレータは、 intlがtrue場合は必要な国際通貨文字列を要求し 、それ以外の場合はオプションの通貨記号が必要です 。
#include <sstream>
#include <locale>
...
std::istringstream in("$1,234.56 2.22 USD 3.33");
long double v1, v2;
std::string v3;
in.imbue(std::locale("en_US.UTF-8"));
in >> std::get_money(v1) >> std::get_money(v2) >> std::get_money(v3, true);
if (in) {
std::cout << std::quoted(in.str()) << " parsed as: "
<< v1 << ", " << v2 << ", " << v3 << '\n';
}
// Output:
// "$1,234.56 2.22 USD 3.33" parsed as: 123456, 222, 333
std::get_time(tmb, fmt) [C ++ 11] - 指定されたフォーマットfmt tmbに格納されている日付/時刻の値を解析します。
tmb - 結果が格納されるconst std::tm*オブジェクトへの有効なポインタ。
fmt - ヌルで終了する文字列へのポインタconst CharT*変換フォーマットを指定します。
#include <sstream>
#include <locale>
...
std::tm t = {};
std::istringstream ss("2011-Februar-18 23:12:34");
ss.imbue(std::locale("de_DE.utf-8"));
ss >> std::get_time(&t, "%Y-%b-%d %H:%M:%S");
if (ss.fail()) {
std::cout << "Parse failed\n";
}
else {
std::cout << std::put_time(&t, "%c") << '\n';
}
// Possible output:
// Sun Feb 18 23:12:34 2011
詳細は上記のリンクを参照してください。