大家好, 我正在尝试打印布尔值,在搜索整个网络后, 我找不到任何东西这样做= /我结束的代码 使用如下: 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添加到字符串中
发布评论