从bool到int:1或0 [英] From bool to int: 1 or 0

查看:91
本文介绍了从bool到int:1或0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我想要一个超高效的算法来计算数组中的6'的数量,(给定第一个元素的地址和

元素),我可能会开始:


#include< cstddef>


unsigned AmountSixes(const int * p,std :: size_t const len)

{

unsigned amount = 0;


const T * const p_over = array + len;


do

{

if(* p ++ == 6)++ amount;

}

while(p!= p_over);


返还金额;

}

然而,根据我自己的编码风格,我可能会将循环更改为:


do

{

amount + =(* p ++ == 6);

} while(p!= p_over);

这将依赖于bool转换为int这将产生1或0.


如果我愿意,那么对于大多数架构而言,哪些可能会更有效率?编写完全可移植的代码,从手表到个人电脑,可以使用

吗?


-


Frederick Gotham

解决方案

Frederick Gotham写道:

如果我想要一个超级高效的算法计算一个数组中的6'的数量,(给定第一个元素的地址和元素的数量),我可以从以下开始:


或者你可以使用''std :: count''......
#include< cstddef>

unsigned AmountSixes(const int * p,std: :size_t const len)
{
无符号数量= 0;

const T * const p_over =数组+ len;


{
if(* p ++ == 6)++ amount;
}
while(p!= p_over);

返回金额;
}

然而,根据我自己的编码风格,我可能会把循环改为:


{
金额+ =(* p ++ == 6);
} while(p!= p_over);


如果有''std :: count'',为什么还要烦恼? :-)

这将依赖于bool到int的转换,这将产生1或
0.


确实如此。

如果我在从手表到个人计算机的所有内容上编写完全可移植的代码,那么在大多数架构上可能会更有效的想法吗?




返回std :: count(p,p + len,6)


V

- -

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


Victor Bazarov发布:

return std :: count(p,p + len,6)


感谢您的投入,Victor。检查

特定元素值的实例数量只是一个例子。


我的帖子的主要观点是讨论以下两个中的哪一个

方法在日常编程中会更好:


if(* p ++ == 6)++ amount;


或:


金额+ =(* p ++ == 6);


-


Frederick Gotham


Frederick Gotham写道:

Victor Bazarov发布:

return std :: count(p,p + len,6)



感谢输入,Victor。检查
特定元素值的实例数量只是一个例子。

我的帖子的主要观点是讨论以下两种方法中的哪一种更可取在日常编程中:

if(* p ++ == 6)++ amount;

或:

金额+ =(* p ++ == 6);




努力说出你的意思我更喜欢前者;它确切地说

是什么意思。后者更加神秘,充满了诡计。你要问的事实只是证明了这一点。



If I wanted a super-efficient algorithm for counting the amount of 6''s in
an array, (given the address of the first element and the amount of
elements), I might start off with:

#include <cstddef>

unsigned AmountSixes( const int *p, std::size_t const len )
{
unsigned amount = 0;

const T* const p_over = array + len;

do
{
if( *p++ == 6 ) ++amount;
}
while ( p != p_over );

return amount;
}
However, by my own coding style, I''d probably change the loop to:

do
{
amount += (*p++ == 6);
} while ( p != p_over );
This would rely on the bool to int conversion which would yield 1 or 0.

Any thoughts on which would be likely to be more efficient on the
majority of architectures if I was writing fully portable code to be used
on everything from a wristwatch to a personal computer?


--

Frederick Gotham

解决方案

Frederick Gotham wrote:

If I wanted a super-efficient algorithm for counting the amount of
6''s in an array, (given the address of the first element and the
amount of elements), I might start off with:
Or you might use ''std::count''...

#include <cstddef>

unsigned AmountSixes( const int *p, std::size_t const len )
{
unsigned amount = 0;

const T* const p_over = array + len;

do
{
if( *p++ == 6 ) ++amount;
}
while ( p != p_over );

return amount;
}
However, by my own coding style, I''d probably change the loop to:

do
{
amount += (*p++ == 6);
} while ( p != p_over );
Why bother when there is ''std::count''? :-)
This would rely on the bool to int conversion which would yield 1 or
0.
It does.
Any thoughts on which would be likely to be more efficient on the
majority of architectures if I was writing fully portable code to be
used on everything from a wristwatch to a personal computer?



return std::count(p, p + len, 6)

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 posted:

return std::count(p, p + len, 6)


Thanks for the input, Victor. Checking for the quantity of instances of a
particular elemental value was just an example.

The main point of my post was to discuss which of the following two
approaches would be preferable in everyday programming:

if( *p++ == 6 ) ++amount;

or:

amount += (*p++ == 6);

--

Frederick Gotham


Frederick Gotham wrote:

Victor Bazarov posted:

return std::count(p, p + len, 6)


Thanks for the input, Victor. Checking for the quantity of instances of a
particular elemental value was just an example.

The main point of my post was to discuss which of the following two
approaches would be preferable in everyday programming:

if( *p++ == 6 ) ++amount;

or:

amount += (*p++ == 6);



In an effort to say what you mean I prefer the former; it says exactly
what is intended. The later is more cryptic and full of trickery. The
fact that you have to ask only proves the point.


这篇关于从bool到int:1或0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