码迷,mamicode.com
首页 > 编程语言 > 详细

C++学习笔记1

时间:2014-07-02 08:10:26      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:c++ 学习笔记

 

1.  操作符的含义——该操作符执行什么操作以及操作结果的类型——取决于操作数的类型。

 

2.  数组通过指针赋值

int ia[10];

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

*ia = 0;                 // ok: dereference also is anlvalue

 

int ival; int *pval;

 ival = pval = 0;   // error: cannot assign the value of a pointerto

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;              // bitsetsolution

 unsigned longint_quiz1 = 0;       // simulatedcollection of bits

 bitset_quiz1.set(27);                    // indicate studentnumber 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 notassignment

 1024 = ival;                         // error: literals arervalues

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

 ci = ival;                               // error: can‘twrite 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 toempty string

 int *pi = new int;                             // pi points to anuninitialized int

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

 string *ps = newstring();             // initialized toempty string

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

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

C++学习笔记1,布布扣,bubuko.com

C++学习笔记1

标签:c++ 学习笔记

原文地址:http://blog.csdn.net/wzhiu/article/details/36196871

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!