2011年软考程序员考试复习笔试知识点整理(8)

来源:软件水平考试    发布时间:2012-11-05    软件水平考试视频    评论

  12、C++操作符优先级:

  记忆方法:

  去掉一个最高的,去掉一个最低的,剩下的是一、二、三、赋值;双目运算符中,顺序为算术、关系和逻辑,移位和逻辑位插入其中。

--摘自《C语言程序设计实用问答》

  问题:如何记住运算符的15种优先级和结合性?

  解答:C语言中运算符种类比较繁多,优先级有15种,结合性有两种。

  如何记忆两种结合性和15种优先级?下面讲述一种记忆方法。

  结合性有两种,一种是自左至右,另一种是自右至左,大部分运算符的结合性是自左至右,只有单目运算符、三目运算符的赋值运算符的结合性自右至左。

  优先级有15种,记忆方法如下:

  记住一个最高的:构造类型的元素或成员以及小括号。

  记住一个最低的:逗号运算符。

  剩余的是一、二、三、赋值——意思是单目、双目、三目和赋值运算符。

  在诸多运算符中,又分为:算术、关系、逻辑。

  两种位操作运算符中,移位运算符在算术运算符后边,逻辑位运算符在逻辑运算符的前面。

  再细分如下:

  算术运算符*,/,%高于+,-。

  关系运算符中:>,>=,<和<=高于==,!=。

  逻辑运算符中,除了逻辑求反(!)是单目外,逻辑与(&&)高于逻辑或(||)。

  逻辑位运算符中,除了逻辑按位求反(~)外,按位与(&)高于按位半加(^),高于按位或(|)。

Prece denceOperatorDescriptionExampleOver loadableAssociativity
1::Scope resolution operatorClass::age = 2;noleft to right
2()Function callprintf(“Hello world/n”);yesleft to right
()Member initalizationc_tor(int x, int y) : _x(x), _y(y * 10) {}yes
[]Array accessarray[4] = 2;yes
->Member access from a pointerptr->age = 34;yes
.Member access from an objectobj.age = 34;no
++Post-incrementfor (int i = 0; i < 10; i++) cout << i;yes
--Post-decrementfor (int i = 10; i > 0; i--) cout << i;yes
dynamic_castRuntime-checked type conversionY& y = dynamic_cast<Y&>(x);no
static_castUnchecked type conversionY& y = static_cast<Y&>(x);no
reinterpret_castReinterpreting type conversionint const* p = reinterpret_cast<int const*>(0x1234);no
const_castCast away/Add constnessint* q = const_cast<int*>(p);no
typeidGet type informationstd::type_info const& t = typeid(x);no
3!Logical negationif (!done) ...yesright to left
notAlternate spelling for !
~Bitwise complementflags = ~flags;yes
complAlternate spelling for ~
++Pre-incrementfor (i = 0; i < 10; ++i) cout << i;yes
--Pre-decrementfor (i = 10; i > 0; --i) cout << i;yes
-Unary minusint i = -1;yes
+Unary plusint i = +1;yes
*Dereferenceint data = *intPtr;yes
&Address ofint *intPtr = &data;yes
sizeofSize (of the type) of the operand in bytessize_t s = sizeof(int);no
newDynamic memory allocationlong* pVar = new long;yes
new []Dynamic memory allocation of arraylong* array = new long[20];yes
deleteDeallocating the memorydelete pVar;yes
delete []Deallocating the memory of arraydelete [] array;yes
(type)Cast to a given typeint i = (int)floatNum;yes
4->*Member pointer selectorptr->*var = 24;yesleft to right
.*Member object selectorobj.*var = 24;no
5*Multiplicationint i = 2 * 4;yesleft to right
/Divisionfloat f = 10.0 / 3.0;yes
%Modulusint rem = 4 % 3;yes
6+Additionint i = 2 + 3;yesleft to right
-Subtractionint i = 5 - 1;yes
7<< Bitwise shift leftint flags = 33 << 1;yesleft to right
>> Bitwise shift rightint flags = 33 >> 1;yes
8< Comparison less-thanif (i < 42) ...yesleft to right
<=Comparison less-than-or-equal-toif (i <= 42) ...yes
> Comparison greater-thanif (i > 42) ...yes
>=Comparison greater-than-or-equal-toif (i >= 42) ...yes
9==Comparison equal-toif (i == 42) ...yesleft to right
eqAlternate spelling for ==
!=Comparison not-equal-toif (i != 42) ...yes
not_eqAlternate spelling for !=
10&Bitwise ANDflags = flags & 42;yesleft to right
bitandAlternate spelling for &
11^Bitwise exclusive OR (XOR)flags = flags ^ 42;yesleft to right
xorAlternate spelling for ^
12|Bitwise inclusive (normal) ORflags = flags | 42;yesleft to right
bitorAlternate spelling for |
13&&Logical ANDif (conditionA && conditionB) ...yesleft to right
andAlternate spelling for &&
14||Logical ORif (conditionA || conditionB) ...yesleft to right
orAlternate spelling for ||
15? :Ternary conditional (if-then-else)int i = a > b ? a : b;noright to left
16=Assignment operatorint a = b;yesright to left
+=Increment and assigna += 3;yes
-=Decrement and assignb -= 4;yes
*=Multiply and assigna *= 5;yes
/=Divide and assigna /= 2;yes
%=Modulo and assigna %= 3;yes
&=Bitwise AND and assignflags &= new_flags;yes
and_eqAlternate spelling for &=
^=Bitwise exclusive or (XOR) and assignflags ^= new_flags;yes
xor_eqAlternate spelling for ^=
|=Bitwise normal OR and assignflags |= new_flags;yes
or_eqAlternate spelling for |=
<<=Bitwise shift left and assignflags <<= 2;yes
>>=Bitwise shift right and assignflags >>= 2;yes
17throwthrow exceptionthrow EClass(“Message”);no
18,Sequential evaluation operatorfor (i = 0, j = 0; i < 10; i++, j++) ...yesleft to right

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答