反向迭代 [英] Reverse Iteration

查看:45
本文介绍了反向迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



查看以下功能:


#include< cstddef>

#include< iostream>


模板< typename T,std :: size_t len>

void PrintOutStrings(const char * const(& Array)[len])

{

for(std :: size_t i = 0; i< len; ++ i)std :: cout<<数组[i];

}


假设我们想要向后打印字符串...你怎么用

通常会这样做吗?我有办法做到这一点,我想知道你怎么想

呢?


#include< cstddef>

#include< iostream>


模板< typename T,std :: size_t len>

void PrintOutStringsBackwards(const char * const( & Array)[len])

{

for(std :: size_t i = len - 1; ~i; --i)std :: cout< <数组[i];

}


我的理由是计数器变量i将变得更小并且b
更小。它将变为0,然后当它再次递减时,它将变为0xFFFF(或0xFFFFFFFF,或0xFFFFFFFFFFFFFFF ......)。如果我们然后使用〜反转

位。算子,然后我们得到0;所以循环应该

那么停止。


任何潜在的陷阱?


安静点头批准会做我。


-Tomás


Looking at the following function:

#include <cstddef>
#include <iostream>

template<typename T, std::size_t len>
void PrintOutStrings( const char* const (&Array)[len] )
{
for (std::size_t i = 0; i < len; ++i) std::cout << Array[i];
}

Let''s say we want to print the strings out backwards... how do you
usually do it? I''ve a way of doing it and I''m wondering what you think of
it?

#include <cstddef>
#include <iostream>

template<typename T, std::size_t len>
void PrintOutStringsBackwards( const char* const (&Array)[len] )
{
for (std::size_t i = len - 1; ~i; --i) std::cout << Array[i];
}

My rationale here is that the counter variable "i" will get smaller and
smaller. It will become 0, and then when it''s decremented again, it will
become 0xFFFF (or 0xFFFFFFFF, or 0xFFFFFFFFFFFFFFF...). If we then invert
the bits using the "~" operator, then we''ll get 0; so the loop should
stop then.

Any potential pitfalls?

A quiet nod of approval would do me.

-Tomás

推荐答案

"Tomás" < NU ** @ NULL.NULL>在留言中写道

新闻:ax ****************** @ news.indigo.ie ...

:让我们说我们想要向后打印字符串...你怎么用b $ b:通常这样做?我有办法做到这一点,我想知道你的想法



:它?



:#include< cstddef>

:#include< iostream>



:template< typename T, std :: size_t len>

:void PrintOutStringsBackwards(const char * const(& Array)[len])

:{

: for(std :: size_t i = len - 1; ~i; --i)std :: cout<<数组[i];

:}



:我的理由是计数器变量i将变得更小和

:更小。它将变为0,然后当它再次递减时,它将变为0xFFFF(或0xFFFFFFFF,或0xFFFFFFFFFFFFFFF ......)。如果我们那么

反转

:使用〜的位算子,然后我们得到0;所以循环应该

:那么停止。



:任何潜在的陷阱?


这是好的和便携的,但是过于聪明。在我看来,

有太多活动部件 (/出错的机会):

如果使用有符号整数类型,代码将被破坏


我发现以下循环更简单更多 ; natural":

for(std :: size_t i = len; i--;)std :: cout<<数组[i];


:一个安静的认可对我有用。


就个人而言,我不接受你的版本代码评论。


欢呼,

伊万

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表单

Brainbench MVP for C ++<> http://www.brainbench.com
"Tomás" <NU**@NULL.NULL> wrote in message
news:ax******************@news.indigo.ie...
: Let''s say we want to print the strings out backwards... how do you
: usually do it? I''ve a way of doing it and I''m wondering what you think
of
: it?
:
: #include <cstddef>
: #include <iostream>
:
: template<typename T, std::size_t len>
: void PrintOutStringsBackwards( const char* const (&Array)[len] )
: {
: for (std::size_t i = len - 1; ~i; --i) std::cout << Array[i];
: }
:
: My rationale here is that the counter variable "i" will get smaller and
: smaller. It will become 0, and then when it''s decremented again, it will
: become 0xFFFF (or 0xFFFFFFFF, or 0xFFFFFFFFFFFFFFF...). If we then
invert
: the bits using the "~" operator, then we''ll get 0; so the loop should
: stop then.
:
: Any potential pitfalls?

