名稱空間
變體
操作

std::dec, std::hex, std::oct

來自 cppreference.com
< cpp‎ | io‎ | manip
 
 
 
輸入/輸出操縱器
浮點格式化
整數格式化
dechexoct
布林格式化
欄位寬度和填充控制
其他格式化
空白字元處理
輸出重新整理
(C++20)  

狀態標誌操作
時間與金錢 I/O
(C++11)
(C++11)
(C++11)
(C++11)
帶引號的操縱器
(C++14)
 
定義於標頭檔案 <ios>
(1)
(2)
(3)

修改整數 I/O 的預設數字基數。

1) 將流 strbasefield 設定為 dec,如同透過呼叫 str.setf(std::ios_base::dec, std::ios_base::basefield)
2) 將流 strbasefield 設定為 hex,如同透過呼叫 str.setf(std::ios_base::hex, std::ios_base::basefield)
3) 將流 strbasefield 設定為 oct,如同透過呼叫 str.setf(std::ios_base::oct, std::ios_base::basefield)

這是一個 I/O 操縱符。它可以用於型別為 std::basic_ostream 的任何 out,如表示式 out << std::hex;或者用於型別為 std::basic_istream 的任何 in,如表示式 in >> std::hex

目錄

[編輯] 引數

str - I/O 流的引用

[編輯] 返回值

str(操作後的流的引用)。

[編輯] 示例

#include <bitset>
#include <iostream>
#include <sstream>
 
int main()
{
    std::cout << "The number 42 in octal:   " << std::oct << 42 << '\n'
              << "The number 42 in decimal: " << std::dec << 42 << '\n'
              << "The number 42 in hex:     " << std::hex << 42 << '\n';
    int n;
    std::istringstream("2A") >> std::hex >> n;
    std::cout << std::dec << "Parsing \"2A\" as hex gives " << n << '\n';
    // the output base is sticky until changed
    std::cout << std::hex << "42 as hex gives " << 42
        << " and 21 as hex gives " << 21 << '\n';
 
    // Note: there is no I/O manipulator that sets up a stream to print out
    // numbers in binary format (e.g. bin). If binary output is necessary
    // the std::bitset trick can be used:
    std::cout << "The number 42 in binary:  " << std::bitset<8>{42} << '\n';
}

輸出

The number 42 in octal:   52
The number 42 in decimal: 42
The number 42 in hex:     2a
Parsing "2A" as hex gives 42
42 as hex gives 2a and 21 as hex gives 15
The number 42 in binary:  00101010

[編輯] 參閱

更改整數 I/O 所用的基數
(函式) [編輯]
控制是否使用字首指示數字基數
(函式) [編輯]