<iomanip> 함수

get_money
get_time
put_money
put_time
quoted
resetiosflags
setbase
setfill
setiosflags
setprecision
setw

get_money

지정된 형식을 사용하여 스트림에서 통화 값을 추출하고 매개 변수의 값을 반환합니다.

template <class Money>
T7 get_money(Money& amount, bool use_intl);

매개 변수

amount
추출된 통화 값입니다.

use_intl
true일 경우 국가별 형식을 사용합니다. 기본값은 false입니다.

설명

조작자는 str 스트림에서 추출될 경우 formatted input function처럼 동작하는 개체를 반환합니다. 이 입력 함수는 str과 연결된 로캘 패싯 money_get에 대해 멤버 함수 get을 호출하며, use_intl을 사용하여 국가별 형식을 나타냅니다. 성공하면 추출된 통화 값을 amount에 저장합니다. 그런 다음 조작자는 str을 반환합니다.

Moneylong double 형식이거나 str과 동일한 요소 및 특성 매개 변수를 사용하는 basic_string의 인스턴스화여야 합니다.

get_time

지정된 형식을 사용하여 스트림에서 시간 값을 추출합니다. 매개 변수의 값을 시간 구조로 반환합니다.

template <class Elem>
T10 get_time(struct tm *time_ptr, const Elem *time_format);

매개 변수

time_ptr
시간 구조 형식의 시간입니다.

time_format
시간 값을 가져오는 데 사용할 형식입니다.

설명

조작자는 str 스트림에서 추출될 경우 formatted input function처럼 동작하는 개체를 반환합니다. 이 입력 함수는 str과 연결된 로캘 패싯 time_get에 대해 멤버 함수 get을 호출하며, tptr을 사용하여 시간 구조를 나타내고 fmt를 사용하여 null로 끝나는 형식 문자열의 시작을 나타냅니다. 성공하면 추출된 시간 필드와 연결된 값을 시간 구조에 저장합니다. 그런 다음 조작자는 str을 반환합니다.

예시

#include <ctime>
#include <iomanip>
#include <iostream>

int main()
{
    std::cout << "Enter a time, for example 15:24 for 3:24pm: ";
    struct std::tm when;
    std::cin >> std::get_time(&when, "%R");
    if (!std::cin.fail())
    {
        std::cout << "Entered: " << when.tm_hour << " hours, " << when.tm_min << " minutes\n";
    }

    return (int)std::cin.fail();
}

put_money

지정된 형식을 사용하여 금액을 스트림에 삽입합니다.

template <class Money>
T8 put_money(const Money& amount, bool use_intl);

매개 변수

amount
스트림에 삽입할 금액입니다.

use_intl
조작자가 국제 형식 falsetrue 사용해야 하는 경우(그렇지 않은 경우)로 설정합니다.

Return Value

str을(를) 반환합니다.

설명

조작자는 str 스트림에 삽입될 경우 서식 있는 출력 함수처럼 동작하는 개체를 반환합니다. 이 출력 함수는 str과 연결된 로캘 패싯 money_put에 대해 멤버 함수 put을 호출합니다. 성공하면 호출은 use_intl 사용하여적절한 형식을 삽입 amount 하고 str.fill()채우기 요소로 국제 형식을 나타냅니다. 그런 다음 조작자는 str을 반환합니다.

Moneylong double 형식이거나 str과 동일한 요소 및 특성 매개 변수를 사용하는 basic_string의 인스턴스화여야 합니다.

put_time

지정된 형식을 사용하여 시간 구조에서 스트림으로 시간 값을 씁니다.

template <class Elem>
T10 put_time(struct tm* time_ptr, const Elem* time_format);

매개 변수

time_ptr
스트림에 쓸 시간 값으로, 시간 구조에 제공됩니다.

time_format
시간 값을 쓸 형식입니다.

설명

조작자는 str 스트림에 삽입될 경우 formatted output function처럼 동작하는 개체를 반환합니다. 출력 함수는 str과 연결된 로캘 패킷 time_put에 대해 멤버 함수 put을 호출합니다. 출력 함수는 시간 구조를 나타내고 time_format null로 끝나는 형식 문자열의 시작을 나타내는 데 사용합니다time_ptr. 성공하면 형식 문자열에서 리터럴 텍스트를 삽입하고 시간 구조에서 변환된 값을 삽입합니다. 그런 다음 조작자는 str을 반환합니다.

quoted

(C++14의 새로운 기능)iostream 및 연산자를 사용하여 스트림 안팎으로 문자열을 편리하게 왕복할 수 >><< 있는 조작자입니다.

