美文网首页
C++&Golang Coding

C++&Golang Coding

作者: 燕京博士 | 来源:发表于2018-05-14 23:39 被阅读28次
image.png
image.png
image.png
image.png

https://blog.csdn.net/u011185231/article/details/51591571
1、在C++,不同类型的指针是不能直接赋值的,必须强转

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_1" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_51" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_51" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=51&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. void *p;
  2. int *i = (int *)p;

2、class是C++的核心,也是面向对象的核心基础

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_2" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_52" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_52" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=52&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. class Person
  2. {
  3. public:
  4. string name;
  5. private:
  6. int age;
  7. public:
  8. int sex;
  9. };

3、引入了命名空间防止大型项目中代码冲突

4、new关键字的使用

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_3" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_53" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_53" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=53&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int *p = new int;

  2. *p = 100;

  3. //分配内存的时候可以同时赋值,前两句代码相当于 int * p = new int (100);

  4. cout << *p << endl;//输出100

  5. delete p;//用完记得删除, 用new分配的内存必须用delete释放,不能用free

  6. int *p = new int[10];//用new创建数组

  7. //删除的时候不能这样写 :delete p; 这样写是删除了数组中第一个地址,会造成内存泄漏;

  8. delete []p;

5、inline 内联关键字的作用:内联函数不用为函数调用,而是直接把函数的代码嵌入到调用的语句中。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_4" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_4" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_54" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_54" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=54&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main(){

  2. for ( int i = 0 ; i < 100; i++)

  3. {

  4. int a = max (i , 50 );

  5. }

  6. return 0 ;

  7. }

  8. inline int max (int a, int b ){

  9. if ( a > b)

  10. return a ;

  11. else

  12. return b ;

  13. }

  14. //inline函数适合用在代码少,且大量的调用的函数。

6、引用:一个变量的别名,不是一个变量的地址

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_5" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_5" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_55" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_55" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=55&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main(){

  2. int a = 1 ; int b = 2 ;

  3. int & c = a; //正确,c就是a的别名

  4. c = b; //错误,不能修改

  5. int & c;

  6. c = a; //错误,c不能单独命名

  7. swap ( a , b );

  8. cout << "a = " << a << endl ; // a = 2;

  9. cout << "b = " << b << endl ; // b = 1;

  10. return 0 ;

  11. }

  12. void swap( int & a , int & b ){

  13. int temp = a ;

  14. a = b;

  15. b = temp;

  16. }

7、函数的默认实参

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_6" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_6" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=6&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_56" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_56" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=56&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. void Func( int a = 100 ){
  2. cout << "a = " << a << endl ;
  3. }
  4. int main(){
  5. Func (); // 打印 a = 100;
  6. return 0 ;
  7. }

8、函数重载:函数名字相同,只是参数不同

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_7" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_7" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=7&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_57" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_57" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=57&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. void Func(){}
  2. void Func( int a ){}
  3. void Func( string str ){}

9、模板函数

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_8" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_8" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=8&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_58" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_58" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=58&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. template <class T>
  2. T Add( T a , T b)
  3. {
  4. return a + b ;
  5. }
  6. int main()
  7. {
  8. int a = 1;
  9. int b = 2;
  10. Add( a , b );
  11. return 0;
  12. }

10、private,public,protected

在类里面如果没有权限限定符,默认是private

在结构体里面没有权限限定符,默认是public

11、一个unsigned int类型与int类型相加的结果是一个unsigned int类型。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_9" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_9" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=9&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_59" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_59" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=59&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. int unsigned a = 1 ;
  4. int b = - 2;
  5. std ::cout << a + b << std :: endl;//输出的类型是一个unsigned int类型,输出结果为:4294967295
  6. }

12、两个字符串位置相邻,实际上是一个整体。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_10" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_10" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=10&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_60" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_60" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=60&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. std :: cout << "asdfasdf" "asdfasdf" "ASdfasd" << std:: endl ;
  2. //输出的是一个整体

13、定义在函数体内部的内置类型变量不会被初始化,定义在函数体外部的内置类型变量会自动初始化。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_11" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_11" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=11&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_61" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_61" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=61&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. int a ;
  4. std ::cout << a << std:: endl ;
  5. return 0 ;
  6. }
  7. //程序会报错,提示局部变量a未被初始化。

