C++学习笔记1

1.  这意味着运营商——什么类型的运营商的运行操作和经营成果——取决于操作数的类型。

2.  通过形成一个数组指针值

int ia[10];

ia[0] = 0;              // ok: subscript is an lvalue

*ia = 0;                 // ok: dereference also is an lvalue

int ival; int *pval;

 ival = pval = 0     // error: cannot assign the value of a pointer to an int

 string s1, s2;

 s1 = s2 ="OK";   // ok: "OK"converted to string

3. 位运算优先级

移位操作符具有中等优先级:其优先级比算术操作符低。但比关系操作符、赋值操作符和条件操作符优先级高。若IO 表达式的操作数包括了比IO 操作符优先级低的操作符。相关的优先级别将影响书写该表达式的方式。通常需使用圆括号强制先实现右结合:

 cout << 42 +10;             // ok, + has higherprecedence, so the sum is printed

 cout << (10< 42);           // ok: parenthesesforce intended grouping; prints 1

 cout << 10 <42;              // error: attempt tocompare cout to 42!

The second cout is interpreted as第二个cout 语句解释为:

 (cout << 10)< 42;

该表达式说“将10 写到cout。然后用此操作(也就是cout)的结果与42 做比較”

4. 位运算

老师能够设置和检查每一个位(30)。比如,如果第27 位所表示的学生及格了,则能够使用以下的语句适当地设置相应的位:

 bitset<30>bitset_quiz1;              // bitset solution

 unsigned longint_quiz1 = 0;       // simulated collection of bits

 bitset_quiz1.set(27);                   // indicate student number 27 passed

 int_quiz1 |=1UL<<27;                  //indicate student  number 27 passed

// 1UL <<27; // generate a value with only bit number 27 set

// following assignment is equivalent to int_quiz1 |= 1UL<< 27;

 int_quiz1 = int_quiz1| 1UL << 27;

bitset_quiz1.reset(27);                  //student number 27 failed

int_quiz1 &= ~(1UL<<27);            // student number 27 failed

5. 赋值操作符

 int i, j, ival;

 const int ci = i;                     // ok: initialization not assignment

 1024 = ival;                          // error: literals are rvalues

 i + j = ival;                            // error: arithmeticexpressions are rvalues

 ci = ival;                               // error: can't write to ci

6. 在单个表达式中组合使用解引用和自增操作

以下的程序使用了一种很通用的C++ 编程模式输出ivec 的内容:

 vector<int>::iterator iter =ivec.begin();

 // prints 10 9 8 ...1

 while (iter !=ivec.end())

 cout << *iter++<< endl; // iterator postfix increment

假设程序猿对C++ 和C 语言都不太熟悉。则经常会弄不清楚表达式*iter++ 的含义。

因为后自增操作的优先级高于解引用操作,因此*iter++ 等效于 *(iter++)。子表达式iter++ 使iter 加1,然后返回iter 原值的副本作为该表达式的结果。因此,解引用操作* 的操作数是iter 未加1 前的副本。 这样的使用方法的依据在于后自增操作返回其操作数原值(没有加1)的副本。

假设返回的是加1 后的值,则解引用该值将导致错误的结果:ivec的第一个元素没有输出。并企图对一个多余的元素进行解引用。

7. 如果iter 为vector<string>::iterator 类型的变量,指出以下哪些表达式是合法的,并解释这些合法表达式的行为。

vector<int> ivec(10, 1);

vector<int>::iterator iter = ivec.begin();

*iter++;

(*iter)++;

iter.empty();                     // X

iter->empty();                   // X

++*iter;

iter++->empty();               // X

8. 能够使用一组嵌套的条件操作符求出三个变量的最大值,并将最大值赋给 max:

 int max = i > j

 ? i > k ?

i : k

 : j > k ?

j : k;

9. 逗号操作符

逗号表达式是一组由逗号分隔的表达式,这些表达式从左向右计算。逗号表达式的结果是其最右边表达式的值。假设最右边的操作数是左值。则逗号表达式的值也是左值。此类表达式通经常使用于for 循环:

10. 动态创建对象的默认初始化

假设不提供显式初始化。动态创建的对象与在函数内定义的变量初始化方式同样。对于类类型的对象,用该类的默认构造函数初始化。而内置类型的对象则无初始化。

 string *ps = newstring;                 // initialized to empty string

 int *pi = new int;                            // pi points to an uninitialized int

相同也可对动态创建的对象做值初始化(value-initialize)

 string *ps = newstring();                // initialized to empty string

 int *pi = new int();                           // pi points to an int value-initialized to 0

 cls *pc = new cls();                         // pc points to avalue-initialized object of type cls

版权声明:本文博主原创文章。博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/blfshiye/p/4814705.html