将bool添加到字符串中

编程入门 行业动态 更新时间:2024-10-10 12:23:28
本文介绍了将bool添加到字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我正在尝试打印布尔值,在搜索整个网络后, 我找不到任何东西这样做= /我结束的代码 使用如下: string boolString(Object * obj_,size_t maxOutput _){ string string_; for(size_t i = 0; i< maxOutput_; i ++){ if(sizeof( obj _-> boolean(i))== sizeof(bool)){ if(obj _-> boolean(i)== true) string_ + = " 1" ;; else string_ + =" 0"; } } 返回string_; } 有什么办法可以简化这段代码吗?

推荐答案

aaragon写道: aaragon wrote: 大家好, 我正在尝试打印布尔值,在我搜索整个网络后, 我找不到任何可以做到这一点= /我最终使用 的代码如下: string boolString(Object * obj_,size_t maxOutput _){ string string_; for(size_t i = 0; I< maxOutput_; i ++){ if(sizeof(obj _-> boolean(i))== sizeof(bool)){ Hi All, I''m trying to print boolean values and after I searched the entire web, I couldn''t find anything that does this =/ The code that I ended up using is as follows: string boolString(Object* obj_, size_t maxOutput_){ string string_; for (size_t i=0; i<maxOutput_; i++){ if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

以上行应该做什么? obj _-> boolean(i)返回什么? 注意sizeof总是在编译时计算。

What is the above line supposed to do? What does obj_->boolean(i) return? Note that sizeof is always computed at compile time.

if(obj_- > boolean(i)== true) string_ + =" 1" ;; else string_ + =" 0" ; } } 返回string_; } 有没有办法简化这段代码? if (obj_->boolean(i) == true) string_ += "1"; else string_ += "0"; } } return string_; } Is there any way to simplify this code?

string boolString(Object * obj_,size_t maxOutput _){ stringstream stream_; for(size_t i = 0; i< maxOutput_; i ++) //取决于boolean()的返回类型,你可以删除强制转换 stream_<< ; bool(obj _-> boolean(i)); 返回stream_.str(); }

string boolString(Object* obj_, size_t maxOutput_){ stringstream stream_; for (size_t i=0; i<maxOutput_; i++) // depending on the return type of boolean(), you can remove the cast stream_ << bool(obj_->boolean(i)); return stream_.str(); }

aaragon写道: aaragon wrote: 我正在尝试打印布尔值并在我搜索完整的 $ b $之后b web,我找不到任何可以做到这一点= /我最终使用的代码如下: string boolString(Object * obj_,size_t maxOutput _){ string string_; for(size_t i = 0; i< maxOutput_; i ++){ if(sizeof(obj _-> boolean(i))== sizeof(bool)){ I''m trying to print boolean values and after I searched the entire web, I couldn''t find anything that does this =/ The code that I ended up using is as follows: string boolString(Object* obj_, size_t maxOutput_){ string string_; for (size_t i=0; i<maxOutput_; i++){ if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

为什么需要检查尺寸?它给你带来了什么?

Why do you need to check the size? What does it give you?

if(obj _-> boolean(i)== true) string_ + =" 1" ;; else string_ + =" 0" ;; if (obj_->boolean(i) == true) string_ += "1"; else string_ += "0";

这可以写成 string_ + =(obj _-> boolean(i)?'''1'' :''0''); 我觉得有点简单。 另外,请考虑你调用''boolean''会员两次在这里(一次是 检查大小,另一个是实际得到的值);如果 函数有副作用,那么调用它可能不是你想要的b $ b。

This could be written string_ += (obj_->boolean(i) ? ''1'' : ''0''); which I find a bit simpler. Also, consider that you call ''boolean'' member twice here (once to check the size and the other to actually get the value); if the function has side effects, calling it twice may not be what you want.

} } 返回string_; } 有什么方法可以简化这段代码? } } return string_; } Is there any way to simplify this code?

V - 请在通过电子邮件回复时删除资金'A' 我没有回复最热门的回复,请不要问

V -- Please remove capital ''A''s when replying by e-mail I do not respond to top-posted replies, please don''t ask

Victor Bazarov写道: Victor Bazarov wrote: aaragon写道: aaragon wrote: 我正在尝试打印布尔值并在我搜索之后整个 网页,我找不到任何可以做到这一点= /我最终使用的代码如下: string boolString(Object * obj_,size_t maxOutput _){ string string_; for(size_t i = 0; i< maxOutput_; i ++ ){ if(sizeof(obj _-> boolean(i))== sizeof(bool)){ I''m trying to print boolean values and after I searched the entire web, I couldn''t find anything that does this =/ The code that I ended up using is as follows: string boolString(Object* obj_, size_t maxOutput_){ string string_; for (size_t i=0; i<maxOutput_; i++){ if( sizeof(obj_->boolean(i)) == sizeof(bool) ){

为什么需要检查尺寸?它给你带来了什么?

Why do you need to check the size? What does it give you?

我需要检查大小,因为函数可能不会返回bool但是 整数例如。

I need to check the size because the function may not return a bool but an integer for example.

> > if(obj _-> boolean(i)== true) string_ + =" 1"; else string_ + =" 0" ;; if (obj_->boolean(i) == true) string_ += "1"; else string_ += "0";

这可以写成 string_ + =(obj _-> boolean(i)?'''1'' :''0''); 我觉得有点简单。 另外,请考虑你调用''boolean''会员两次在这里(一次是 检查大小,另一个是实际得到的值);如果 函数有副作用,那么调用它可能不是你想要的b $ b。

This could be written string_ += (obj_->boolean(i) ? ''1'' : ''0''); which I find a bit simpler. Also, consider that you call ''boolean'' member twice here (once to check the size and the other to actually get the value); if the function has side effects, calling it twice may not be what you want.

} } 返回string_; } 有什么方法可以简化这段代码? } } return string_; } Is there any way to simplify this code?

V - 请在通过电子邮件回复时删除资金'A' 我没有回复最热门的回复,请不要问

V -- Please remove capital ''A''s when replying by e-mail I do not respond to top-posted replies, please don''t ask

更多推荐

将bool添加到字符串中

本文发布于:2023-05-31 10:30:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389932.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   bool

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!