[英]Is there a way to convert a bool to a string?
I need to convert a bool to a text string.我需要将布尔值转换为文本字符串。 Does anyone know how to do that?
有谁知道这是怎么做到的吗?
I tried to use (std::string)bool
syntax but it didn't work.我尝试使用
(std::string)bool
语法但它没有用。
The C++ standard does not define any direct conversions between bool
and std::string
at the language level. C++ 标准没有在语言级别定义
bool
和std::string
之间的任何直接转换。
The simplest option is to just do the conversion yourself, eg:最简单的选择是自己进行转换,例如:
std::string s;
if (theBoolValue)
s = "true";
else
s = "false";
Or:或者:
std::string s = theBoolValue ? "true" : "false";
On the other hand, if you want to use the standard library to do the conversion, there are options for that.另一方面,如果您想使用标准库进行转换,则有一些选择。
operator<<
and operator>>
for I/O streams can convert between bool
and std::string
. I/O 流的
operator<<
和operator>>
可以在bool
和std::string
之间转换。 For instance, you can use a std::ostringstream
to format a bool
into a std::string
, eg:例如,您可以使用
std::ostringstream
将bool
格式化为std::string
,例如:
std::ostringstream oss;
oss << theBoolValue;
std::string s = oss.str();
By default, that will output "1"
for true
and "0"
for false
.默认情况下, output
"1"
表示true
, "0"
表示false
。 If you want the output to actually say "true"
or "false"
instead, you can use the std::boolalpha
I/O manipulator, eg:如果您希望 output 实际上说
"true"
或"false"
,您可以使用std::boolalpha
I/O 操纵器,例如:
oss << std::boolalpha << theBoolValue;
Alternatively, in C++20 and later, you can use std::format()
(in earlier versions, you can use the {fmt} library), eg:或者,在 C++20 及更高版本中,您可以使用
std::format()
(在早期版本中,您可以使用{fmt}库),例如:
std::string s = std::format("{}", theBoolValue); // returns "true" or "false"
std::string s = std::format("{:L}", theBoolValue); // returns the locale-specific representation
std::string s = std::format("{:d}", theBoolValue); // returns "1" or "0"
// other supported numeric formats:
// {:b} and {:B} for binary, "0b..." lowercase and "0B..." uppercase
// {:o} for octet, "0..."
// {:x} and {:X} for hex, "0x..." lowercase and "0X..." uppercase
#include <string>
#include <iostream>
namespace vt {
static constexpr auto as_string(const bool x) -> std::string {
return (x) ? "true" : "false";
}
} // namespace vt
int main(int argc, char** argv) {
const auto x = argv[argc] != nullptr;
// NB: argv[argc] is always nullptr so it's false.
// Using a turnary to initialize a std::string
// (See: https://en.cppreference.com/w/cpp/language/operator_other)
const auto k0 = std::string(x ? "true" : "false");
std::cout << "k0 = std::string(x ? \"true\" : \"false\"):\t" << k0 << "\n";
// Using a function to achieve the same.
const auto k1 = vt::as_string(x);
std::cout << "k1 = vt::as_string(x):\t\t\t" << k1 << "\n";
// If you need a string but you're fine with "0"/"1" over "false"/"true"
const auto k2 = std::to_string(x); // x is implicitly converted to int
std::cout << "k2 = std::to_string(x):\t\t\t" << k2 << "\n";
// If you only intend to write to a std::ostream you don't need a string.
std::cout << "std::cout << std::boolalpha << x:\t"
<< std::boolalpha << x << std::endl;
}
This is what Sam Varshavchik suggested in comment, basically, that is it:)这就是 Sam Varshavchik 在评论中的建议,基本上就是这样:)
bool b;
std::string decoded;
...
decoded = b ? "true" : "false";
printf("b is %s\n"
, decoded.c_str()
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.