14、extern关键字

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_12" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_12" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=12&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_62" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_62" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=62&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. extern int i;//声明i
  2. int j;//声明并定义j
  3. extern int k = 1;//定义

15、引用就是别名,引用不是对象,它只是一个已存在的对象的别名,一旦绑定一个对象之后就无法再绑定其他对象。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_13" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_13" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=13&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_63" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_63" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=63&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. int b = 0;
  4. int &a = b ;//a指向b
  5. int &c;//报错,引用必须初始化
  6. int &d = 1;//报错,引用只能绑定到对象上。
  7. std ::cout << &b << std :: endl;
  8. std ::cout << &a << std :: endl;//a和b都指向同一个地址
  9. return 0 ;
  10. }

16、void*是一种特殊的指针类型,可以存储任意对象的地址。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_14" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_14" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=14&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_64" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_64" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=64&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. int val = 0 ;
  4. void *a = &val ;//合法
  5. long *b = &val;//非法
  6. }

17、const对象在默认情况下只在本文件内有效。若要共享const对象,就必须在前面加上extern关键字

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_15" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_15" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=15&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_65" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_65" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=65&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. extern const int val = 0;//定义了并初始化了一个常量,这个常量可以在其他文件中使用。

  2. extern const int val;//声明了一个val,说明val并不是本文件独有的,它的定义在别处。

18.const指针

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_16" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_16" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=16&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_66" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_66" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=66&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()

  2. {

  3. int val = 1 ;

  4. int num = 2 ;

  5. int * const p = & val; //p是一个const指针,p将一直指向val,可以修改所指对象的值(也就是val的值)

  6. p = &num ; //错误,p是一个const指针

  7. const int num1 = 1 ;

  8. const int *const p1 = & num1 ;//p1是一个指向常量对象的常量指针,既不能指向其他地址,也不能修改所指对象的值(也就是num1的值),

  9. }

  10. //如果表达式太复杂,可以通过从右往左阅读来弄清楚。拿上面例子来讲:p旁边的符号是const,说明p是一个常量对象,再往左的符号是*,表示p是一个常量指针,再往左是int,则说明p是一个指向int类型对象的常量指针。

  11. //通过上面方法可以得知p1是一个指向int类型常量的常量指针。

19、关于指向常量的指针和引用。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_17" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_17" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=17&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_67" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_67" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=67&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. int val = 1 ;
  4. const int *p1 = & val; //正确,可以指向一个非常量对象
  5. const int &a = val ; //正确,可以绑定一个非常量对象
  6. }
  7. //1、指向常量的指针可以指向非常量,但不可以通过该指针来修改这个非常量的值。
  8. //2、常量引用也可以绑定非常量对象,但是不可以通过该引用来对绑定的这个对象作出修改。

20、字符串的几种初始化方法,一般使用常用方法即可。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_18" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_18" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=18&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_68" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_68" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=68&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. string s = "aaaa" ; //常用
  4. string s1 ( 10, 'a') ;//输出10个a
  5. string s2 = { "aaaa" };
  6. string s3 = ( "aaaa" );
  7. string s4 ( "aaaa" );
  8. string s5 = ( "aaaa" );
  9. string s6 = string( "aaaa" ) ;
  10. }
  11. //若使用=来初始化则表示<strong>拷贝初始化</strong>,不使用=初始化表示<strong>直接初始化</strong>

21、getline会读取一整行,如果希望在得到的字符串中保留空格,就用getline替换>>运算符。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_19" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_19" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=19&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_69" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_69" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=69&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. string s ;
  4. while ( getline (cin , s))//读入一整行直到遇到文件末尾
  5. {
  6. cout << s << endl ;
  7. }
  8. return 0 ;
  9. }

22、字面值和string对象相加时,必须确保加号两边至少有一个是string对象

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_20" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_20" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=20&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_70" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_70" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=70&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. string s1 = "1" ;
  4. string s2 = "1" + "2"; //错误,加号两边都是不是string对象
  5. string s3 = s1 + "1"; //正确,加号左边是一个string对象
  6. string s4 = "1" + "2" + s1 ; //错误,第一个加号两边都不是string对象
  7. string s5 = s1 + s2; //正确,加号两边都是string对象
  8. return 0 ;
  9. }

23、标准库类型vector表示对象的集合,是一个类模板

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_21" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_21" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=21&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_71" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_71" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=71&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. vector <int > a ;
  4. vector <vector < int>> b;
  5. return 0 ;
  6. }
