在C ++中,如何使用指针将每个元素向右移动一次? [英] In C++, how can one move each element one to the right using only pointers?

查看:135
本文介绍了在C ++中,如何使用指针将每个元素向右移动一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,此数组例如[1,2,3,4,5]将成为此数组[5,1,2,3,4]



这是我想出的,但它不工作:

  int * ptr = arr; // initialize to first element 
int * ptr2 = arr + 1; //初始化它到第二个元素

while(n> 0)//继续做它,直到大小完成
{
* ptr2 = * ptr;
++ ptr2;
ptr ++;
n - ; //
}


解决方案>

仅使用指针是一个非常模糊的要求。下面的现代解决方案以某种方式也使用只有指针,因为 std :: rotate 对迭代器和指针操作是迭代器: / p>

  #include< algorithm> 
#include< iostream>

int main()
{
int arr [] = {1,2,3,4,5};

使用std :: begin;
using std :: end;
std :: rotate(begin(arr),end(arr) - 1,end(arr));

for(auto&& element:arr)
{
std :: cout<元素<< \\\
;
}
}

但我觉得给这个作业的老师不会因为某种原因而感到满意。至于您的解决方案:


  int * ptr = arr; // initialize to first element 
int * ptr2 = arr + 1; //初始化它到第二个元素

while(n> 0)//继续做它,直到大小完成
{
* ptr2 = * ptr;
++ ptr2;
ptr ++;
n - ; //
}


(我将假设你的 n 在循环开始之前是4,因为否则你将有超过4次迭代,并在最后一个行为中遇到未定义的行为。)



在循环的第一次迭代中:




  • arr [1] 变为 arr [0]

  • ptr2 是指向 arr [2]

  • ptr 指向 arr [1]



第一次迭代后的数组: [1,3,4,5]



可以看到,此时,包含值2的元素已经丢失。



在循环的第二次迭代中:




  • arr [2] 变为 arr [1] code>

  • ptr2 指向 arr [3]

  • ptr 指向 arr [2]



第二次迭代后的数组: [1,1,1,4,5]



现在包含值3的元素也将丢失。现在的模式是清晰的;你会丢失所有的元素,并简单地覆盖一切与1s。



你必须找到一种方式来保存要覆盖的元素。或者最好只是使用 std :: rotate


for example, this array for example, [1,2,3,4,5] would become this array [5,1,2,3,4]

this is what I have come up with, but it does not work:

 int  *ptr = arr; //initialize to first element
    int  *ptr2 = arr+1; //initialize it to second element

   while (n >0) // keep doing it until size is done with
   {
       *ptr2 = *ptr; 
       ++ptr2;
       ptr ++;
       n--;//
   }

解决方案

"using only pointers" is a pretty vague requirement. The following, modern solution in a certain way also uses "only pointers", because std::rotate operates on iterators and pointers are iterators:

#include <algorithm>
#include <iostream>

int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };

    using std::begin;
    using std::end;
    std::rotate(begin(arr), end(arr) - 1, end(arr));

    for (auto&& element : arr)
    {
        std::cout << element << "\n";
    }
}

Yet I feel that the teacher who gave you this assignment would not be happy with it for some reason. As for your solution:

 int  *ptr = arr; //initialize to first element
    int  *ptr2 = arr+1; //initialize it to second element

   while (n >0) // keep doing it until size is done with
   {
       *ptr2 = *ptr; 
       ++ptr2;
       ptr ++;
       n--;//
   }

(I will assume that your n is 4 before the loops begins, because otherwise you will have more than 4 iterations and run into undefined behaviour in the last one.)

In the first iteration of the loop:

  • arr[1] becomes arr[0].
  • ptr2 is made to point to arr[2].
  • ptr is made to point to arr[1].

Array after first iteration: [1, 1, 3, 4, 5]

As you can see, at this point, the element containing value 2 is already lost.

In the second iteration of the loop:

  • arr[2] becomes arr[1].
  • ptr2 is made to point to arr[3].
  • ptr is made to point to arr[2].

Array after second iteration: [1, 1, 1, 4, 5]

Now the element containing value 3 is also lost. The pattern is now clear; you are "losing" all elements and are simply overwriting everything with 1s.

You must find a way to conserve the elements you are overwriting. Or preferably just use std::rotate.

这篇关于在C ++中,如何使用指针将每个元素向右移动一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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