在c++中链表的节点上使用括号有什么意义?
What is the significance of using a bracket on the node of the linked list in c++?
在下面的代码中,mn 函数中有一个节点指针 naya
。在 if
条件下,naya
指针第一次指向 null,我们正在尝试访问它的数据。如果我们尝试在 naya
指针周围没有括号的情况下执行相同的代码,则会出现如下错误:
prog.cpp: In function ‘void mn(Node*, Node**)’:
prog.cpp:66:50: error: request for member ‘data’ in ‘* naya’, which is of pointer type ‘Node*’ (maybe you meant to use ‘->’ ?)
if(*naya == NULL || temp -> data <= *naya -> data)
^*
但是当我们使用括号时它工作正常。为什么?
完整代码如下:
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next = NULL;
Node(int x) {
data = x;
next = NULL;
}
};
void mn(Node* temp, Node** naya) {
Node* current;
if (*naya == NULL || temp->data <= (*naya)->data) {
temp->next = *naya;
*naya = temp;
} else {
current = *naya;
while (current->next != NULL && (current->next->data < temp->data)) {
current = current->next;
}
temp->next = current->next;
current->next = temp;
}
}
Node* isort(Node* head) {
Node* temp = head;
Node* naya = NULL;
while (temp != NULL) {
Node* nex1 = temp->next;
mn(temp, &naya);
temp = nex1;
}
return naya;
}
void printll(Node* head) {
Node* temp = head;
while (temp != NULL) {
cout << temp->data;
temp = temp->next;
}
}
int main() {
Node *head = NULL, *temp = NULL;
int a;
cin >> a;
for (int i = 0; i < a; i++) {
int x;
cin >> x;
Node* newnode = new Node(x);
if (head == NULL) {
head = newnode;
temp = head;
} else {
temp->next = newnode;
temp = temp->next;
}
}
head = isort(head);
printll(head);
}
之所以要这样写,是因为operator precedence决定了用哪个操作数来执行哪些操作。从link可以看出,'member access'或->
默认绑定在'Indirection (dereference)'或*a
之前。通过将取消引用操作括在方括号中,您可以指定您希望它在成员访问之前首先绑定。
为了更具体,在您的示例中 naya
是指向指针的指针。默认情况下,C++ 尝试首先绑定成员访问操作的操作数(即 naya->data
)。如果我们在这里添加括号使顺序明确,它看起来像这样:
*(naya->data)
这具有取消引用 naya
的效果,然后在取消引用的对象中查找 data
成员变量。虽然取消引用的对象是指针,因此它没有 data
成员。如果它在那里没有出错,它将继续尝试取消引用数据成员的值(表达式的 *
部分)。
当你输入 (*naya)->data
时,你是在告诉 C++ 它应该使用 *
取消对 naya
的引用,而不是默认情况下的 (naya->data)
。 (*naya)
是指向Node对象的指针,所以->
会成功找到数据成员。
在下面的代码中,mn 函数中有一个节点指针 naya
。在 if
条件下,naya
指针第一次指向 null,我们正在尝试访问它的数据。如果我们尝试在 naya
指针周围没有括号的情况下执行相同的代码,则会出现如下错误:
prog.cpp: In function ‘void mn(Node*, Node**)’:
prog.cpp:66:50: error: request for member ‘data’ in ‘* naya’, which is of pointer type ‘Node*’ (maybe you meant to use ‘->’ ?)
if(*naya == NULL || temp -> data <= *naya -> data)
^*
但是当我们使用括号时它工作正常。为什么?
完整代码如下:
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next = NULL;
Node(int x) {
data = x;
next = NULL;
}
};
void mn(Node* temp, Node** naya) {
Node* current;
if (*naya == NULL || temp->data <= (*naya)->data) {
temp->next = *naya;
*naya = temp;
} else {
current = *naya;
while (current->next != NULL && (current->next->data < temp->data)) {
current = current->next;
}
temp->next = current->next;
current->next = temp;
}
}
Node* isort(Node* head) {
Node* temp = head;
Node* naya = NULL;
while (temp != NULL) {
Node* nex1 = temp->next;
mn(temp, &naya);
temp = nex1;
}
return naya;
}
void printll(Node* head) {
Node* temp = head;
while (temp != NULL) {
cout << temp->data;
temp = temp->next;
}
}
int main() {
Node *head = NULL, *temp = NULL;
int a;
cin >> a;
for (int i = 0; i < a; i++) {
int x;
cin >> x;
Node* newnode = new Node(x);
if (head == NULL) {
head = newnode;
temp = head;
} else {
temp->next = newnode;
temp = temp->next;
}
}
head = isort(head);
printll(head);
}
之所以要这样写,是因为operator precedence决定了用哪个操作数来执行哪些操作。从link可以看出,'member access'或->
默认绑定在'Indirection (dereference)'或*a
之前。通过将取消引用操作括在方括号中,您可以指定您希望它在成员访问之前首先绑定。
为了更具体,在您的示例中 naya
是指向指针的指针。默认情况下,C++ 尝试首先绑定成员访问操作的操作数(即 naya->data
)。如果我们在这里添加括号使顺序明确,它看起来像这样:
*(naya->data)
这具有取消引用 naya
的效果,然后在取消引用的对象中查找 data
成员变量。虽然取消引用的对象是指针,因此它没有 data
成员。如果它在那里没有出错,它将继续尝试取消引用数据成员的值(表达式的 *
部分)。
当你输入 (*naya)->data
时,你是在告诉 C++ 它应该使用 *
取消对 naya
的引用,而不是默认情况下的 (naya->data)
。 (*naya)
是指向Node对象的指针,所以->
会成功找到数据成员。