image

vector对象能够高效的增长,所以在定义的时候设定大小是没有必要的, 甚至性能更差。但是如果所有初始元素都是一样的值的话是可以在定义的时候初始化的。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_22" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_22" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=22&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_72" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_72" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=72&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. vector <int > vInt = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
  4. for ( auto &i : vInt )
  5. {
  6. i *= i;
  7. }
  8. return 0 ;
  9. }
  10. //与string类型一样,若要改变元素的值,需要在循环的时候对循环变量定义为引用类型

24、迭代器

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_23" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_23" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=23&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_73" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_73" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=73&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()

  2. {

  3. string s = "asdfasdfasfd" ;

  4. auto a = s .begin (); //a表示第一个元素

  5. auto b = s .end (); //b表示尾元素的下一个元素

  6. cout << *a << endl;//打印结果是输出'a'

  7. cout << *b << endl;//错误,因为b是指向尾元素的下一个元素,没有实际指向一个元素,所以不能对他进行解引用或者递增操作

  8. vector <int > vInt ( 10, 1 );

  9. auto c = vInt .begin ();

  10. auto d = vInt .end ();

  11. return 0 ;

  12. }

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_24" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_24" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=24&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_74" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_74" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=74&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. 容器运算符
  2. *iter 返回该迭代器所指元素的引用
  3. iter->men 返回该迭代器所指元素中名为men的成员,可写成(*iter).men;
  4. ++iter 指向该迭代器的下一个元素
  5. --iter 指向该迭代器的上一个元素
  6. iter1 == iter2 判断两个迭代器是否相等,如果两个迭代器指向的是同一个元素或者两个迭代器都指向同一个容器的尾后迭代器,说明相等;反之不相等。
  7. iter1 != iter2

对于迭代器和vector对象的使用上的注意事项

1、vector对象可以动态的增长,所以这也限制了使用for(rangefor)来循环对vector中push_back元素。

2、另一个就是:如果定义了一个vector的迭代器,之后再向vector对象中push_back元素的话,原迭代器会失效。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_25" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_25" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=25&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_75" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_75" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=75&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()

  2. {

  3. vector <string > vStr;

  4. auto a = vStr.begin ();//初始化之后立即定义一个迭代器

  5. vStr.push_back ( "11" );

  6. vStr.push_back ( "22" );

  7. auto b = vStr.begin ();//push_back元素之后重新等一个迭代器

  8. cout << *a << endl;//报错,因为vStr为空,a是指向尾迭代器

  9. cout << *b << endl;//成功,b是指向第一个元素

  10. return 0 ;

  11. }

25、字符数组有一种额外的初始化方式。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_26" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_26" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=26&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_76" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_76" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=76&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. char a[] = { 'c' , '+' , '+' , '\n' };
  4. char b[] = "c++";
  5. char c[4] = "c++";
  6. //这3种初始化方式效果是一样的
  7. return 0 ;
  8. }

26、指针也可以实现迭代器的功能,但必须得指定头指针和尾后指针,但不安全,c++11提供了新方法:begin()和end()

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_27" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_27" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=27&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_77" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_77" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=77&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. const int size = 10 ;
  4. int arr[size] = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
  5. int *pHead = arr;
  6. int *pEnd = &arr[size];//arr[size]这个元素并不存在,所以递增操作或者也不能解引用来取值,这里唯一的用处就是提供其地址用于初始化pEnd。
  7. for ( int *p = pHead ; p != pEnd ; ++p)
  8. cout << *p << " " ;
  9. cout << endl;
  10. return 0 ;
  11. }

