在 C++ 中使用指针交换数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3393518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:45:09  来源:igfitidea点击:

Swap arrays by using pointers in C++

c++arrayspointersswap

提问by thornate

I have two arrays of pointers to doubles that I need to swap. Rather than just copy the data within the arrays, it would be more efficient just to swap the pointers to the arrays. I was always under the impression that array names were essentially just pointers, but the following code receives a compiler error:

我有两个指向需要交换的双精度指针数组。与其只是复制数组中的数据,不如将指针交换到数组中会更有效。我一直认为数组名称本质上只是指针,但以下代码收到编译器错误:

double left[] = {1,2,3};
double right[] = {9,8,7};

double * swap = left;
left = right; // Error "ISO C++ forbids assignment of arrays"
right = swap; // Error "incompatible types in assignment of `double*' to `double[((unsigned int)((int)numParameters))]'"

Creating the arrays dynamically would solve the problem, but can't be done in my application. How do I make this work?

动态创建数组可以解决问题,但不能在我的应用程序中完成。我如何使这项工作?

回答by Daniel

double array_one[] = {1,2,3};
double array_two[] = {9,8,7};

double *left = array_one;
double *right = array_two;

double * swap = left;
left = right;
right = swap;

Works nicely.

很好用。

edit:The definitions array_one and array_two shouldn't be used and the doubleleft and doubleright should be as public as your original left and right definitions.

编辑:不应使用定义 array_one 和 array_two ,并且双左和双右应该与原始左右定义一样公开。

回答by 5ound

Arrays are not the same as pointers and cannot be swapped in the way you describe. To do the pointer swap trick, you must use pointers, either dynamically allocate the memory, or use pointers to access the data (in the way Daniel has described).

数组与指针不同,不能以您描述的方式交换。要进行指针交换技巧,您必须使用指针,或者动态分配内存,或者使用指针访问数据(如 Daniel 所描述的那样)。

回答by Cubbi

C-style arrays are not pointers, but like most objects, they can be swapped with the standard std::swap():

C 风格的数组不是指针,但像大多数对象一样,它们可以与标准的 交换std::swap()

#include <iostream>
#include <utility>
int main()
{
        double array_one[] = {1,2,3};
        double array_two[] = {9,8,7};
        std::swap(array_one, array_two);
        std::cout << "array_one[0] = " << array_one[0] << '\n';
        std::cout << "array_two[0] = " << array_two[0] << '\n';
}

Actually, looks like std::swap() for arrays is only defined in C++0x (20.3.2), so nevermind. The correct answer is, for both arrays in scope and arrays as pointers to first elements:

实际上,数组的 std::swap() 看起来只在 C++0x (20.3.2) 中定义,所以没关系。正确的答案是,对于作用域中的数组和作为指向第一个元素的指针的数组:

 std::swap_ranges(array_one, array_one + 3, array_two);

回答by MSalters

One of the easiest ways to convince people that they're not pointers, and not easily swapped, is to show the following code:

说服人们他们不是指针并且不容易交换的最简单方法之一是显示以下代码:

struct ex {
  char c[4];
  double d[3];
};
struct ex a = {"foo", {1.0, 2.0, 3.0} };
struct ex b = {"bar", {6,7,8} };

Now clearly a.dand b.dare arrays. Swapping them will involve hard work, as the array {6,7,8} has to end in memory after a.c=="foo"and that means copying 3 doubles. There's no pointer in the picture.

现在显然a.db.d是数组。交换它们需要艰苦的工作,因为数组 {6,7,8} 之后必须在内存中结束a.c=="foo",这意味着复制 3 个双精度数。图片中没有指针。

回答by UnknownGosu

You can pass both pointers to arrays by references, and in case pointers are not const, you can just swap them:

您可以通过引用将两个指针传递给数组,如果指针不是常量,您可以交换它们:

void swap(char * & first, char * & second)
{
     std::swap(first, second);
}

回答by Charles Ma

Try this

尝试这个

double *right = (double[]){9,8,7};
double *left = (double[]){8,2,3};   

回答by KedarX

When you declare an array, the name is a pointer, which cannot be altered.

声明数组时,名称是指针,不能更改。

Ex:

前任:

int array[10];
int *p;

p = array; // legal
array = p; // illegal; array is a constant pointer which can't be altered.

The only way you can achieve the swap is using new pointers to the array.

实现交换的唯一方法是使用指向数组的新指针。

This should help you:

这应该可以帮助您:

SO question on array name as pointer

关于数组名称作为指针的问题