Jas*_*ker 86 c++ string boolean
也许这是一个愚蠢的问题,但有没有办法将布尔值转换为字符串,使得1转为"true",0转为"false"?我可以使用if语句,但很高兴知道是否有办法用语言或标准库来实现.另外,我是一个学生.:)
gra*_*eds 112
如何使用C++语言本身?
bool t = true;
bool f = false;
std::cout << std::noboolalpha << t << " == " << std::boolalpha << t << std::endl;
std::cout << std::noboolalpha << f << " == " << std::boolalpha << f << std::endl;
Run Code Online (Sandbox Code Playgroud)
更新:
如果您需要超过4行代码而没有任何控制台输出,请访问cppreference.com的页面std::boolalpha,std::noboolalpha其中显示了控制台输出并解释了有关API的更多信息.
另外使用std::boolalpha将修改全局状态std::cout,您可能希望恢复原始行为,请转到此处以获取有关恢复状态的更多信息std::cout.
OJ.*_*OJ. 69
我们在谈论C++吧?为什么我们还在使用宏!?
C++内联函数提供与宏相同的速度,具有类型安全性和参数评估的额外好处(这避免了Rodney和dwj提到的问题.
inline const char * const BoolToString(bool b)
{
return b ? "true" : "false";
}
Run Code Online (Sandbox Code Playgroud)
除此之外,我还有其他一些抱怨,尤其是接受的答案:)
// this is used in C, not C++. if you want to use printf, instead include <cstdio>
//#include <stdio.h>
// instead you should use the iostream libs
#include <iostream>
// not only is this a C include, it's totally unnecessary!
//#include <stdarg.h>
// Macros - not type-safe, has side-effects. Use inline functions instead
//#define BOOL_STR(b) (b?"true":"false")
inline const char * const BoolToString(bool b)
{
return b ? "true" : "false";
}
int main (int argc, char const *argv[]) {
bool alpha = true;
// printf? that's C, not C++
//printf( BOOL_STR(alpha) );
// use the iostream functionality
std::cout << BoolToString(alpha);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
干杯:)
@DrPizza:为了这个简单的函数包含一个完整的升级库?你一定是在开玩笑?
DrP*_*zza 21
C++有适当的字符串,所以你也可以使用它们.它们位于标准标题字符串中.#include <string>使用它们.不再有strcat/strcpy缓冲区溢出; 不再缺少空终止符; 没有杂乱的手动内存管理; 正确计算具有适当值语义的字符串.
C++也能够将bool转换为人类可读的表示形式.我们之前通过iostream示例看到了它的提示,但它们有点受限,因为它们只能将文本爆炸到控制台(或者使用fstreams,文件).幸运的是,C++的设计者并不是完全白痴; 我们还有iostream,它不是由控制台或文件支持,而是由自动管理的字符串缓冲区支持.他们被称为字符串流.#include <sstream>来获取它们.然后我们可以说:
std::string bool_as_text(bool b)
{
std::stringstream converter;
converter << std::boolalpha << b; // flag boolalpha calls converter.setf(std::ios_base::boolalpha)
return converter.str();
}
Run Code Online (Sandbox Code Playgroud)
当然,我们并不是真的想要输入所有内容.幸运的是,C++还有一个方便的第三方库,名为Boost,可以帮助我们在这里.Boost有一个很好的函数叫做lexical_cast.我们可以这样使用它:
boost::lexical_cast<std::string>(my_bool)
Run Code Online (Sandbox Code Playgroud)
现在,可以说这比一些宏的开销更高; stringstreams处理你可能不关心的语言环境,并创建一个动态字符串(带内存分配),而宏可以产生一个文字字符串,这避免了这一点.但另一方面,stringstream方法可用于可打印和内部表示之间的大量转换.你可以向后跑; 例如,boost :: lexical_cast <bool>("true")做正确的事.您可以将它们与数字一起使用,实际上任何类型都使用正确的格式化I/O运算符.因此它们非常通用且有用.
如果毕竟你的分析和基准测试显示lexical_casts是一个不可接受的瓶颈,那就是你应该考虑做一些宏观恐怖.
C++20std::format("{}"
https://en.cppreference.com/w/cpp/utility/format/formatter#Standard_format_specation声称默认输出格式默认为字符串:
Run Code Online (Sandbox Code Playgroud)#include <format> auto s6 = std::format("{:6}", true); // value of s6 is "true "
和:
可用的布尔表示类型有:
- none, s:将文本表示形式(true 或 false,或特定于区域设置的形式)复制到输出。
- b、B、c、d、o、x、X:使用带有值 static_cast(value) 的整数表示类型。
现有的fmt库在获得官方支持之前就实现了它:https://github.com/fmtlib/fmt在 Ubuntu 22.04 上安装:
sudo apt install libfmt-dev
Run Code Online (Sandbox Code Playgroud)
修改源码替换:
<format>和<fmt/core.h>std::format到fmt::format主程序
#include <string>
#include <iostream>
#include <fmt/core.h>
int main() {
std::string message = fmt::format("The {} answer is {}.", true, false);
std::cout << message << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
并编译并运行:
g++ -std=c++11 -o main.out main.cpp -lfmt
./main.out
Run Code Online (Sandbox Code Playgroud)
输出:
The true answer is false.
Run Code Online (Sandbox Code Playgroud)
这应该没问题:
const char* bool_cast(const bool b) {
return b ? "true" : "false";
}
Run Code Online (Sandbox Code Playgroud)
但是,如果你想更多地使用C++ - ish:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string bool_cast(const bool b) {
ostringstream ss;
ss << boolalpha << b;
return ss.str();
}
int main() {
cout << bool_cast(true) << "\n";
cout << bool_cast(false) << "\n";
}
Run Code Online (Sandbox Code Playgroud)
如果您决定使用宏(或在未来的项目中使用 C),您应该在宏扩展中的“b”周围添加括号(我还没有足够的点数来编辑其他人的内容):
#define BOOL_STR(b) ((b)?"true":"false")
Run Code Online (Sandbox Code Playgroud)
这是一种防御性编程技术,可以防止隐藏的操作顺序错误;即,这如何评估所有编译器?
1 == 2 ? "true" : "false"
Run Code Online (Sandbox Code Playgroud)
相比
(1 == 2) ? "true" : "false"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
135651 次 |
| 最近记录: |