27、c风格的字符串操作

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_28" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_28" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=28&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_78" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_78" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=78&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()

  2. {

  3. char a [] = { '1' , '2' };

  4. cout << strlen( a ) << endl ;//错误,没有以空字符结束

  5. char b[] = "11111 22222";

  6. char d[] = "22222 11111";

  7. strcmp(b, d);//如果b>d返回真值,如果b<d返回负值

  8. char s[100] = {}; //初始化一个用于存放结果的数组

  9. strcpy(s, d);//把d复制给s

  10. strcat(s, d);//把d加在s的后面

  11. char *p = s;

  12. while ( *p )

  13. {

  14. cout << *p << endl ;//输出操作结果

  15. p++;

  16. }

  17. return 0 ;

  18. }

  19. //对于大多数应用程序来说使用标准库string比c风格字符串更安全高效

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_29" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_29" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=29&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_79" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_79" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=79&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. //string对象转换为c风格字符串

  2. int main()

  3. {

  4. string s = "11111111" ;

  5. const char *cc = s .c_str();

  6. return 0 ;

  7. }

  8. //使用数组初始化vector对象

  9. int main()

  10. {

  11. int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };

  12. vector <int> vInt1 ( begin( arr ), end (arr) );//把数组的值全部用于初始化vector对象

  13. //也可以用数组的一部分值来初始化

  14. vector <int> vInt2 ( arr + 1, arr + 3 );

  15. vector <int> vInt3 ( begin(arr) + 1, end(arr) - 1);

  16. return 0 ;

  17. }

28、用指针来遍历多维数组。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_30" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_30" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=30&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_80" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_80" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=80&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. int arr[3][4] = {
  4. 1, 2, 3, 4,
  5. 5, 4, 3, 2,
  6. 1, 13, 3, 4
  7. };
  8. for ( auto p = arr ; p != arr + 3 ; ++p )//先循环一维得到一维的地址
  9. for ( auto d = *p; d != *p + 4; ++d )//根据一维的地址得到二维地址,并输出值
  10. cout << *d << " " ;
  11. return 0 ;
  12. }
  13. //也可以使用标准函数begin()和end()来实现相同功能,更简洁安全
  14. int main()
  15. {
  16. int arr[3][4] = {
  17. 1 , 2 , 3 , 4 ,
  18. 5 , 4 , 3 , 2 ,
  19. 1 , 13 , 3 , 4
  20. };
  21. for ( auto p = begin (arr ); p != end (arr ); ++ p )
  22. for ( auto q = begin (p ); q != end (p );++ q)
  23. cout << *q << " " ;
  24. return 0 ;
  25. }

29、关于递增递减运算符的选用

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_31" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_31" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=31&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_81" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_81" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=81&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int i = 0; j = 0;

  2. j = ++i;//前置版本,i = 1; j = 1 //改变i的值并返回给j

  3. j = i++;//后置版本,i = 2; j = 1 //存储i的值并计算返回j

  4. //尽量选用前置版本的递增递减运算符:

  5. //1、后置版本的需要将原始值储存下来以便返回这个未修改的内容,如果我们不需要这个未修改的内容,那么这个后置版本的操作就是一种浪费。

  6. //2、基于上面的原因,对于整数和指针类型来说,编译器可能会对这种工作进行一定的优化,但对于相对复杂的迭代器类型来说,这种额外的工作是非常耗性能的。

30、sizeof运算符返回一条表达式或者一个类型名字所占的字节数,它不会求实际类型的值

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_32" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_32" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=32&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_82" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_82" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=82&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. sizeof(type)

  2. sizeof expr//返回表达式结果类型的大小

  3. int main()

  4. {

  5. string s , * p ;

  6. sizeof( string );//返回string类型的对象的大小

  7. sizeof s;//s类型的大小,与上面一样

  8. sizeof p;//指针所占空间大小

  9. sizeof *p;//因为sizeof不会求实际类型的值,所以即使p没有初始化也没有影响

  10. return 0 ;

  11. }

  12. //对string和vector对象执行sizeof运算只返回该类型固定部分的大小,不会计算对象中的元素占用了多少空间

31、算术转换的规则是运算符的运算对象转换成最宽的类型,如:

** 1、**一个运算类型是long double,那么不论另一个运算类型是什么都会转换为long double。

 **2、**当表达式中有浮点型也有整型时,整数类型将转换为浮点型。

