无标题
Expression An expression os composed of one or more operands adn yields a result when it is evaluated. Fundamentals unary operators and binary operators. overloaded operators. Lvalues and Rvalues: Lvalue: object identity (its location in memory); Rvalue: object’s value (its contents) delctype: 作用于左值产生引用类型(如解指针得到的是object本身),作用于右值则产生本身。 逻辑与:&&;逻辑或:|| 都是短路求值,先计算左侧表达式的值,再判断需不需要计算右侧。 赋值运算:右结合律,返回左侧运算对象。运算优先级较低。 i++:返回原始值的副本,本身自增。 ++i: 返回增加后的值。 12345auto pbeg = v.begin();while (pbeg != v....
Chapter 12: Dynamic Memory
Dynamic Memory C++编写原则之一:对象由谁创建,就由谁释放目前为止,所使用的所有对象都有着严格定义的生命周期。全局对象在程序启动时分配,在程序结束时销毁;局部自动对象,其生命周期在语句块内;局部static对象在第一次使用前分配,程序结束时销毁C++支持动态分配对象。静态内存保存局部static对象、类static数据成员以及定义在任何啊函数农户之外的变量;栈内存用来保存定义在函数内的非static对象。除此之外,每个程序还有一个内存池,被称为自由空间或堆(heap)。程序用对来存储动态分配的对象。必须显式销毁 Dynamic Memory and Smart Pointershared_ptr template make_shared(args): 创建指向T类型的指针,args进行初始化(建议使用) shared_ptrp(q): p是q的copy,递增q中的计数器 p.unique(): 如果p.usr_count() == 1,则返回true p.use_count(): 返回与p共享对象的智能指针数量 new + shared...
Chapter 13: Copy Control
通过定义5种成员函数控制一个类的copy(copy control). copy constructor copy-assignment operator move constructor move-assignment operator destructor Copy, Assignment and Destructor copy constructor 第一个参数是自身类型;所有额外参数都有默认值。 123456class Foo{public: Foo(); Foo(const Foo&); // copy constructor} 合成拷贝构造函数 complier会至少给class合成一个copy constructor,无论是定义还是编译器优化后的 123456789101112131415161718class Sales_data{public: Sales_data(const Sales_data&);private: std::string bookNo; int...
Chapter 7: Classes
Classes this: 使用实例化的Obj调用类方法时,隐式传入参数this,告诉类方法这个Obj的地址 const 修饰成员函数参数列表:常量成员函数,不能修改类成员,也不能调用非const成员函数,对类只读 类的外部定义成员函数:class::funtion。成员函数的声明必须在类中完成 123456Sales_data& Sales_data::combine(const Sales_data &rhs){ units_sold += rhs.units_sold; // this.units_sold += rhs.units_sold revenus += rhs.revenus; return *this; // 隐式传入this指针} 构造函数:C++编译器存在默认构造函数。可自定义构造函数 123456Sales_data(const std::string &s, unsigned n, double p):bookNo(s), units_sold(n), revenue(p*n) ...
Chapter 11: Associative Container
Associative Container 按照关键字来保存和访问。map 和 set。 map: 键值对 set: 集合,高效查询操作 标准库提供8个关联容器:1. set或map; 2. 要求不重复的关键字,或者允许重复; 3. 按顺序保存元素或无序保存 1[unordered_][multi][map|set] 使用关联容器multi_map 1234567891011121314151617181920212223242526272829303132333435363738394041424344#include <iostream>#include <map>#include <string>int main() { // 创建一个multimap,键为int类型,值为string类型 std::multimap<int, std::string> mm; // 插入元素 mm.insert({1, "apple"}); mm.inse...
InputAndOutput
Input Function: read-char read-line read Output Function: write-char write display newline write会向端口写一个S表达式(机器可读),display不需要write用双引号表示字符串,#\表示字符,display不用newline会在输出端口输出一个换行符 File IO Port Function: open-input-file call-with-input-file call-with-output-file 123456789(define o (open-output-file "greeting.txt"))(display "hello" o)(call-with-input-file "hello.txt (lambda (i) (let* ((a (read-char i)) (b (read-char i)) (c (read-char i))) (li...
Recursion
letrec 如果希望在一个结构中,支持不同procedure之间的互递归,即在定义之后,生成局部并行处理的block,使用letrec 1234567(letrec ((local-even? (lambda (n) (if (= n 0) #t (local-odd? (- n 1))))) (local-odd? (lambda (n) (if (= n 0) #f (local-even? (- n 1)))))) (list (local-even? 23) (local-odd? 23))) let使用letrec定义递归过程可以实现循环,显示10到1的降序列 1234567(letrec ((countdown (lambda (i) (if (= i 0) 'liftoff ...
LexicalVariable
以下代码中,由于语法闭包,set!修改的是局部变量而非全局变量 1234(define add2 (lambda (x) (set! x (+ x 2)) x)) 以下代码修改的是全局变量,因为指定给lambda传入一个空的参数列表 1234(define bump-counter (lambda () (set! counter (+ counter 1)) counter)) let & let*12345(let ((x 1) (y 2) (z 3)) (list x y z)); ==> (1 2 3) let可以显式的让参数进行语法闭包,设置为局部变量 123(let ((x 1) (y x)) (+ x y)) let针对变量的引用并不是从结构体中调用的,不作为结构的一部分,而是调用全局变量,以上的结构中,***(y x)是将全局变量中的x赋值给y* 如果想在引用时,优先考虑结构体内的变量,优先调用局部变量,使用let* 12345678(let* ((x 1) ...
CondtionalBlock
if123456789;Syntax;(if cond ;then-branch ;else-branch)(define p 80)(if (> p 70) 'safe 'unsafe); ==> safe when & unless 只需要基本语句then branch时,使用when和else 1234567891011(when (< a b) (display "a is") (display a) (display "b is") (display b)); ==(unless (>= a b) (display "a is") (display a) (display "b is") (display b)) cond cond结构在表示多重if时很方便 123(cond ((char<? c #\c) ...
CodeStructure
Procedure is Data Procedures在编译器接收到procesure的表头时,会判断是属于哪种过程,例如,在读取到cons后,会将之后输入的数据作为参数进入过程。特殊的过程如begin、dispaly。 局部变量lambda procedure接受紧跟着它的第一个变量作为参数,传递多个参数可以采用列表的形式传递。 123(define area (lambda (length breadth) (* length breadth))) Applyapply允许直接传递一个list给procedure来完成批量操作例如 123(define x '(1 2 3))(apply + x); ==> 6 apply首先接受一个过程,然后接受不定长度的参数,但最后一个参数一定是list,将这个list与所有的其他参数一起执行这个过程。 顺序执行很多Scheme procedure都隐含了begin语句 123456789(define display3 (lambda (arg1 arg2 arg3) (begin ...