quoted(std::string str) // or wstring
quoted(const char* str) //or wchar_t*
quoted(std::string str, char delimiter, char escape) // or wide versions
quoted(const char* str, char delimiter, char escape) // or wide versions

매개 변수

str
char*, std::string문자열 리터럴 또는 원시 문자열 리터럴 또는 이러한 리터럴의 와이드 버전(예: std::wstring, wchar_t*)입니다.

delimiter
문자열의 시작과 끝에 대한 구분 기호로 사용할 사용자 지정 문자 또는 와이드 문자입니다.

escape
문자열 내의 이스케이프 시퀀스에 대한 이스케이프 문자로 사용할 사용자 지정 문자 또는 와이드 문자입니다.

설명

삽입 연산자 사용 및 형식 제어를 참조하세요.

예제

이 예제에서는 좁은 문자열을 사용하여 기본 구분 기호 및 이스케이프 문자와 함께 quoted를 사용하는 방법을 보여 줍니다. 와이드 문자열도 동일하게 지원됩니다.

#include <iostream>
#include <iomanip>
#include <sstream>

using namespace std;

void show_quoted_v_nonquoted()
{
    // Results are identical regardless of input string type:
    // string inserted { R"(This is a "sentence".)" }; // raw string literal
    // string inserted { "This is a \"sentence\"." };  // regular string literal
    const char* inserted = "This is a \"sentence\".";  // const char*
    stringstream ss, ss_quoted;
    string extracted, extracted_quoted;

    ss << inserted;
    ss_quoted << quoted(inserted);

    cout << "ss.str() is storing       : " << ss.str() << endl;
    cout << "ss_quoted.str() is storing: " << ss_quoted.str() << endl << endl;

    // Round-trip the strings
    ss >> extracted;
    ss_quoted >> quoted(extracted_quoted);

    cout << "After round trip: " << endl;
    cout << "Non-quoted      : " << extracted << endl;
    cout << "Quoted          : " << extracted_quoted << endl;
}

int main(int argc, char* argv[])
{
    show_quoted_v_nonquoted();

    // Keep console window open in debug mode.
    cout << endl << "Press Enter to exit" << endl;
    string input{};
    getline(cin, input);
}

/* Output:
ss.str() is storing       : This is a "sentence".
ss_quoted.str() is storing: "This is a \"sentence\"."

After round trip:
Non-quoted      : This
Quoted          : This is a "sentence".

Press Enter to exit
*/

다음 예제에서는 사용자 지정 구분 기호 또는 이스케이프 문자를 제공하는 방법을 보여줍니다.

#include <iostream>
#include <iomanip>
#include <sstream>

using namespace std;

void show_custom_delimiter()
{
    string inserted{ R"("This" "is" "a" "heavily-quoted" "sentence".)" };
    // string inserted{ "\"This\" \"is\" \"a\" \"heavily-quoted\" \"sentence\"" };
    // const char* inserted{ "\"This\" \"is\" \"a\" \"heavily-quoted\" \"sentence\"" };
    stringstream ss, ss_quoted;
    string extracted;

    ss_quoted << quoted(inserted, '*');
    ss << inserted;
    cout << "ss_quoted.str() is storing: " << ss_quoted.str() << endl;
    cout << "ss.str() is storing       : " << ss.str() << endl << endl;

    // Use the same quoted arguments as on insertion.
    ss_quoted >> quoted(extracted, '*');

    cout << "After round trip: " << endl;
    cout << "Quoted          : " << extracted << endl;

    extracted = {};
    ss >> extracted;
    cout << "Non-quoted      : " << extracted << endl << endl;
}

void show_custom_escape()
{
    string inserted{ R"(\\root\trunk\branch\nest\egg\yolk)" };
    // string inserted{ "\\\\root\\trunk\\branch\\nest\\egg\\yolk" };
    stringstream ss, ss_quoted, ss_quoted_custom;
    string extracted;

    // Use '"' as delimiter and '~' as escape character.
    ss_quoted_custom << quoted(inserted, '"', '~');
    ss_quoted << quoted(inserted);
    ss << inserted;
    cout << "ss_quoted_custom.str(): " << ss_quoted_custom.str() << endl;
    cout << "ss_quoted.str()       : " << ss_quoted.str() << endl;
    cout << "ss.str()              : " << ss.str() << endl << endl;

    // No spaces in this string, so non-quoted behaves same as quoted
    // after round-tripping.
}