It is ok and portable, but excessively "smart" in my opinion,
with too many "moving parts" (/opportunities for error):
if a signed integer type is used, code will be broken

I find the following loop to be simpler and more "natural":
for ( std::size_t i = len; i-- ; ) std::cout << Array[i];

: A quiet nod of approval would do me.

Personally, I would not accept your version in a code review.

Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com

Tomás写道:
template< typename T,std :: size_t len>
void PrintOutStrings(const char * const(& Array)[len])
{
for(std :: size_t i = 0; i< len; ++ i)std :: cout<<数组[i];
}


实际上,我认为上述功能已经是错误的。

正确版本看起来像这样:


模板< typename T,std :: size_t len>

void PrintOutStrings(const char * const(& Array)[len] ])

{

std :: copy(数组+ 0,数组+ len,

std :: ostream_iterator< char const *> (std :: cout));

}

假设我们想要向后打印字符串...


....哪种规定了反转版本的样子:


模板< typename T,std :: size_t len>

void PrintOutStrings(const char * const(& Array)[len])

{

std :: copy(std :: reverse_iterator< char const *>(array) + len),

std :: reverse_iterator< char const *>(array + 0),

std :: ostream_iterator< char const *>(std :: cout));

}

for(std :: size_t i = len - 1; ~i; --i)std :: cout<<数组[i];
template<typename T, std::size_t len>
void PrintOutStrings( const char* const (&Array)[len] )
{
for (std::size_t i = 0; i < len; ++i) std::cout << Array[i];
}
Actually, I consider the above function already to be "wrong". The
"correct" version looks like this:

template<typename T, std::size_t len>
void PrintOutStrings( const char* const (&Array)[len] )
{
std::copy(array + 0, array + len,
std::ostream_iterator<char const*>(std::cout));
}
Let''s say we want to print the strings out backwards...
.... which kind of prescribes how the reversed version will look like:

template<typename T, std::size_t len>
void PrintOutStrings( const char* const (&Array)[len] )
{
std::copy(std::reverse_iterator<char const*>(array + len),
std::reverse_iterator<char const*>(array + 0),
std::ostream_iterator<char const*>(std::cout));
}
for (std::size_t i = len - 1; ~i; --i) std::cout << Array[i];




我认为这应该有效。然而,更传统的版本

实际上更不容易出错,并且需要更少考虑


for(std :: size_t i = len; i--;)std :: cout<<数组[i];

-

< mailto:di *********** @ yahoo.com> < http://www.dietmar-kuehl.de/>

< http://www.eai-systems.com> - 高效的人工智能



I think this should work. However, the more conventional version
is actually much less error prone and requires less thinking about:

for (std::size_t i = len; i--; ) std::cout << Array[i];
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence


" Dietmar Kuehl" <二*********** @ yahoo.com>在消息中写道

news:44 ************ @ individual.net ...
"Dietmar Kuehl" <di***********@yahoo.com> wrote in message
news:44************@individual.net...
我认为这应该有效。然而,更传统的版本实际上更不容易出错,并且需要更少的思考

(std :: size_t i = len; i-- ;)std :: cout<<数组[i];
I think this should work. However, the more conventional version
is actually much less error prone and requires less thinking about:

for (std::size_t i = len; i--; ) std::cout << Array[i];




我不认为这种方法有什么特别的错误。

然而,它没有''转换为迭代器域,因为它产生了一个非开始值的
。对于整数来说这样做是可以的,但对于

迭代器则没有。


因为这个原因,我倾向于按如下方式编写循环:


{std :: size_t i = len;而(i){ - i; std :: cout<<阵列[I]; }


是的,它只是一点点,但是(例如)你可以把它翻译成


{const Element * p = Array + len; while(p!= Array){ - p; std :: cout<<

* p;
而不影响其有效性。



I don''t think there''s anything specifically wrong with this approach.
However, it doesn''t translate to the iterator domain, because it generates
an off-the-beginning value. Doing so is OK for integers, but not for
iterators.

For that reason, I would be inclined to write the loop as follows:

{ std::size_t i = len; while (i) { --i; std::cout << Array[i]; }}

Yes it''s a little longer, but (for example) you can translate it to

{ const Element* p = Array + len; while (p != Array) { --p; std::cout <<
*p; }}

without affecting its validity.


这篇关于反向迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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