2021年4月4日,我终于开始学习C++啦,下面的笔记会记录着我的心酸的学习历程,每个标题会记录着下面代码在《C++中文版 Primer》中的页码

P6 实现两数之和

# include <iostream>int main()
{std::cout << "Enter two numbers:" << std::endl;int v1 = 0, v2 = 0;std::cin >> v1 >> v2;std::cout << "the sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl;return 0;}

P10 1到100的求和 while 循环

# include <iostream>int main()
{int sum = 0, val = 1;while (/* condition */val <= 100){/* code */sum += val;++val;}std::cout << "Sum of 1 to 100 inclusive is " << sum << std::endl;return 0;
}

P11 for循环实现1到100求和

# include <iostream>int main()
{int sum = 0;for (int val = 1; val <= 100; ++val){sum += val;}std::cout << "Sum of 1 to 100 is  " << sum << std::endl;return 0;}

P15 if语句统计一个数连续出现的次数

# include <iostream>int main()
{int currVal = 0, val = 0;if (std::cin >> currVal){int cnt = 1;while (std::cin >> val){/* code */if (val == currVal){++cnt;}else{std::cout << currVal << " occurs " << cnt << " times" << std::endl;currVal = val;cnt = 1;}}std::cout << currVal << " occurs " << cnt << " times" << std::endl;}return 0;
}

统计书籍的简单程序

# include <iostream>
# include "Sales_item.h"int main()
{Sales_item total;if (std::cin >> total){Sales_item trans;while (std::cin >> trans){if (total.isbn() == trans.isbn())total += trans;else{std::cout << total << std::endl;total = trans;}}std::cout << total << std::endl;}else{std::cerr << "No data?" << std::endl;return -1;}return 0;
}

P34 变量以及基本类型

unsigned和int是不能混用的,因为会转成unsigned类型,一旦小于0会自动转成unsigned的合法值

#include <iostream>
using namespace std;int main()
{unsigned u = 10;int i = 42;cout << i + i << endl; // 84cout << u - i << endl; // 4294967264return 0;
}

字符串单行内写不下的时候

#include <iostream>
using namespace std;int main()
{cout << "hello world"" hello c++" << endl; //hello world hello c++return 0;
}

P39 四种合法的变量初始化方法

#include <iostream>
using namespace std;int main()
{int a = 0;int b = {0};int c{0};int d(0);cout << a << b << c << d << endl;return 0;
}
#include <iostream>
using namespace std;int main()
{long double ld = 3.1415926536;int a{ld}, b = {ld}; // {}赋值如果检测到存在丢失风险,可能会报错的int c(ld), d = ld; // 不做检测,直接初始化return 0;
}

P45 引用的定义

int ival = 1024;
// 添加一个引用
int refVal = ival;

P48 指针的赋值与取值

#include <iostream>
using namespace std;int main() {int ival = 42;// 赋值int* p = &ival;// 取值cout << *p;return 0;
}

定义空指针

 int* p1 = 0;int* p2 = nullptr;

49 指针的指向与赋值

#include <iostream>
using namespace std;int main() {int ival = 1;// 空指针int* pi = 0;// pi 指向ivalpi = &ival;// ival赋值*pi = 2;cout << ival << endl;return 0;
}

51 同一行定义不同种类的数据

#include <iostream>
using namespace std;int main() {// 一条语句声明不同的变量int i = 1024, * p = &i, & r = i;return 0;
}

51 容易产生误导的定义

#include <iostream>
using namespace std;int main() {// p1是指针,p2是int数据类型int* p1, p2;return 0;
}

52 指向指针的指针

#include <iostream>
using namespace std;int main() {int ival = 1024;int* pi = &ival;int** ppi = &pi;cout << ival << "\n" << *pi << "\n" << **ppi;return 0;
}

53 定义常量

#include <iostream>
using namespace std;int main() {// 初始化一个常量,初始化后不能改变const int bufSize = 512;// 错误,不能改变常量的值bufSize = 256;return 0;
}

54 多文件中用同一个常量需要extern定义

#include <iostream>
using namespace std;int main() {extern const int bufSize = 1;return 0;
}

54对常量的引用

#include <iostream>
using namespace std;int main() {const int bufSize = 1;// 对常量的引用也必须加上constconst int& r1 = bufSize;return 0;
}

55 常量引用类型不用与被引用对象

#include <iostream>
using namespace std;int main() {double dval = 3.14;// 定义与被引用对象不同的常量引用const int& ri = dval;// 和上面的一句话实现相同效果const int temp = dval;const int& ri = temp;// 原来数改变了,因为赋值完就断开了,所以引用是不变的dval = 5.14;cout << ri << endl;//3return 0;
}

指向常量的指针

#include <iostream>
using namespace std;int main() {const double pi = 3.14;// 指向常量的指针const double* cptr = &pi;// 一个常量的指针可以指向一个非常量的对象double dval = 3.14;cptr = &dval;return 0;
}

55 const指针

#include <iostream>
using namespace std;int main() {int errNumb = 0;// 定义常量指针int* const curErr = &errNumb;// 常量指针指的是一直指向某个对象,可以通过指针改变对象的值*curErr = 1;cout << errNumb << endl;// 指向常量对象的常量指针const int * pip = &errNumb;// 以下赋值是非法的// *pip = 2;return 0;
}

58 顶层const

#include <iostream>
using namespace std;int main() {int i = 0;int* const p1 = &i; //p1值不可改变,是顶层constconst int ci = 42;  //ci值不可改变,是顶层constconst int* p2 = &ci; //允许改变p2的值,是底层constconst int* const p3 = p2; //左边是顶层const, 右边是底层constconst int& r = ci;i = ci; //正确p2 = p3;  //正确// int* p = p3; // 错误p2 = p3;  // 正确 p2 p3都是底层constp2 = &i;  //正常,int* 可以转化成const int*// int& r = ci;  // 错误 普通int& 不能绑定在int上面const int& r2 = i; //正确 const int& 可以绑定在普通int上return 0;}

constexpr常量表达式

#include <iostream>
using namespace std;int main() {// 常量表达式constexpr int mf = 20;constexpr int limit = mf + 1;return 0;
}
#include <iostream>
using namespace std;int j = 0;
constexpr int i = 42;int main() {// 常量表达式constexpr int* np = nullptr;// i, j必须要在函数体之外,因为这样才会分配固定的地址constexpr const int* p = &i;constexpr int* p1 = &j;return 0;}

60 类型的别名

#include <iostream>
using namespace std;int main() {// 类型别名, wages完全等价与doubletypedef double wages;wages num = 3.2;// p完全等价与double*typedef double* p;p pi = &num;cout << *pi << endl;// 另一种申明类型别名的方法using double_nickname = double;double_nickname num2 = 7.8;cout << num2 << endl;return 0;
}

类型别名变得很容易混淆

#include <iostream>
using namespace std;int main() {// pstring 等价于 char*typedef char* pstring;const pstring cstr = 0;// ps 是一个指针,他的对象是指向char常量的指针const pstring* ps;int num = 2;const int* pi = &num;const int** ps = &pi;cout << **ps << endl;return 0;
}

auto关键字,编译器自动推到变量的类型

#include <iostream>
using namespace std;int main() {// auto关键字,自动推断变量的类型auto i = 0, * p = &i;// 在声明符列表中,“auto”必须始终推导为同一类型//auto sz = 0, pi = 3.14;return 0;
}

auto导时候顶层const被忽略

#include <iostream>
using namespace std;int main() {int i = 0, & r = i;const int ci = i, & cr = ci;// 顶层const被忽略,b是一个整数auto b = ci;b = 7;// 强制推导出顶层constconst auto c = ci;return 0;
}

63 decltype用于推断变量的类型,顶层const会被保留下来

#include <iostream>
using namespace std;int main() {// decltype用于推断变量的类型,顶层const会被保留下来const int ci = 0, & cj = ci;decltype(ci) x = 0;decltype(cj) y = x;// 错误顶层const必须赋值//decltype(ci) z;return 0;
}

decltype注意点

#include <iostream>
using namespace std;int main() {int j = 45;int i = 42, * p = &i, & r = i;// 返回一个引用类型decltype (*p) c = j;cout << c << endl;// 返回一个int类型,可以不用初始化decltype(i) e;// 返回一个引用,必须初始化decltype((i)) d = j;return 0;
}

auto和decltype的特性以及各自的异同点

#include <iostream>
using namespace std;int main() {const int a = 3;int b = 4;//auto 部分// 底层const 顶层const都在const int* const p = &a;// 顶层const被干掉了auto pi = p;// 可以改变指向了pi = &b;// 底层const还在,所以不能通过指针改变b的值//*pi = 6;// auto前面加上constconst auto pi3 = p;// 加上const,连顶层const也保留下来了,所以既不能改变指向,又不能改变指向对象的值// pi3 = &b;// decltype部分decltype(p) pi2 = &a;// 顶层const和底层const都在所以既不能改变指向,又不能改变指向对象的值// pi2 = &b;// *pi2 = 6;return 0;
}

65 利用struct关键字自定义类

#include <iostream>
using namespace std;struct Sale_data {std::string bookNo;unsigned units_sold = 0;double revenue = 0.0;
};int main() {return 0;
}

C++ Primer 学习笔记 第一,二章相关推荐

  1. 『RNN 监督序列标注』笔记-第一/二章 监督序列标注

    『RNN 监督序列标注』笔记-第一/二章 监督序列标注 监督序列标注(Supervised Sequence Labeling)与传统的监督模式分类(supervised pattern classi ...

  2. 《C++标准程序库》学习笔记1--第二章第三章

    --------- 第二章 --------- 1.(P11) C++规定:除了以typename修饰外,template内的任何标志符号都被视为一个值(value)而非一个型别. eg. templ ...

  3. 预览文章: c++ primer学习笔记,二:标准库类型

    111111111111 转载于:https://www.cnblogs.com/kuangcheng/archive/2012/02/25/2368187.html

  4. C++ Primer 学习笔记 第十七章 标准库特殊设施

    标准库特殊设施 637 初始化tuple #include <iostream> #include <vector> #include <string> #incl ...

  5. 《C Primer Plus》学习笔记—第9章

    目录 <C Primer Plus>学习笔记 第9章 函数 1.复习函数 1.引入 2.创建并使用简单的函数:程序lethead1.c 3.分析程序 4.函数参数 1.程序lethead2 ...

  6. 《C Primer Plus》学习笔记—第12章

    目录 <C Primer Plus>学习笔记 第12章 存储类别.链接和内存管理 1.存储类别 1.作用域 2.链接 3.存储期 4.自动变量 1.程序hiding.c 2.没有花括号的块 ...

  7. 《C Primer Plus》学习笔记—第14章

    目录 <C Primer Plus>学习笔记 第14章 结构和其他数据形式 1.示例问题:创建图书目录 1.程序book.c 2.建立结构声明 3.定义结构变量 1.初始化结构 2.访问结 ...

  8. TensorFlow学习笔记(二):快速理解Tutorial第一个例子-MNIST机器学习入门 标签: 机器学习SoftmaxTensorFlow教程 2016-08-02 22:12 3729人阅

    TensorFlow学习笔记(二):快速理解Tutorial第一个例子-MNIST机器学习入门 标签: 机器学习SoftmaxTensorFlow教程 2016-08-02 22:12 3729人阅读 ...

  9. 《Go语言圣经》学习笔记 第一章 Go语言入门

    Go语言圣经学习笔记 第一章 Go语言入门 目录 Hello, World 命令行参数 查找重复的行 GIF动画 获取URL 并发获取多个URL Web服务 本章要点 注:学习<Go语言圣经&g ...

最新文章

  1. 递归删除目录下的所有文件
  2. Java 8中Stream API的这些奇技淫巧!你都Get到了吗?
  3. RocketMQ中的主从复制
  4. 数据表记录包含表索引和数值,请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照key值升序进行输出。...
  5. 清理AD过期对象,并将结果发送给指定管理员
  6. 二叉树两个结点的最低公共父结点 【微软面试100题 第七十五题】
  7. 使用 Boost.MPI 的 split() 操作对通信器的示例
  8. JavaParser入门:以编程方式分析Java代码
  9. 分析方法的基础 — 3. 业务与管理的特性,分析与设计的抓手
  10. k8s之scheduler
  11. Bailian2689 大小写字母互换【文本】(POJ NOI0107-14)
  12. 计算机无法启动打印服务,Win7无法启动print spooler服务报错1068怎么办?
  13. 社会化统计工具分享之Google分析、cnzz、51la、百度统计和Js比较
  14. Oracle学习3:dual详解
  15. nodejs 使用jsonwebtoken进行权限验证
  16. PermissionError: [Errno 13] Permission denied: ‘C:\\Users\\langxu\\AppData\\Local\\Temp
  17. 【jmeter性能测试】模拟多个IP同时登录
  18. 【Delphi】中使用消息Messages(五)Windows消息
  19. Beyond Compare 4密钥过期解决办法
  20. Phonics 自然拼读法 sm sn sl sw sp st sc sk all ew y 小结与回顾

热门文章

  1. 通过触摸屏幕/鼠标 图片跟随手指 加滑动效果
  2. Windows系统通过卷影副本备份系统遇到的几个错误
  3. html 绘制 拓扑图,如何绘制网页版拓扑图
  4. cookies和缓存的区别_Cookies和缓存之间的区别
  5. Android工具大全
  6. ESG全球领导者峰会|英特尔王锐:以科技之力应对全球气候挑战
  7. 如何在mysql中创建连接_如何在MySQL中创建新用户并开启远程连接访问?
  8. 欧几里得算法 详细证明
  9. 永磁同步电机(PMSM)控制中为什么要Id=0?还有其他的控制方法吗?
  10. 项目部署到阿里云服务器并保持后台运行