int main(int argc, char* argv[])
{
    cout << "Custom delimiter:" << endl;
    show_custom_delimiter();
    cout << "Custom escape character:" << endl;
    show_custom_escape();

    // Keep console window open in debug mode.
    cout << endl << "Press Enter to exit" << endl;
    string input{};
    getline(cin, input);
}
/* Output:
Custom delimiter:
ss_quoted.str() is storing: *"This" "is" "a" "heavily-quoted" "sentence".*
ss.str() is storing       : "This" "is" "a" "heavily-quoted" "sentence".

After round trip:
Quoted          : "This" "is" "a" "heavily-quoted" "sentence".
Non-quoted      : "This"

Custom escape character:
ss_quoted_custom.str(): "\\root\trunk\branch\nest\egg\yolk"
ss_quoted.str()       : "\\\\root\\trunk\\branch\\nest\\egg\\yolk"
ss.str()              : \\root\trunk\branch\nest\egg\yolk

Press Enter to exit
*/

resetiosflags

지정된 플래그를 지웁니다.

T1 resetiosflags(ios_base::fmtflags mask);

매개 변수

mask
선택을 취소할 플래그입니다.

Return Value

조작자는 스트림str에서 추출하거나 스트림에 삽입할 때 호출str.setf(ios_base::fmtflags, mask)한 다음 , 참조 setf 및 반환하는 개체를 반환str합니다fmtflags.

예시

resetiosflags 사용 예제는 setw를 참조하세요.

setbase

정수의 밑을 설정합니다.

T3 setbase(int base);

매개 변수

base
숫자 밑입니다.

Return Value

조작자는 스트림에서 추출되거나 스트림str에 삽입될 때 호출str.setf(mask, ios_base::basefield)한 다음 반환되는 개체를 반환strios_base::basefield합니다. mask 여기서는 다음과 같이 결정됩니다.

예시

setbase 사용 예제는 setw를 참조하세요.

setfill

오른쪽 맞춤된 디스플레이에서 공백을 채우는데 사용할 문자를 설정합니다.

template <class Elem>
T4 setfill(Elem Ch);

매개 변수

Ch
오른쪽 맞춤된 디스플레이에서 공백을 채우는데 사용할 문자입니다.

Return Value

템플릿 조작자는 스트림 str에서 추출하거나 스트림에 삽입할 때 호출 str.fill(Ch)한 다음 반환하는 개체를 반환합니다 str. 형식 Elem 은 스트림 str의 요소 형식과 동일해야 합니다.

예시

setfill 사용 예제는 setw를 참조하세요.

setiosflags

지정된 플래그를 설정합니다.

T2 setiosflags(ios_base::fmtflags mask);

매개 변수

mask
설정할 플래그입니다.

Return Value

조작자는 스트림에서 추출되거나 스트림str에 삽입될 때 호출str.setf(mask)한 다음 반환되는 개체를 반환strsetf합니다.

예시

setiosflags 사용 예제는 setw를 참조하세요.

setprecision

부동 소수점 값의 전체 자릿수를 설정합니다.

T5 setprecision(streamsize Prec);

매개 변수

Prec
부동 소수점 값의 전체 자릿수입니다.

Return Value

조작자는 스트림에서 추출되거나 스트림str에 삽입될 때 호출str.precision(Prec)한 다음 반환되는 개체를 반환strprecision합니다.

예시

setprecision 사용 예제는 setw를 참조하세요.

setw

스트림에서 다음 요소에 대한 표시 필드의 너비를 지정합니다.

T6 setw(streamsize Wide);

매개 변수

Wide
표시 필드의 너비입니다.

Return Value

조작자는 스트림 str에서 추출하거나 스트림에 삽입할 때 호출 str.width(Wide)한 다음 반환하는 개체를 반환합니다 str. 자세한 내용은 width를 참조하세요.

설명

setw 는 스트림의 다음 요소에 대해서만 너비를 설정하며 지정하려는 너비가 있는 각 요소 앞에 삽입해야 합니다.

예시

// iomanip_setw.cpp
// compile with: /EHsc
// Defines the entry point for the console application.
//
// Sample use of the following manipulators:
//   resetiosflags
//   setiosflags
//   setbase
//   setfill
//   setprecision
//   setw

#include <iostream>
#include <iomanip>

using namespace std;

const double   d1 = 1.23456789;
const double   d2 = 12.3456789;
const double   d3 = 123.456789;
const double   d4 = 1234.56789;
const double   d5 = 12345.6789;
const long      l1 = 16;
const long      l2 = 256;
const long      l3 = 1024;
const long      l4 = 4096;
const long      l5 = 65536;
int         base = 10;

