简体   繁体   English

无法找出int [3] [3]与int(* const)[3]之间的区别

[英]can't figure out difference between int[3][3] and int(* const)[3]

I have this code: 我有以下代码:

int board[3][3];        // I am ommiting initialization code for board here so as not to clutter it.
typedef std::vector<std::pair<int [3][3], int> > History;
History hist = History();
const std::pair<int[3][3], int> p = std::make_pair(board, cell);
hist.push_back(p);

but compiling it with g++ gives me this error which I can't understand: 但是用g ++编译会给我这个我无法理解的错误:

In file included from /usr/include/c++/4.7/bits/stl_algobase.h:65:0,
                 from /usr/include/c++/4.7/vector:61,
                 from performancesystem.h:29:
/usr/include/c++/4.7/bits/stl_pair.h: In instantiation of
    ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = 
    int (*)[3]; _U2 = int; _T1 = int [3][3]; _T2 = int]’:
Test.cpp:74:65:   required from here
/usr/include/c++/4.7/bits/stl_pair.h:111:39: error: incompatible types in
    assignment of ‘int (* const)[3]’ to ‘int [3][3]’

I always had this problem understanding the difference between pointers and arrays in C++. 我总是在理解C ++中的指针和数组之间的区别时遇到这个问题。 Shouldn't they be the same thing? 他们不应该是同一个人吗? Can anyone help me please? 谁能帮我吗?

The problem in your case is not array-pointer difference (which is big). 您遇到的问题不是数组指针差异(很大)。

The problem is that arrays cannot be by-value-copied in assignment. 问题在于数组不能在赋值中按值复制。 Wrapping the C array in a class would solve it, or better - use a standard array. 将C数组包装在类中可以解决,或者更好-使用标准数组。

In some situations arrays can be converted to pointers. 在某些情况下,数组可以转换为指针。 There is never any conversion the other way around which is what you are trying. 您尝试的其他方法永远不会发生任何转换。

To move forward you should stop using arrays directly. 要继续前进,您应该停止直接使用数组。 They are fairly useless in C++. 它们在C ++中相当无用。 I would write a new class (or struct if you prefer) 我会写一个新的类(如果愿意,也可以写一个struct)

class Board
{
...
private:
   int sq[3][3];
};

typedef std::vector<std::pair<Board, int> > History;

This way you will stop using useless arrays, and you won't have the confusion over arrays and pointers. 这样,您将停止使用无用的数组,并且不会对数组和指针感到困惑。

Can't blame you, as the difference of array vs. pointer realizes typically only in dynamically sized structures. 与指针只有在动态调整结构通常实现不能怪你,因为阵列的区别。

The array int a[3][4][5][6] eg is allocated linearly from memory. 数组int a [3] [4] [5] [6]例如是从内存中线性分配的。 It contains 3*4*5*6 units and where a[2][3][4][0] ... a[2][3][4][5] are in 6 consecutive indices and where a[0][1][n][5] are each 6 elements apart and where a[1][n][3][6] are each 5*6 elements apart and so on. 它包含3 * 4 * 5 * 6个单位,其中a [2] [3] [4] [0] ... a [2] [3] [4] [5]位于6个连续索引中,其中a [ 0] [1] [n] [5]每隔6个元素,而a [1] [n] [3] [6]每隔5 * 6个元素,依此类推。

Pointers however provide 'indirection'. 但是,指针提供“间接”。 The multidimensional array can be implemented by first_level array a[3] providing 3 pointers (indirections) to next level arrays b[4], c[4] and d[4], each of which provide indirection to next level and so on. 多维数组可以通过first_level数组a [3]实现,该数组提供指向下一级数组b [4],c [4]和d [4]的3个指针(间接),每个指针提供到下一级的间接指示,依此类推。

In many real life application (eg a table of pointer to strings) one really can't see the difference, as the syntax hides the details: 在许多现实生活中的应用程序(例如,指向字符串的指针表)中,由于语法隐藏了细节,因此确实看不到区别:

int main(int ac, char **av) { // one can get third character of third string with av[2][2] , even though the prototype can be represented with int main(int ac, char *av[]); int main(int ac, char **av) { //即使原型可以用int main(int ac, char *av[]);表示,也可以使用av[2][2]获得第三个字符串的第三个字符。 int main(int ac, char *av[]); , but not with int main(int ac, char av[][]); ,但不能使用int main(int ac, char av[][]); as the dimension of the table av can't possibly be determined from that syntax. 因为表av的大小可能无法通过该语法确定。

The root of the problem is the way in which C and C++ deal with multidimensional arrays. 问题的根源是C和C ++处理多维数组的方式。 The problems magnify when const is involved. 当涉及const时,问题会放大。

That std::pair<int [3][3], int> is pair of values, the second of which is an int and the first of which is a pointer to an array of int[3] . std::pair<int [3][3], int>是一对值,其中第二个是int ,第一个是指向int[3]数组的指针。 That first element is problematic for a number of reasons. 由于许多原因,第一个要素是有问题的。 One is that it's a pointer. 一个是它是一个指针。 Another is that const does weird things to multidimensional arrays. 另一个是const确实使多维数组变得奇怪。 That's the problem you ran into. 那就是你遇到的问题。

What you want is a 3x3 array as an object. 您想要的是一个3x3数组作为对象。 So -- make it an object: 所以-使其成为对象:

struct Board {
   int arr[3][3];
};

Now your pair becomes std::pair<Board,int> , your vector a std::vector<std::pair<Board, int> > . 现在您的对变成std::pair<Board,int> ,向量成为std::vector<std::pair<Board, int> > You can now safely construct a Board , int pair out of local variables because the Board is copied. 现在您可以安全地从局部变量构造一个Boardint对,因为Board被复制了。 You couldn't do that safely with raw C multidimensional arrays because that first element would have been a pointer. 您不能使用原始C多维数组安全地执行此操作,因为第一个元素将是指针。

Your board defines a constant pointer. 您的board定义了一个常量指针。 The int (* const)[3] is actually the same as a int[n][3] variable: Just a pointer to a array of 3 integers (although the first is a constant one)! 实际上, int (* const)[3]int[n][3]变量相同:只是一个指向3个整数的数组的指针(尽管第一个是常数1)!
You have three options: 您有三种选择:

  • Define the T1 in the template as a int (*const)[3] 将模板中的T1定义为int (*const)[3]
  • Define the your board as a dynamically allocated array. 将您的board定义为动态分配的阵列。 (with int (*board)[3] = new int[3][3]; ) (使用int (*board)[3] = new int[3][3];
  • DANGEROUS: const_cast<> your board to a non-const variable. 危险:const_cast会<>你的board到一个非const变量。 (Better don't do it :D ) (最好不要这样做:D)

LG ntor LG ntor

PS: However, I would also recommend to write a class for this one and overload the []-operator. PS:但是,我也建议为此编写一个类并重载[]运算符。 Much more state of the art ;) 先进的技术 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 const int*、const int * const 和 int const * 之间有什么区别? - What is the difference between const int*, const int * const, and int const *? 数组声明中的const int和int之间的区别? - Difference between const int and int in an array declaration? unordered_map <const T,int>和map <const T,int>之间的区别 - difference between unordered_map<const T, int> and map<const T, int> const int &amp;x = 4 和 const int x = 4 的区别 - Difference between const int &x = 4 and const int x = 4 int const函数(参数),int函数(const参数)和int函数(参数)const之间有什么区别? - What is the difference between int const function(parameters), int function(const parameters), and int function(parameters) const? 传递(int x)和(const int&x)之间的区别 - Difference between passing (int x) and (const int &x) std :: map之间有区别吗? <int, int> 和std :: map <const int, int> ? - Is there a difference between std::map<int, int> and std::map<const int, int>? 什么是const int const&和const int&C ++之间的区别? - Whats the difference between const int const& and const int& in C++? “const int&jj”和“int&const jj”有什么区别? - What is the difference between “const int& jj” and “int& const jj”? C++ 中 `int* const&amp; x` 和 `int* const x` 之间的区别 - Difference between `int* const& x` and `int* const x` in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM