分享方式:


<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 關聯之地區設定 facet money_get 的成員函式 get,其中會使用 use_intl 來指出使用國際格式。 如果成功,該呼叫就會在 amount 中儲存所擷取的貨幣值。 接著,操作工具會傳回 str

Money 必須屬於 long 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 關聯之地區設定 facet 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
true如果操作工具應該使用國際格式,則設定為 ,false如果不應該的話。

傳回值

傳回 str

備註

操作工具會傳回一個物件,將此物件插入到資料流 str 中時,其行為會像已格式化的輸出函式一樣,此函式會呼叫與 str 關聯之地區設定 facet money_put 的成員函式 put。 如果成功,呼叫會 amount 使用 use_intl 來插入適當格式,以表示國際格式和 str.fill(),做為填滿專案。 接著,操作工具會傳回 str

Money 必須屬於 long 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 關聯之地區設定 facet time_put 的成員函式 put。 輸出函式會使用 time_ptr 來表示時間結構,以及 time_format 指出 Null 終止格式字串的開頭。 如果成功,該呼叫就會插入來自格式字串的常值字串,並轉換來自時間結構的值。 接著,操作工具會傳回 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
std::stringchar*、字串常值或原始字串常值,或其中任何一種的寬版本(例如 、 。 std::wstringwchar_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
要清除的旗標。

傳回值

操作工具會傳回 物件,當從 資料流中擷取或插入 str時,會呼叫 str.setf(ios_base::fmtflags, mask),然後傳 str回 ,請參閱 setffmtflags

範例

如需使用 resetiosflags的範例,請參閱 setw

setbase

設定整數的基底。

T3 setbase(int base);

參數

base
數字基底。

傳回值

操作工具會傳回 物件,當擷取或插入數據流 str時,會呼叫 str.setf(mask, ios_base::basefield),然後傳 str回 ,請參閱 ios_base::basefield。 在這裡, mask 會依下列方式決定:

範例

如需使用 setbase的範例,請參閱 setw

setfill

設定將用來填滿靠右對齊顯示中的空格字元。

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

參數

Ch
將用來填滿靠右對齊顯示中空格的字元。

傳回值

樣板操作工具會傳回 物件,從 擷取或插入數據流 str時,會呼叫 str.fill(Ch),然後傳 str回 。 此類型 Elem 必須與數據流 str的項目類型相同。

範例

如需使用 setfill的範例,請參閱 setw

setiosflags

設定指定的旗標。

T2 setiosflags(ios_base::fmtflags mask);

參數

mask
要設定的旗標。

傳回值

操作工具會傳回 物件,當擷取或插入數據流 str時,會呼叫 str.setf(mask),然後傳 str回 ,請參閱 setf

範例

如需使用 setiosflags的範例,請參閱 setw

setprecision

設定浮點值的有效位數。

T5 setprecision(streamsize Prec);

參數

Prec
浮點數值的有效位數。

傳回值

操作工具會傳回 物件,當擷取或插入數據流 str時,會呼叫 str.precision(Prec),然後傳 str回 ,請參閱 precision

範例

如需使用 setprecision的範例,請參閱 setw

setw

指定資料流中下一個元素的顯示欄位寬度。

T6 setw(streamsize Wide);

參數

Wide
顯示欄位的寬度。

傳回值

操作工具會傳回 物件,從擷取或插入資料流 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>