32、显示转换

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_33" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_33" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=33&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_83" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_83" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=83&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. 1、旧版的强制类型转换

  2. type (expr)//函数形式的

  3. type expr//c语言风格的

  4. int main()

  5. {

  6. int i = 11 , j = 2;

  7. double res1 = double (i ) / j;

  8. return 0 ;

  9. }

  10. 2、新版的强制类型转换

  11. cast-name<type>(expr)

  12. cast-name分别有:static_cast、dynamic_cast、const_cast和reinterpret_cast

  13. static_cast:任何具有明确定义的类型转换,只要不包含底层const,都可以使用它。

  14. dynamic_cast:支持运算时类型识别。

  15. const_cast:只能改变运算对象的底层const。

  16. reinterpret_cast:通常为运算对象的位模式提供较低层次上的重新解释。

  17. int main()

  18. {

  19. int i = 11 , j = 2;

  20. //使用static_cast

  21. double res = static_cast <double> (i) / j ;

  22. void *d = & i;

  23. int p = static_cast <int> (d);

  24. //使用const_cast

  25. const char *pc ;

  26. char cp = const_cast <char> (pc);

  27. //使用reinterpret_cast

  28. int *pi;

  29. char ic = reinterpret_cast <char> (pi);

  30. string str = ic;//错误,ic实际存放的是指向int类型的指针

  31. return 0;

  32. }

33、局部静态对象:如果我们想让一个局部变量的生命周期贯穿函数以及之后的时间。我们可以把变量定义为static类型,局部静态对象在程序执行第一次经过对象时被初始化,直到程序运行终止时才销毁。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_34" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_34" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=34&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_84" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_84" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=84&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int count()

  2. {

  3. static int res = 0 ;

  4. return ++res ;

  5. }

  6. int main()

  7. {

  8. for ( int i = 0 ; i < 10; ++i )

  9. cout << count() << endl;

  10. return 0 ;

  11. }

34、建议变量和函数的声明放在头文件中,在源文件中定义。

35、在拷贝大的类类型对象或者容器对象时是比较低效的。而且有些类类型对象还不支持拷贝。所以使用引用是非常明智的选择。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_35" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_35" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=35&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_85" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_85" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=85&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. //使用引用来比较

  2. bool isShorter( string &s1, string &s2 )

  3. {

  4. return s1.size() < s2.size();

  5. }

  6. //使用拷贝来比较

  7. bool isShorter( string s1, string s2 )

  8. {

  9. return s1.size() < s2.size ();

  10. }

  11. //使用常量引用来比较,如果函数不用修改形参的值,最好使用常量引用

  12. bool isShorter( const string &s1, const string &s2 )

  13. {

  14. return s1.size() < s2.size();

  15. }

  16. int main()

  17. {

  18. string s1 = "111111" ;

  19. string s2 = "11111111" ;

  20. cout << isShorter( s1, s2 ) << endl;

  21. return 0;

  22. }

36、函数的数组形参,数组有两个特性:(1)不允许拷贝数组。(2)使用数组时会转为指针

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_36" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_36" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=36&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_86" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_86" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=86&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. void set(const *int);

  2. void set(const int[]);

  3. void set(const int[20]);//这里的20表示期望含有多少元素,实际上不一定。

  4. //上面3中表达方式是等价的,都是const *int类型的形参

  5. int i = 0;

  6. int j[2] = {1, 2};

  7. set( &i );//正确

  8. set( j );//正确

37、函数中管理指针的3中方法

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_37" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_37" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=37&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_87" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_87" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=87&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. 第一种方法:必须要求数组本身包含结束标记

  2. void set( const char *p )

  3. {

  4. if ( p )

  5. while (* p )

  6. {

  7. cout << *p ++ << endl;

  8. }

  9. }

  10. void set(const int *p)

  11. {

  12. if(p)

  13. {

  14. while(*p)

  15. cout << *p << endl;

  16. }

  17. }

  18. //这用方法用于有结束标记的情况如char类型。如果用于int类型就会无效。

  19. 第二种方法:是传递一个数组的首元素和尾后元素的指针

  20. void print( const int *begin , const int *end)

  21. {

  22. while ( begin != end)

  23. {

  24. cout << *begin ++ << endl;

  25. }

  26. }

  27. int main()

  28. {

  29. int i[] = { 1, 2 , 3 };

  30. print (begin (i), end(i));

  31. return 0 ;

  32. }

  33. 第三种方法:是在函数的形参中添加一个表示数据大小的形参,在以前c和c++的程序中经常使用这种方法

  34. void print( const int *p , const int size )

  35. {

  36. for ( int i = 0 ; i < size; ++i )

  37. {

  38. cout << *(p + i ) << endl ;

  39. }

  40. }

  41. int main()

  42. {

  43. int i [] = { 1 , 2 , 3 };

  44. print (i , end ( i) - begin (i ));

  45. return 0 ;

  46. }

