English 中文(简体)
C++的明显逆转
原标题:string reverse in C++
  • 时间:2009-09-22 17:12:54
  •  标签:

我在现场发现这部法典,似乎提交人已经走过了很长的路,我很不时理解实际的雕刻,以及相反的情况:

void strrev2(char *str)
{
        if( str == NULL )
                return;

        char *end_ptr = &str[strlen(str) - 1];
        char temp;
        while( end_ptr > str )
        {
                temp = *str;
                *str++ = *end_ptr;
                *end_ptr-- = temp;
        }
}

让我说一下,你把“测试”改为“试验”。

第一胎:

*end_ptr =  g ;

temp =  t 
*str =  g  // is it first assigned and then incremented to point to the next location?
*end_ptr =  t  // is it first assigned and then decremented to point to the previous location?

第二次发生的情况如何? 我花了不起时间,因为我认为,在这个方面:

char *end_ptr = &str[strlen(str) - 1];

<代码>end_ptr将只包含一封信的地址,因此*end_ptr work?

Anyway, if someone can explain this to me in some graphical way.. Thanks.

问题回答

。 每次环形时,其特性都会被放弃,每一次都朝str的中转。 当他们见面(或穿过)时, lo就终止。

这里的情况......

1. testing        s is str
   ^     ^        e is end_ptr
   s     e

2. gestint
    ^   ^
    s   e

3. gnitset
      ^
      se

4. done!

(这样,就是在认为人才培养者提出好面试问题的公司开始共同接受询问。) 这个问题现在不可避免地继续......,你将如何使用同样的原则来推翻判决中的字句,同时又不改变每一字的文字?)

我喜欢亚历克文的艺术,我想再作一点阐述,因此,从这一同样解释来看,这只是一个略微多的版本。

To Start off:

| t | e | s | t | i | n | g |  | str  |      |      |
  ^                              | 0x00 |      |      |
 str
Then after the line
char *end_ptr = &str[strlen(str) - 1];
since the strlen(str) returns 7,
end_ptr = &str[6]
So:
| t | e | s | t | i | n | g |  | str  | end  | temp |
  ^                       ^      | 0x00 | 0x06 |      |
 str                     end
once it enter the loop it checks to see that end is a bigger address than str, and then assigns the value at the address str is pointing to to temp:
| t | e | s | t | i | n | g |  | str  | end  | temp |
  ^                       ^      | 0x00 | 0x06 |   t  |
 str                     end
It then continues by assigning the value at end to the address at str:
| g | e | s | t | i | n | g |  | str  | end  | temp |
  ^                       ^      | 0x00 | 0x06 |   t  |
 str                     end
Then advances the pointer str to the next address
| g | e | s | t | i | n | g |  | str  | end  | temp |
      ^                   ^      | 0x01 | 0x06 |   t  |
     str                 end
Assigns the value of temp to the address at end:
| g | e | s | t | i | n | t |  | str  | end  | temp |
      ^                   ^      | 0x01 | 0x06 |   t  |
     str                 end
Then finally decrements end and loops back to the top of the while statement:
| g | e | s | t | i | n | t |  | str  | end  | temp |
      ^               ^          | 0x01 | 0x05 |   t  |
     str             end
etc. etc. etc.

点数在派任后被增减,因此起算点随着终点站向后运行,起算点数在上升。

在“测试”、“t”和“g”、“g”、“e”和“n”中进行首次代谢交流,直至点人在字中开会。

首先,它避开了第一个和最后一个阶段,同时增加了第一个要素的点,从而把第二点推向第二点,把点推到最后一点,以便最后一点点。 只要“最后”性质在“第一”之后就继续如此。 如果不是这样,它就做了。

不好意思,我不是一个好的 ASCII 艺术家,所以没有图形给你看。

附带说明:这样做非常容易:

#include <algorithm>
void strrev2(char *str)
{
    std::reverse(str, str + strlen(str));
}




相关问题
热门标签