How does the following statement evaluate?
I am having problem understanding as to what the following statement means(not the tertiary operator part) -
return(((*data)*(tmp_left)<(*data)*(tmp_right))?tmp_left:tmp_right);
where data is a pointer and tmp_left is a variable of type int. This line is in relation to a tree 开发者_如何转开发structure, if that helps.
To be exact I want to know what does (*data)
mean? Is it a value pointed to by the pointer or a member variable accessed by it? What does the asterisk '*' between the pointer and a variable signify? *
(tmp_left)
Without knowing the data types, and knowing that it's used in a tree (assuming it's a BST) this looks like a comparison of data values.
In essence, when you have a BST you will either insert nodes to the left of the immediate node or the right of the immediate node. It looks like the data value is contained within the node and is the pointer. So, (*data) derferences the node. The (*data) * tmp_left multiplies the values.
The rest is a comparison statement. If the left hand side of the expression is less than the right hand size, use the tmp_left data value. Otherwise, continue to the right.
(*data) * (tmp_left) is dereferencing the value of the data pointer, and multiplying the resulting value by tmp_left.
Overall, if 'd' is the value pointed to by data, this says "if (d * tmp_left) is less than (d * tmp_right), return tmp_left, else return tmp_right".
(*data)*(tmp_left)
- as data
is a pointer, (*data)
dereferences it (gets the value, pointed by data
, then the value is multiplied by tmp_left
(which, in your case, as you said, is int
)
The other parts should be clear :)
EDIT: you can easily read such things by separating then into different parts
return(((*data)*(tmp_left)<(*data)*(tmp_right))?tmp_left:tmp_right);
1. (*data) - get the value, pointed by data 2. tmp_left is an int 3. multiply (*data) and tmp_left (lets say the value is X) 4. (*data) - get the value, pointed by data 5. tmp_right is an int 6. multiply (*data) and tmp_right (lets say the value is Y) 7. now compare X and Y 8. if X < Y, return tmp_left, else return tmp_right
What does the asterisk '*' between the pointer and a variable signify?
Multiplication (in this context)
return(((*data)*(tmp_left)<(*data)*(tmp_right))?tmp_left:tmp_right);
Lets simplify this, as,
int a = (*data); //get the value from the address pointed to be 'data'
int x = a * (tmp_left); //multiplication
int y = a * (tmp_right); //multiplication
//substitute x and y in the original expression, it becomes
return ( x < y ? tmp_left : tmp_right );
Now it looks simple and easy to understand, right?
The *
before a variable name as in *data
is to dereference a pointer.
The *
between them is for multiplication.
This code returns the smallest of tmp_right
and tmp_left
.
It also appears to be functionally identical to:
return((tmp_left)<(tmp_right)?tmp_left:tmp_right);
I'm not sure what *data
refers to, but unless something weird is going on the multiplication doesn't actually have any effect, as x*y < x*z
is equivalent to y < z
.
When you see a complicated statement like that, trying simplifying it. First, break out the ternary operator and add some whitespace:
if(((*data) * (tmp_left) < (*data) * (tmp_right)))
{
return tmp_left;
}
else
{
return tmp_right;
}
Next factor out *data and remove the redundant parens:
data_type& d = *data;
if((d * tmp_left < d * tmp_right))
{
return tmp_left;
}
else
{
return tmp_right;
}
At this point it should be obvious that the *
is multiplication, and exactly what's happening in the function. If *data
is always positive then you could factor that term out completely but that's probably not the case.
精彩评论