zoukankan      html  css  js  c++  java
  • [2017.02.04] C++学习记录(1)

    编编程语言的目的是帮助程序员以代码的形式表述ideas。编程语言一方面为程序员提供一组关于可以做什么的抽象,另一方面为程序员提供可以被机器执行的轮子。C++编程语言,支持4种编程范式:过程式(Procedural Programming,主要集中在过程和合适的数据结构)、数据抽象(Data abstraction,主要表现为设计抽象接口隐藏具体实现细节)、面向对象编程(Object-oriented programming,主要集中在设计、实现和使用类继承结构,提供运行时多态性)和泛型编程(Generic programming,主要集中在设计、实现和使用泛型算法,提供编译时多态性)。

    C++广泛应用于教学和科学研究,虽然不是“最好”的语言,但是它是一门可以伴随着成长的语言,有如下特点:

    Sufficiently clean for successfully teaching basic design and programming concepts
    Sufficiently comprehensive to be a vehicle for teaching advanced concepts and techniques
    Sufficiently realistic, efficient, and flexible for demanding projects
    Sufficiently commercial to be a vehicle for putting what is learned into nonacademic use
    Sufficiently available for organizations and collaborations relying on diverse development and execution environments

    在学习C++的时候,一是要关注基本概念(fundamental concepts),如类型安全(type safety)、资源管理(resource management)和不变量(invariants)等;二是要学习一些编程技术,如资源管理的实现、算法中的迭代器的使用、设计模式(可复用面向对象软件的基础)等。切记沉浸在语言的细节中。学习语言的目的是为了更加高效设计和实现新的系统并维护旧的工程。因此评判鉴定不同的语言和设计技术,比理解所有的细节更加重要。细节的熟练掌握需要时间的沉淀和实践打磨。

    C++语言有几个不同独立版本的实现,如Microsoft自家在VisualStudio中的实现,GNU的MinGW实现等。每种版本都有自己对C++标准的解释和一些偏见,所有学习的时候,多多综合参考各自的manuals、online resources等,以期达到更加全面的理解。

    学好一门编程语言,就如同学会写作一样。需要我们知道自己想要说什么(Know what you want to say)并且多加练习和模仿名家作品(Practice and imitate good writing)。大道至简却也知易行难

    一些C++学习和使用建议:

    1. C++不仅仅是C的超集。C++虽然可以像C那样使用,但是在维护和性能上属于这是未达到标准的行为。为了真正发挥C++优势,我们需要采用不同的设计和实现风格(implementation styles)。C++提供了有用的语言特性,就不必再使用C的风格(如字符串赋值时使用 = 而不是strcpy(),字符串比较时使用==而不是strcmp())。
    2. C++中尽量少用宏替换(macro substitution),虽然宏替换减少了工作量,但是不利于静态类型检查,不便于调试和维护。可以使用const/constexpr/enum/enum class来定义常量(manifest constants),使用inline减少函数调用的开销,使用templates来定义相似的函数和类型,使用namespace避免名称冲突。
    3. 不要使用原生的malloc/free,使用new/delete结合智能指针unique_ptr/shared_ptr/weak_ptr等实现内存管理;使用vector等类库替换realloc等操作。
    4. 避免隐式类型转换,使用显式的有名转换(explicit named casts),如使用static_cast运算符。
    5. 使用容器和算法,不要认为辛苦地编写C风格的代码就比使用STL库更加有效率,通常吃力不讨好。
    6. 使用constructor/destructor pairs 简化资源管理操作,常使用RAII技术。
    7. 尽量使用久经考验的标准库设施而不是个人开发的代码,以增强代码复用性和可维护性。
    8. 当错误不能够被局部代码处理时,使用exception而不是error。
    9. 使用移动语义(move sematics)避免大对象的值拷贝。
    10. 使用智能指针代替原生指针,不要混用智能指针和原生指针。
    11. 使用模板维护静态类型安全检查和实现编译时多态。
    12. 不要过度抽象(overabstract),避免使用不必要的类继承。
    13. 使用局部变量,避免使用全局变量,最小化指针的使用场合。
    14. Keep simple things simple (without making complex things impossible).

    C++的history/timeline/language features/library facilities。

    经过几十年发展,C++的语言标准也在不停的进化,我们想要做一个合格的码农,也需要跟上语言标准的步伐。学习新技术不是为了学习而学习,而是为了使用新技术解决了过往技术的痛点。如今C++11已经提出了五六年了,新版本C++14/17也近在眼前,如果我们还仅仅停留在C++98而对新技术无动于衷,未免有点故步自封了。学习和使用新版本中的语法特性,使得设计和编程时更加现代化。

    C++11的目标:

    1. Make C++ a better language for systems programming and library building.
    2. Make C++ easier to teach and learn.

    C++11 的 new features:

    1. Control of defaults: =delete and =default.
    2. Deducing the type of an object from its initializer, auto.
    3. Generalized constant expression evaluation (including literal types), constexpr.
    4. In-class member initializers.
    5. Inheriting constructors.
    6. Lambda expressions, a way of implicitly defining function objects at the point of their use in an expression.
    7. Move semantics, a way of transmitting information without copying.
    8. A way of stating that a function may not throw exceptions noexcept.
    9. A proper name for the null pointer.
    10. The range-for statement.
    11. Override controls: final and override.
    12. Type aliases, a mechanism for providing an alias for a type or a template. In particular, a way of defining a template by binding some arguments of another template.
    13. Typed and scoped enumerations: enum class.
    14. Universal and uniform initialization (including arbitrary-length initializer lists and protection against narrowing).
    15. Variadic templates, a mechanism for passing an arbitrary number of arguments of arbitrary types to a template.

    ...

    C++11的library facilities

    1. Hashed containers, such as unordered_map.
    2. The basic concurrency library components, such as thread, mutex, and lock.
    3. Launching asynchronous computation and returning results, future, promise, and async().
    4. The garbage collection interface.
    5. A regular expression library, regexp.
    6. A random number library.
    7. A pointer for simply and efficiently passing resources, unique_ptr.
    8. A pointer for representing shared ownership, shared_ptr.
    9. The tuple library.
    10. The general bind().
    11. The function type for holding callable objects.

    学习C/C++的书单列表

    1. The C Programming Language
    2. The C++ Programming Language
    3. C++ Primer
    4. Computer Systems: A Programmer's Perspective
    5. Code Complete
    6. Design Patterns:Elements of reusable object-orientd- software
    7. Python源码剖析
    8. STL源码剖析
    9. 冒号课堂
    10. Algorithms
    11. Compilers:Principle,Techniques and Tools

    C/C++编程入门和进阶项目

    1. 字符串类
    2. 多线程编程:多线程IO环形队列缓冲大文件英文单词词频统计
    3. 多线程编程:消息传递同步操作的多线程银行ATM模拟
    4. LevelDB源码阅读
    5. Python源码剖析
    6. STL源码剖析
    7. Redis源码阅读

    面向对象编程和设计(Object oriented programming and design)的SOLID原则:

    S:SRP,single responsibility principle,单一职责原则。一个类应该只有一个职责。
    O:OCP,Open/Closed principle,开闭原则。一个软件实体(类、模块、函数等)应该对拓展开放,对修改关闭。这样可以不修改实体的源码而直接拓展它的行为。
    L:LSP,Liskov substitution principle,里氏替换原则。 基类可以被子类替换而不产生类型错误。
    I:ISP,Interface segregation principle,接口隔离原则。 多个特例化的接口优于一个通用的接口。对软件进行解耦合,便于后期重构和维护。
    D:DIP,Dependency inversion principle,依赖倒转原则。高层模块不应爱依赖于底层模块,它们都应该依赖于抽象。抽象不依赖于细节,细节依赖于抽象。

  • 相关阅读:
    electron 显示对话框 showMessageBoxSync showMessageBox
    c++ 随机数 取值范围 多线程
    c++ 字符串转换为数字
    VS2019 C++动态链接库的创建使用(1)
    js mutationobserver property vs attribute
    Chromium base 基础库概览
    Git:合并分支----git merge命令应用的三种情景
    chromium 处理 addEventListener 事件
    JavaScript监听属性改变
    chrome 启动开关参数
  • 原文地址:https://www.cnblogs.com/ausk/p/6366243.html
Copyright © 2011-2022 走看看