C++ ios::Showpoint() 函式



C++ 的std::ios::showpoint()函式是一個流操縱器,它確保浮點數以小數點形式顯示,即使它們是整數。當此函式在輸出流上呼叫時,它會修改流格式標誌以包含 showpoint 標誌。

showpoint() 函式可以與插入運算子 (<<) 一起使用,以整合到流操作中。

語法

以下是 std::ios::showpoint() 函式的語法。

ios_base& showpoint( std::ios_base& str );

引數

  • str - 它指示受影響的格式標誌的流物件。

返回值

此函式返回引數 str。

異常

如果丟擲異常,則 str 處於有效狀態。

資料競爭

它修改了 str。對同一流物件的併發訪問可能會導致資料競爭。

示例

在以下示例中,我們將考慮 showpoint() 函式的基本用法。

#include <iostream>
#include <iomanip>
int main()
{
    double x = 112.0;
    std::cout << std::showpoint << x << std::endl;
    return 0;
}

輸出

以上程式碼的輸出如下:

112.000

示例

考慮以下示例,我們將使用 std::fixed 以及 showpoint() 函式。

#include <iostream>
#include <iomanip>
int main()
{
    double x = 12.3;
    std::cout << std::fixed << std::showpoint << x << std::endl;
    return 0;
}

輸出

以下是以上程式碼的輸出:

12.300000

示例

在以下示例中,我們將觀察沒有 showpoint() 函式的輸出。

#include <iostream>
int main()
{
    double x = 112.0;
    std::cout << x << std::endl;
    return 0;
}

輸出

如果我們執行以上程式碼,它將生成以下輸出:

112
ios.htm
廣告

© . All rights reserved.