也许这是一个愚蠢的问题,但是否有任何方法来转换一个布尔价值串这样的,1变成"真正的"和0变为"虚假的"?我可能只使用,如果声明,但它会很高兴知道,如果有一种方法可做到这一用语言或标准图书馆。另外,我是一个学究。:)

有帮助吗?

解决方案

关于如何使用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;

更新:

如果你想超过4行的代码没有任何输出控制台,请到 cppreference.com's页说 std::boolalphastd::noboolalpha 它显示了你们的控制台输出并解释了更多的有关API。

此外,使用 std::boolalpha 将修改全球状态 std::cout, 你可能想要恢复原始行为 去这里了解更多信息在恢复状态的 std::cout.

其他提示

我们正在谈论有关C++对吗?为什么地球是我们仍然使用宏!?

C++内联的功能给你同样的速度作为一个宏观、与加入有益的类型安全和参数的评估(其避免了的问题罗德尼和dwj提及。

inline const char * const BoolToString(bool b)
{
  return b ? "true" : "false";
}

除此之外我还有其他一些抱怨,特别是与接受的答案:)

// 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;
}

干杯:)


@DrPizza:包括一个整体提升lib为了一个功能这么简单?你不是在开玩笑吗?

C++有适当的串所以你也可以使用它们。他们在标准头串。#包括 <string> 使用它们。没有更多的strcat/缓冲本超支;没有更多的失踪空终止;没有更多的混乱的手册,内存管理;适当计数字的适当价值的语义。

C++有能力把弹球成为人类可读的陈述。我们看到暗示它早些时候与法师的例子,但他们一点有限,因为他们只能爆炸的文本,以控制台(或与fstreams,文件)。幸运的是,设计用C++不是完全是白痴;我们还有iostreams支持不是通过控制台或文件,但是通过一个自动管理串的缓冲区。他们被称为stringstreams.#包括 <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();
}

当然,我们真的不想要的类型。幸运的是,C++还有一个方便第三方的图书馆命名为 提高 这可以帮助我们在这里。提升有一个不错的功能称为lexical_cast.我们可以使用它,从而:

boost::lexical_cast<std::string>(my_bool)

现在,这是真的来说,这是更高的开销比一些宏;stringstreams处理选择,你可能不关心,创建一个动态的string(与存分配),而宏可以产生一个字符串,其避免的。但另一方面,stringstream方法可以用于许多伟大之间的转换印刷和内部陈述。你可以跑他们向后;提升::lexical_cast<bool>("真正的")并不正确的事情,例如。你可以用它们的数字和事实上的任何类型的正确格式的I/O运营商。所以他们相当灵活和有用的。

如果这一切之后你的分析和基准显示,lexical_casts是不可接受的瓶颈, 那是 当你应该考虑做一些宏观恐怖。

这应该被罚款:


const char* bool_cast(const bool b) {
    return b ? "true" : "false";
}

但是,如果你想要做更多的C++上下:


#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";
}

如果你决定使用宏(或使用C在一个未来的项目)应加括号的围绕'b'在宏展(我没有足够的分尚编辑的其他人的内容):

#define BOOL_STR(b) ((b)?"true":"false")

这是一个 防御编程 技术,防止隐藏的秩序的操作错误;即如何评估 所有 编译器?

1 == 2 ? "true" : "false"

相比

(1 == 2) ? "true" : "false"

我使用一个三元在printf这样的:

printf("%s\n", b?"true":"false");

如果你宏:

B2S(b) ((b)?"true":"false")

然后你需要确保不论你通过因为 'b' 没有任何副作用。不要忘记前后的方括号的 'b' 因为你可以得到汇编的错误。

用C++11您可以使用一种氧得到一个稍微更加紧凑的代码,在地方用途:

bool to_convert{true};
auto bool_to_string = [](bool b) -> std::string {
    return b ? "true" : "false";
};
std::string str{"string to print -> "};
std::cout<<str+bool_to_string(to_convert);

打印:

string to print -> true

这个职位是旧的,但现在你可以使用 std::to_string 换了很多的变量 std::string.

http://en.cppreference.com/w/cpp/string/basic_string/to_string

使用 boolalpha 印bool到串。

std::cout << std::boolalpha << b << endl;
std::cout << std::noboolalpha << b << endl;

C++基准

我同意,宏可能是最好的配合。我只是刮起了一个试验案(相信我我没有好与C/C++但这听起来有趣的):

#include <stdio.h>
#include <stdarg.h>

#define BOOL_STR(b) (b?"true":"false")

int main (int argc, char const *argv[]) {
    bool alpha = true;
    printf( BOOL_STR(alpha) );
    return 0;
}

只要串可以直接查看作为一个char阵它要是真的很难说服我 std::string 表示串为第一类公民在C++。

此外,结合分配和有界似乎是个好主意给我反正。

试试这个宏。任何你想要的"真正"或虚假的,显示出来,只是取代它PRINTBOOL(var)在var是bool你想要的文字。

#define PRINTBOOL(x) x?"true":"false"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top