溫馨提示×

c++中setfill的用法

c++
小樊
92
2024-08-11 04:01:35
欄目: 編程語言

在C++中,setfill()函數(shù)用于設(shè)置填充字符,即在輸出數(shù)據(jù)中占位符之間的字符。setfill()函數(shù)是通過iomanip頭文件中的setfill函數(shù)來調(diào)用的,并且必須與setw()一起使用。

下面是setfill()函數(shù)的用法示例:

#include <iostream>
#include <iomanip>

int main() {
    int num = 123;
    
    // 使用setfill來設(shè)置填充字符為'*'
    std::cout << std::setfill('*');
    
    // 設(shè)置寬度為10,并且填充字符為'*'
    std::cout << std::setw(10) << num << std::endl;
    
    return 0;
}

在上面的示例中,setfill(‘‘)函數(shù)用于設(shè)置填充字符為’’,然后使用setw()函數(shù)設(shè)置輸出寬度為10,輸出結(jié)果為:

******123

這樣就會在數(shù)字123之前用填充字符’*'來填充空白位置。

0