C++ ios::Skipws() 函式



C++ 的std::ios::skipws()函式是一個用於輸入流的運算子。當它在輸入流上被呼叫時,它會使流在提取實際輸入之前跳過任何空白字元(空格、製表符、換行符)。這確保了空白字元不會干擾從流中讀取資料。

語法

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

ios_base& skipws (ios_base& str);

引數

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

返回值

此函式返回引數 str。

異常

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

資料競爭

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

示例

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

#include <iostream>
#include <sstream>
int main()
{
    std::istringstream x("  1  12");
    int a, b;
    x >> std::skipws >> a >> b;
    std::cout << "a: " << a << ", b: " << b << std::endl;
    return 0;
}

輸出

以上程式碼的輸出如下:

a: 1, b: 12

示例

考慮以下示例,我們將停用 skipws() 函式。

#include <iostream>
#include <sstream>
int main()
{
    std::istringstream a("  1  22");
    int x, y;
    a >> std::noskipws >> x >> y;
    std::cout << "x : " << x << ", y : " << y << std::endl;
    return 0;
}

輸出

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

x : 0, y : 32761
ios.htm
廣告