38、多维数组就是数组的数组

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_38" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_38" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=38&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_88" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_88" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=88&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. //p指向数组的首元素,该元素是包含10个整数的数组
  2. void set(int (*p)[10], int rowSize){}
  3. //等价于
  4. void set(int p[][10], int rowSize){}

39、返回引用的函数

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_39" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_39" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=39&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_89" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_89" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=89&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. char & get_value (string &s , int index)

  2. {

  3. return s [ index] ;

  4. }

  5. int main()

  6. {

  7. string s = "11111" ;

  8. get_value (s , 0 ) = '2';

  9. cout << s << endl ;//输出21111

  10. return 0 ;

  11. }

40、返回数组指针的函数

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_40" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_40" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=40&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_90" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_90" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=90&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. func(int i)//表示调用func时需要一个int型实参
  2. (*func(int i))//表示可以对该函数的结果解引用
  3. (*func(int i))[10]//表示解引用该函数将得到一个大小为10的数组
  4. int (*func(int i))[10]//表示数组中的元素是int类型

41、函数指针:函数指针指向的是函数,函数类型由它的返回值类型和形参类型共同决定。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_41" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_41" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=41&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_91" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_91" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=91&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. 如:bool compare( const string & , const string &);

  2. 则该函数的类型是bool( const string &, const string &),

  3. 则指向该函数的指针是:bool (*p)( const string &, const string &)

  4. //函数的重载指针

  5. void set( int *);

  6. void set(char *);

  7. void (*p)( int * ) = set; //p默认指向了 set(int *)

  8. //声明语句直接使用函数指针显得冗长,可以使用typedef来简化。

  9. //下面四个是等价声明

  10. typedef void func1(int *); //func是函数类型

  11. typedef void (*funcp1)(int *);//funcp是函数指针

  12. using func2 = void( int *);

  13. using funcp2 = void(*)(int *);

  14. 使用的时候直接:

  15. void use(int a, int b, func);

  16. void use(int a, int b, funcp);

  17. //用容器把函数存储起来

  18. int add( const int & a , const int & b )

  19. {

  20. return a + b ;

  21. }

  22. int reduce( const int & a , const int &b )

  23. {

  24. return a - b ;

  25. }

  26. int multiply( const int &a , const int & b)

  27. {

  28. return a * b ;

  29. }

  30. int divide( const int &a , const int & b)

  31. {

  32. return a / b ;

  33. }

  34. using p = int (*)( const int &, const int &);

  35. vector < p> b{ add, reduce, multiply, divide } ;

  36. int main()

  37. {

  38. for ( auto a : b )

  39. {

  40. cout << a( 1, 2) << endl ;

  41. }

  42. return 0 ;

  43. }

42、如果一个类允许其他类或者函数访问它的非公有成员,可以让这个其他类或者函数成为他们的友元。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_42" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_42" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=42&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_92" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_92" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=92&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. class Person
  2. {
  3. friend istream & read ( istream& is , Person& person );//友元的声明仅仅指定了访问权限
  4. private :
  5. string name;
  6. string address;
  7. };
  8. inline istream& read( istream& is, Person& person)
  9. {
  10. is >> person.name >> person.address;//虽然name和address是私有变量,但是成为友元之后可以访问。
  11. return is;
  12. }

43、如果我们希望能修改类的某个数据成员,即使在一个const成员函数中,我们可以通过在数据成员的声明前加上mutable关键字

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_43" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_43" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=43&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_93" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_93" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=93&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. class Person

  2. {

  3. public :

  4. void Total () const;

  5. private :

  6. mutable int sum;

  7. };

  8. void Person:: Total () const

  9. {

  10. ++sum;

  11. }

