C++ 优先级表

来源:互联网 发布:matlab将数据写入矩阵 编辑:程序博客网 时间:2024/06/02 06:20

The operators at the top of this list are evaluated first. Operators within a group have the same precedence. All operators have left-to-right associativity unless otherwise noted.

Operator Description Example
Group 1 (no associativity)
::Scope resolution operatorClass::age = 2;
Group 2
()Function callisdigit('1')
[]Array accessarray[4] = 2;
->Member access from a pointerptr->age = 34;
.Member access from an objectobj.age = 34;
++Post-incrementfor( int i = 0; i < 10; i++ ) cout << i;
--Post-decrementfor( int i = 10; i > 0; i-- ) cout << i;
Group 3 (right-to-left associativity)
!Logical negationif( !done ) …
~Bitwise complementflags = ~flags;
++Pre-incrementfor( i = 0; i < 10; ++i ) cout << i;
--Pre-decrementfor( i = 10; i > 0; --i ) cout << i;
-Unary minusint i = -1;
+Unary plusint i = +1;
*Dereferenceint data = *intPtr;
&Address ofint *intPtr = &data;
(type)Cast to a given typeint i = (int) floatNum;
sizeofReturn size of an objectint size = sizeof(floatNum);
Group 4
->*Member pointer selectorptr->*var = 24;
.*Member object selectorobj.*var = 24;
Group 5
*Multiplicationint i = 2 * 4;
/Divisionfloat f = 10.0 / 3.0;
%Modulusint rem = 4 % 3;
Group 6
+Additionint i = 2 + 3;
-Subtractionint i = 5 - 1;
Group 7
<<Bitwise shift leftint flags = 33 << 1;
>>Bitwise shift rightint flags = 33 >> 1;
Group 8
<Comparison less-thanif( i < 42 ) …
<=Comparison less-than-or-equal-toif( i <= 42 ) ...
>Comparison greater-thanif( i > 42 ) …
>=Comparison geater-than-or-equal-toif( i >= 42 ) ...
Group 9
==Comparison equal-toif( i == 42 ) ...
!=Comparison not-equal-toif( i != 42 ) …
Group 10
&Bitwise ANDflags = flags & 42;
Group 11
^Bitwise exclusive OR (XOR)flags = flags ^ 42;
Group 12
|Bitwise inclusive (normal) ORflags = flags | 42;
Group 13
&&Logical ANDif( conditionA && conditionB ) …
Group 14
||Logical ORif( conditionA || conditionB ) ...
Group 15 (right-to-left associativity)
? :Ternary conditional (if-then-else)int i = (a > b) ? a : b;
Group 16 (right-to-left associativity)
=Assignment operatorint a = b;
+=Increment and assigna += 3;
-=Decrement and assignb -= 4;
*=Multiply and assigna *= 5;
/=Divide and assigna /= 2;
%=Modulo and assigna %= 3;
&=Bitwise AND and assignflags &= new_flags;
^=Bitwise exclusive or (XOR) and assignflags ^= new_flags;
|=Bitwise normal OR and assignflags |= new_flags;
<<=Bitwise shift left and assignflags <<= 2;
>>=Bitwise shift right and assignflags >>= 2;
Group 17
,Sequential evaluation operatorfor( i = 0, j = 0; i < 10; i++, j++ ) …
原创粉丝点击