C ++ print boolean, what is displayed?

I print boolto the output stream as follows:

#include <iostream>

int main()
{
    std::cout << false << std::endl;
}

Does the standard require a specific result in the stream (e.g. 0for false)?

+30
source share
2 answers

Standard threads have a flag boolalphathat determines what is displayed, when it is false, they will be displayed as 0and 1. When this is true, they will be displayed as falseand true.

There is also a manipulator std::boolalphato set a flag, so this:

#include <iostream>
#include <iomanip>

int main() {
    std::cout<<false<<"\n";
    std::cout << std::boolalpha;   
    std::cout<<false<<"\n";
    return 0;
}

... produces output, for example:

0
false

, , , , boolalpha true, , <locale> num_put, , , locale, / true false, . ,

#include <iostream>
#include <iomanip>
#include <locale>

int main() {
    std::cout.imbue(std::locale("fr"));

    std::cout << false << "\n";
    std::cout << std::boolalpha;
    std::cout << false << "\n";
    return 0;
}

... , , ( / "fr" "French" ), faux false. , , - Dinkumware/Microsoft ( ) false , .

, , numpunct, , , , numpunct, . , , ( ), , :

#include <array>
#include <string>
#include <locale>
#include <ios>
#include <iostream>

class my_fr : public std::numpunct< char > {
protected:
    char do_decimal_point() const { return ','; }
    char do_thousands_sep() const { return '.'; }
    std::string do_grouping() const { return "\3"; }
    std::string do_truename() const { return "vrai";  }
    std::string do_falsename() const { return "faux"; }
};

int main() {
    std::cout.imbue(std::locale(std::locale(), new my_fr));

    std::cout << false << "\n";
    std::cout << std::boolalpha;
    std::cout << false << "\n";
    return 0;
}

( , , ):

0
faux
+88

0 .

++ true 1 false 0.

, false 0, boolalpha str.

boolalpha, bool / : true, false .

#include <iostream>
int main()
{
  std::cout << std::boolalpha << false << std::endl;
}

:

false

IDEONE

+18

All Articles