void DisplayDefault( )
{
   cout << endl << "default display" << endl;
   cout << "d1 = " << d1 << endl;
   cout << "d2 = " << d2 << endl;
   cout << "d3 = " << d3 << endl;
   cout << "d4 = " << d4 << endl;
   cout << "d5 = " << d5 << endl;
}

void DisplayWidth( int n )
{
   cout << endl << "fixed width display set to " << n << ".\n";
   cout << "d1 = " << setw(n) << d1 << endl;
   cout << "d2 = " << setw(n) << d2 << endl;
   cout << "d3 = " << setw(n) << d3 << endl;
   cout << "d4 = " << setw(n) << d4 << endl;
   cout << "d5 = " << setw(n) << d5 << endl;
}

void DisplayLongs( )
{
   cout << setbase(10);
   cout << endl << "setbase(" << base << ")" << endl;
   cout << setbase(base);
   cout << "l1 = " << l1 << endl;
   cout << "l2 = " << l2 << endl;
   cout << "l3 = " << l3 << endl;
   cout << "l4 = " << l4 << endl;
   cout << "l5 = " << l5 << endl;
}

int main( int argc, char* argv[] )
{
   DisplayDefault( );

   cout << endl << "setprecision(" << 3 << ")" << setprecision(3);
   DisplayDefault( );

   cout << endl << "setprecision(" << 12 << ")" << setprecision(12);
   DisplayDefault( );

   cout << setiosflags(ios_base::scientific);
   cout << endl << "setiosflags(" << ios_base::scientific << ")";
   DisplayDefault( );

   cout << resetiosflags(ios_base::scientific);
   cout << endl << "resetiosflags(" << ios_base::scientific << ")";
   DisplayDefault( );

   cout << endl << "setfill('" << 'S' << "')" << setfill('S');
   DisplayWidth(15);
   DisplayDefault( );

   cout << endl << "setfill('" << ' ' << "')" << setfill(' ');
   DisplayWidth(15);
   DisplayDefault( );

   cout << endl << "setprecision(" << 8 << ")" << setprecision(8);
   DisplayWidth(10);
   DisplayDefault( );

   base = 16;
   DisplayLongs( );

   base = 8;
   DisplayLongs( );

   base = 10;
   DisplayLongs( );

   return   0;
}
default display
d1 = 1.23457
d2 = 12.3457
d3 = 123.457
d4 = 1234.57
d5 = 12345.7

setprecision(3)
default display
d1 = 1.23
d2 = 12.3
d3 = 123
d4 = 1.23e+003
d5 = 1.23e+004

setprecision(12)
default display
d1 = 1.23456789
d2 = 12.3456789
d3 = 123.456789
d4 = 1234.56789
d5 = 12345.6789

setiosflags(4096)
default display
d1 = 1.234567890000e+000
d2 = 1.234567890000e+001
d3 = 1.234567890000e+002
d4 = 1.234567890000e+003
d5 = 1.234567890000e+004

resetiosflags(4096)
default display
d1 = 1.23456789
d2 = 12.3456789
d3 = 123.456789
d4 = 1234.56789
d5 = 12345.6789

setfill('S')
fixed width display set to 15.
d1 = SSSSS1.23456789
d2 = SSSSS12.3456789
d3 = SSSSS123.456789
d4 = SSSSS1234.56789
d5 = SSSSS12345.6789

default display
d1 = 1.23456789
d2 = 12.3456789
d3 = 123.456789
d4 = 1234.56789
d5 = 12345.6789

setfill(' ')
fixed width display set to 15.
d1 =      1.23456789
d2 =      12.3456789
d3 =      123.456789
d4 =      1234.56789
d5 =      12345.6789

default display
d1 = 1.23456789
d2 = 12.3456789
d3 = 123.456789
d4 = 1234.56789
d5 = 12345.6789

setprecision(8)
fixed width display set to 10.
d1 =  1.2345679
d2 =  12.345679
d3 =  123.45679
d4 =  1234.5679
d5 =  12345.679

default display
d1 = 1.2345679
d2 = 12.345679
d3 = 123.45679
d4 = 1234.5679
d5 = 12345.679

setbase(16)
l1 = 10
l2 = 100
l3 = 400
l4 = 1000
l5 = 10000

setbase(8)
l1 = 20
l2 = 400
l3 = 2000
l4 = 10000
l5 = 200000

setbase(10)
l1 = 16
l2 = 256
l3 = 1024
l4 = 4096
l5 = 65536

참고 항목

<iomanip>