수색…
소개
조작자는 operator >> 또는 operator << 사용하여 입력 및 출력 스트림을 제어하는 데 도움이되는 특수 도우미 함수입니다.
그것들은 모두 #include <iomanip> 포함될 수 있습니다.
비고
매니퓰레이터는 다른 방법으로 사용될 수 있습니다. 예 :
-
os.width(n);os << std::setw(n);와 같습니다.os << std::setw(n);
is.width(n);와 같습니다.is >> std::setw(n);
-
os.precision(n);os << std::setprecision(n);와 같습니다.os << std::setprecision(n);
is.precision(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;
( ## - 연결 연산자 )
다음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);와 같습니다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);와 같습니다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;
flags :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);
os.unsetf(mask)는os << resetiosflags(mask);와 같습니다.os << resetiosflags(mask);
is.unsetf(mask)같습니다.is >> resetiosflags(mask);
std::ios_base::fmtflags타입의 거의 모든mask.
스트림 조작기
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 (십진수), std::hex (16 진수) 및 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 으로 재설정됩니다 (전체 목록은 여기에 있음 ).
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::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 가 other, 8 , 10 또는 16 이면 std::ios_base::basefield 는 std::ios_base::fmtflags(0) 됩니다. 10 진수 출력 및 접두어 종속 입력을 의미합니다.
기본 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 - 형식화 된 입력 함수를 사용하여 선행 공백을 건너 뛰 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.put(charT()) 를 호출하여 문자를 배치합니다
os << std::ends;
std::endl 및 std::flush 모두 수세식 출력 스트림 out 호출 out.flush() . 즉시 출력을 생성합니다. 그러나 std::endl 끝내기 전에 '\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 변경합니다. 종종 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) 의 표현식 out << std::put_money(mon, intl) mon ( long double 또는 std::basic_string 유형)의 mon 값을 현재 입력 된 로케일의 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 - null로 끝나는 문자열에 대한 포인터 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 Manipulator는 필수 국제 통화 문자열을 예상하고 그렇지 않으면 선택적 통화 기호가 필요합니다.
#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 - null로 끝나는 문자열에 대한 포인터 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
자세한 내용은 위의 링크를 참조하십시오.