44、要想让某个成员函数作为友元,必须按顺序来设计程序。以满足声明和定义的依赖关系。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_44" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_44" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=44&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_94" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_94" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=94&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. 1.首先定义Manager类,其中声明clear函数,但不定义它。在clear使用Screen之前必须先声明Screen
  2. 2.然后定义Screen,包括对clear友元声明
  3. 3.最后定义clear,这样才能正常使用Screen的成员。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_45" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_45" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=45&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_95" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_95" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=95&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. </pre><pre code_snippet_id="1708766" snippet_file_name="blog_20160605_46_4473679" name="code" class="cpp"><pre name="code" class="cpp">class Screen;

  2. class Manager

  3. {

  4. public :

  5. using ScreenIndex = vector < string>:: size_type ;

  6. inline void clear( ScreenIndex );

  7. private :

  8. vector <Screen > screens ;

  9. };

  10. class Screen

  11. {

  12. friend void Manager:: clear (ScreenIndex );

  13. //friend class Manager;

  14. private :

  15. pos width = 0 ;

  16. pos height = 0 ;

  17. pos cursor = 0 ;

  18. string contents ;

  19. };

  20. inline void Manager::clear ( ScreenIndex index )

  21. {

  22. if ( index > screens.size()) return;

  23. Screen & sc = screens[index];

  24. sc.contents = string( sc.width * sc.height, ' ' );

  25. }

45、当在类的外部定义成员函数时,必须提供类名和函数名

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_46" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_46" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=46&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_96" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_96" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=96&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. void Manager::clear ( ScreenIndex index )//因为提供了类名,所以我们可以直接使用类中其他的成员:ScreenIndex、screens等

  2. {

  3. if ( index > screens.size()) return;

  4. Screen & sc = screens[index];

  5. sc.contents = string( sc.width * sc.height, ' ' );

  6. }

  7. //但如果是在函数返回值要使用类中的成员,就必须指明这个返回值是属于哪个类的

  8. Screen::pos Screen::size() cosnst

  9. {

  10. return height * width;

  11. }

46、如果类的成员是const、引用、或者属于某种没有提供默认构造函数的类类型,我们必须通过构造函数初始值列表为这些成员提供初值。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_47" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_47" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=47&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_97" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_97" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=97&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. class Test

  2. {

  3. public :

  4. //这个版本的构造函数是对数据成员执行赋值操作

  5. Test (int a, int b , int c )

  6. {

  7. val1 = a; //正确

  8. val2 = b; //错误,不能给const赋值

  9. val3 = c; //错误,val3没有初始值

  10. }

  11. //下面这个版本是显式的初始化引用和const,是正确做法

  12. //Test(int a, int b, int c) :val1(a), val2(b), val3(c){}

  13. private:

  14. int val1;

  15. const int val2;

  16. int &val3;

  17. };

  18. //最好使用第二个版本

47、类成员的初始化顺序是由它们在类中定义的顺序决定的

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_48" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_48" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=48&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_98" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_98" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=98&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. class Test
  2. {
  3. int i;
  4. int j;
  5. public:
  6. Test(int val ) : j(val),i(j){} //这里i会先初始化,j后初始化,这样初始化的结果是用一个未定义的j来初始化i,这种做法是错误的,我们应该尽量避免这种情况。
  7. }

48、声明一个用默认构造函数初始化的对象常犯的错

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_49" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_49" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=49&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_99" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_99" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=99&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()
  2. {
  3. Person person1(); //声明并初始化一个函数,不是对象
  4. Person person2; //定义了一个对象,并使用默认构造函数初始化
  5. return 0 ;
  6. }

49、C++中的隐式类类型转换:如果构造函数只接受一个参数,那么它们就默认定义了这个类类型的隐式转换机制。

[cpp] view plaincopy

<embed id="ZeroClipboardMovie_50" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_50" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=50&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_100" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_100" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=100&width=16&height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

  1. int main()

  2. {

  3. Sales_Data item1 ;

  4. string newBook = "11111" ;

  5. item1.combine(newBook);//合法的,这里的combine实际上是一个接受Sales_Data类型的函数,但在这里却接受了一个string类型:它是先构建一个临时的Sales_Data类型的对象,然后把newBook作为构造函数的参数传给这个临时的Sales_Data对象,然后再把这个临时对象传给combine

  6. Sales_Data item2 = "111111" ;//正确,拷贝初始化

  7. item1.combine("11111");//错误的,是因为只允许一次类类型转换,而这里是先把字符串转换成string,然后再把这个临时的string转换成Sales_Data对象,执行了二次类类型转换,所以是错误的。

  8. return 0 ;

  9. }

  10. //如果我们想禁止隐式类类型转换,可以在构造函数的声明出使用explicit关键字

  11. explicit Sales_Data ( const string s ) : bookId (s ){}

  12. explicit Sales_Data ( istream& is );

相关文章

网友评论

      本文标题:C++&Golang Coding

      本文链接:https://www.haomeiwen.com/subject/ejmsdftx.html