《C++ primer》笔记 (updating)

第二章

1.  如果使用iostream,而不是iostream.h,则应使用下面的名称空间编译指令来使iostream中的定义对程序可用。

    using namespace std;

    名称空间支持是一项C++特性,旨让您编写大型程序以及将多个厂商现有的代码组合起来的程序时更容易。

    注意作用域。

2. cout  与其说程序显示了一条消息,不如说它将一个字符串插入到了输入流中。

    endl 是一个特殊的C++符号,表示一个重要的概念:重起一行。和“\n"相同

    cin 输入,其使用>>运算符从输入流中抽取字符。

    以上二三者均位于名称空间std 

3. 在C++中,分号标示了语句的结尾。因此,在C++中,回车的作用就和空格或制表符相同。

    eg:    using

            namespace 

                std;

4.函数中的语句都相对于花括号进行缩进。与函数名称相关的圆括号周围没有空白。

5. 暂停窗口:

        system("pause");

cin.get();

6. cout << "Now you have"

            << carrots

            << ”carrots."

            << endl;

7. main()的返回值并不是返回给程序的其他部分,而是返回给操作系统。

第三章

1.命名时,以两个下划线或下划线和大写字母打头的名称被保留给实现(编译器及其使用的资源)使用。以一个下划线开头的名称被保留给实现,用作全局标识符。

2. int owls = 101

    int wrens{432}

    int emus = {12}

3. int chest = 42 //decimal integer literal

    int waist = 0x42 //hexadecimal integer literal

    int inseam = 042 // octal integer literal

4. cout<<oct // manipulator for changing number base 8

    cout<<hex // manipulator for changing number base 16

5  22022L   被存储为 long

    22022LU  被储存为 unsigned long

6. cout.put() 函数,该函数显示一个字符

7. ASCII 为 Unicode 子集

8.wchar_t(宽字符类型),系统把每个字符存储在一个两个字节的内存单元中。cin和cout将输入和输出看作是char流,因此不适用处理wchar_t类型,iostream头文件的最新版本提供了作用相似的工具——wcin和wcout,可以处理wchar_t流。

另外可以通过加上前戳L来指示宽字符常量和宽字符串。

    wchar_t bob = L'P' //a wide character constant

    wcout << L"tall" << endl; //outputting a wide character string

7. 前戳u和U分别指出字符字面值的类型为char16_t和char32_t

8. bool is_ready = true

9. const 限定符。C++有一种更好的处理符号变量的方法,就是使用const关键字来修改变量声明和初始化。

    const int Months = 12 

    常见命名符号变量的方法就是将名称的首字母大写。 另一种约定是将整个名称大写,使用#define 创建常量时通常使用这种约定。

    const 相对#define 的优点: 能够明确指定类型,其次,可以使用C++的作用域规则将定义限制在特定的函数或文件中。

10. d.dddE+n指的是将小数点向右移n位,而d.dddE~n指的是将小数点向左移n位。之所以称为“浮点”,就是因为小数点可移动。

11. 注意float相对double, long double的精度限制。

12.

  类型      32位  64位

  char      1    1

  short int    2    2

  int       4    4

  long int     4    8

  long long int  8    8

  char*       4    8

  float       4    4

  double      8    8

第四章

1. 求数组元素数量

    short things[]={1,3,3,8}

    int num_element = sizeof things / sizeof(short)

2. char tlifs[4] {'h','i',112,'\0'} //allowed

3. 数组输出以‘\0’结尾,无论'\0‘元素后面有什么

4.  strlen()函数返回的是存储在数组中的字符串的长度,而不是数组本身的长度。另外,strlen()只计算可见的字符,而不把空字符计算在呢你。

5. getline()和get(),这两个函数都读取一行输入,直到到达换行符。然后getline()将丢弃换行符,而get()将换行符保留在输入序列中。

    cin.getline(name,20)

6. get不丢弃换行符,而是将其留在输入队列中。

    cin.get(name,Arsize)

    cin.get(dessert,Arsize)

    由于第一次调用后,换行符将留在输入队列中,因此第二次调用时看到的第一个字符便是换行符。因此get()认为已到达行位,而没有发现任何可读取的内容。get()将不能跨过该换行符。

    可以这样使用:

    cin.get(name,Arsize)

    cin.get()

    cin.get(dessert,Arsize)

    或

    cin.get(name,Arsize).get()

    cin.ger(dessert,Arsize).get()

    或

    cin.getline(name1,Arsize).getline(name2,Arsize)

getline()使用起来简单一些,但get()使得检查错误更简单些。

7. string 类 

    #include <string>

    string str1 

8. strcpy(charr1,charr2); //copy charr2 to charr1

    strcat(charr1,charr2); // append contents of charr2 to char1

9. strncat()和strncpy(),它们接受指出目标数组最大允许长度的第三个参数,因此更加安全。

10. 确定字符串中字符数的方法:

    int len1 = str1.size()

    int len2 = strlen(charr1)

11.cin.getline(charr,20) // 读入字符数组

    getline(cin,str) //读如字符串

    cin>>str

12 原始(Raw)字符串

    R“{Jim "King" Tutt uses "\n" instead of endl.}"

    显示:Jim "King" Tutt uses \n instead of endl.

    也可以 用\\n 显示 \n

    R"+*{"{Who wouldn't}",she whispered.}+*"

    显示:”{Who wouldn't}", she whispered.

13. struct infiatable

    {

        char name[20];

        float volume;

        double price;

    };

    定义:1. struct inflatable goose;

            2. inflatable vincent ;//struct 可省略

            3.inflatable pal =

            {

                "Audacious Arthur",

                3.12,

                32.99

            };

            4.inflatable duck = {"Daphne", 0.12 ,9.98};

            5.inflatable duck{"Daphe", 0.12,9.98};

            6.struct name

            {

                .....

            }var1,var2;

14.结构数组: inflatable gifts[100]

15.共用体(union)只能同时存储一种数据类型

    union one4all

    {

        int int_val;

        long long_val;

        double double_val;

    };

    可以使用one4all变量来存储int,long或double

    one4all pail;

    pail.int_val = 15; //store an int

    cout<<pail.int_val;

    pail.double_val = 1.38; //store a double ,int value is lost

    cout<<pail.double_val;

    由于共用体每次只能存储一个值,因此它必须又足够的空间来存储最大的成员,所以共用体的长度为其最大成员的长度。

16.struct widget

    {

        char brand[20];

        int type;

        union id   // format depends on widget type

        {

            long id_num;  //type 1 widgets

            char id_char[20]; //other widgets

          }id_val;

          };

          共用体常用于节省内存。

17. 枚举 

       enum spectrum{red, orange,yellow,green,blue,violet,indigo,ultraviolet};

       spectrum band;

        band = orange;

      设置枚举量的值

       enum bits{one = 1 ,two = 2 , four= 4, eight = 8};

       enum bigstep{first, second = 100 ,third};

        注意枚举的取值范围,P116

18.




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值