c++ 中的数组复制反转

发布于 2024-11-06 12:55:20 字数 95 浏览 0 评论 0原文

有没有办法通过使用 C++ 中的 while 循环将数组以相反的顺序复制到另一种方式? 我很确定我知道如何使用 for 循环,但我很好奇是否有人知道使用 while 循环的方法

Is there a way to copy an array to another way in reverse order by using a while loop in c++??
I'm pretty sure I know how to do one with a for loop, but I'm curious if anyone knows of a way by using a while loop

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无边思念无边月 2024-11-13 12:55:20

为什么不做这样的事情呢?

#include <algorithm>

int src[] = {1, 2, 3, 4, 5};
int dst[5];

std::reverse_copy(src, src+5, dst);

Why not something like this?

#include <algorithm>

int src[] = {1, 2, 3, 4, 5};
int dst[5];

std::reverse_copy(src, src+5, dst);
嘿看小鸭子会跑 2024-11-13 12:55:20
int anArray = {1, 2, 3, 4, 5};
int reverseArray[5];
int count = 4;
int place = 0;
while(place < 5) {
  reverseArray[place] = anArray[count];
  count--;
  place++;
}  
int anArray = {1, 2, 3, 4, 5};
int reverseArray[5];
int count = 4;
int place = 0;
while(place < 5) {
  reverseArray[place] = anArray[count];
  count--;
  place++;
}  
梅窗月明清似水 2024-11-13 12:55:20

正如您所说,您已经使用 for 循环完成,您可以按照以下步骤将其转换为 while 循环。

for(int i = sizeof(arr) - 1; i >= 0; i--)
{
  // your logic
}

现在将其转换为,

int i = sizeof(arr);
for(; i >= 0; )
{
  // your logic
  i--;
}

只需将 for 替换为 while 并删除大括号内的 ; 即可。

int i = sizeof(arr);
while(i >= 0)
{
  // your logic
  i--;
}

As you said that you have done using for loop, you can follow following steps to convert it to while loop.

for(int i = sizeof(arr) - 1; i >= 0; i--)
{
  // your logic
}

now convert it to,

int i = sizeof(arr);
for(; i >= 0; )
{
  // your logic
  i--;
}

simply replace for with while and remove ; within the braces.

int i = sizeof(arr);
while(i >= 0)
{
  // your logic
  i--;
}
明月夜 2024-11-13 12:55:20

您可以使用 std::reverse 反转同一个数组,使用 std::reverse_copy 反转到另一个输出数组,如下所示:

int main() {
        int a[]= {1,2,3,4,5,6,7,8,9,10};
        const int size = sizeof(a)/sizeof(int);
        int b[size];

        //copying reverse to another array
        reverse_copy(a, a + size, b);
        cout << "b = {";
        copy(b, b + size, ostream_iterator<int>(cout, ", "));
        cout << "}" << endl;

        //reverse the same array
        reverse(a, a + size);
        cout << "a = {";
        copy(a, a + size, ostream_iterator<int>(cout, ", "));
        cout << "}" << endl;
        return 0;
}

Output:

b = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, }
a = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, }

Demo : http://www.ideone.com/Fe5uj

You can use std::reverse for reversing the same array and std::reverse_copy for reversing to another output array as:

int main() {
        int a[]= {1,2,3,4,5,6,7,8,9,10};
        const int size = sizeof(a)/sizeof(int);
        int b[size];

        //copying reverse to another array
        reverse_copy(a, a + size, b);
        cout << "b = {";
        copy(b, b + size, ostream_iterator<int>(cout, ", "));
        cout << "}" << endl;

        //reverse the same array
        reverse(a, a + size);
        cout << "a = {";
        copy(a, a + size, ostream_iterator<int>(cout, ", "));
        cout << "}" << endl;
        return 0;
}

Output:

b = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, }
a = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, }

Demo : http://www.ideone.com/Fe5uj

活雷疯 2024-11-13 12:55:20

最近有几个类似的问题。我想知道这是作业还是面试问题。这是一个答案:

#define ELEMENT_COUNT(a) (sizeof((a))/sizeof((a)[0]))


int anArray[] = { 1, 2, 3, 4, 5 };
int reverseArray[ELEMENT_COUNT(anArray)];
int n = ELEMENT_COUNT(anArray);
int i = 0;
while(n--)
    reverseArray[i++] = anArray[n];

我认为这可能是为了看看您是否理解何时评估 i++ 和 n-- 这样的表达式。

There's been a few questions similar to this recently. I wonder if it is homework or an interview question somewhere. Here's one answer:

#define ELEMENT_COUNT(a) (sizeof((a))/sizeof((a)[0]))


int anArray[] = { 1, 2, 3, 4, 5 };
int reverseArray[ELEMENT_COUNT(anArray)];
int n = ELEMENT_COUNT(anArray);
int i = 0;
while(n--)
    reverseArray[i++] = anArray[n];

I think it might be probing to see if you understand when expression like i++ and n-- are evaluated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文