Converting bool to text in C++

前端 未结 15 2344
走了就别回头了
走了就别回头了 2020-12-01 04:17

Maybe this is a dumb question, but is there any way to convert a boolean value to a string such that 1 turns to \"true\" and 0 turns to \"false\"? I could just use an if st

15条回答
  •  Happy的楠姐
    2020-12-01 04:37

    C++20 std::format("{}"

    https://en.cppreference.com/w/cpp/utility/format/formatter#Standard_format_specification claims that the default output format will be the string by default:

    auto s6 = std::format("{:6}", true);  // value of s6 is "true  "
    

    and:

    The available bool presentation types are:

    • none, s: Copies textual representation (true or false, or the locale-specific form) to the output.
    • b, B, c, d, o, x, X: Uses integer presentation types with the value static_cast(value).

    Related: std::string formatting like sprintf

提交回复
热议问题