3

関数の使用を終了する行で、すべての出力フラグをデフォルトにリセットするつもりですresetiosflags。私の期待に反して、この方法で実行しようとすると、誤った出力が提供されます。

#include <iostream>
#include <iomanip>
using namespace std;
int
main()
{
    bool first;
    int second;
    long third;
    float fourth;
    float fifth;
    double sixth;

    cout << "Enter bool, int, long, float, float, and double values: ";
    cin >> first >> second >> third >> fourth >> fifth >> sixth;
    cout << endl;

// ***** Solution starts here ****
    cout << first << " " << boolalpha << first  << endl << resetiosflags;
    cout << second << " " << showbase << hex << second << " " << oct << second << endl << resetiosflags;
    cout << third << endl;
    cout << showpos << setprecision(4) << showpoint << right << fourth << endl << resetiosflags;
    cout << scientific << fourth << endl << resetiosflags;
    cout << setprecision(7) << left << fifth << endl << resetiosflags;
    cout << fixed << setprecision(3) << fifth << endl << resetiosflags;
    cout << third << endl;
    cout << fixed << setprecision(2) << fourth << endl << resetiosflags;
    cout << fixed << setprecision(0) << sixth << endl << resetiosflags;
    cout << fixed << setprecision(8) << fourth << endl << resetiosflags;
    cout << setprecision(6) << sixth << endl << resetiosflags;
// ***** Solution ends here ****

    cin.get();
    return 0;
}

私の既知の代替手段は、それらを再記述して個別にフラグを解除することですが、それは不必要に思えます。

4

1 に答える 1

1
/*unspecified*/ resetiosflags( std::ios_base::fmtflags mask );

std::resetiosflags()などの式で使用するためのマニピュレータout << resetiosfloags( flags )です。おそらく、あなたがしているstd::operator<<ことは、ブール値を取って1を出力するオーバーロードによって選択される関数ポインタを渡すことです.

ただしstd::resetiosflags()、精度を操作できないパラメーターとしてフォーマット フラグを使用します。std::ios_base::boolalphaただし、次のことができます。

std::cout << ... << std::resetiosflags(std::ios_base::boolalpha);

もありますstd::noboolalpha

std::cout << ... << std::noboolalpha;

ただし、精度をデフォルトにリセットする必要がある場合は、そのための独自のマニピュレータを作成できます。Boost IO State Saverも使用できます。

于 2015-01-31T23:12:19.927 に答える