`

accelerated c++ 学习笔记

 
阅读更多

Chapter 0

 

A namespace is a collection of related names。Namespaces are a mechanism for grouping related names.

  the standard library uses std to contain all the names that it defines. So, for example, the iostream standard header defines the names cout and endl , and we refer to these names as std::cout and std::endl .

 

output operator , <<。 << is left-associative , which, loosely speaking, means that when << appears twice or more in the same expression, each << will use as much of the expression as it can for its left operand, and as little of it as it can for its right operand.

 

An expression contains operators and operands。Every operand has a type。 a type denotes a data structure and the meanings of operations that make sense for that data structure.

 

std::endl , which is a manipulator。 << does whatever the manipulator says to do to the given stream, and returns the stream as its result。

 

The scope of a name is the part of a program in which that name has its meaning。

  The standard library defines all of its names in a namespace named std , so that it can avoid conflicts with names that we might define for ourselves-as long as we are not so foolish as to try to define std .

 

:: operator. This operator is also known as the scope operator。

  std::cout means "the name cout that is in the (namespace) scope std ."

 

Types define data structures and operations on those data structures.

  C++ has two kinds of types: those built into the core language, such as int , and those that are defined outside the core language, such as std::ostream .

 

The main function: A zero return from main indicates success; a nonzero return indicates failure.

  It may omit the return; explicitly including a return from main is good practice

 

 

chapter 1

 

three events that cause the system to flush the buffer.

  First, the buffer might be full.

  Second, the library might be asked to read from the standard input stream.

  The third occasion for flushing the buffer is when we explicitly say to do so.

 

+ to concatenate a string

  the + operator (and, for that matter, the >> operator) is also left-associative.

  s+t : Either s or t , but not both, may be a string literal or a value of type char .

 

When an operator has different meanings for operands of different types, we say that the operator is overloaded .

 

string construct method:

std::string spaces(greeting.size(), ' ');

 

wchar_t

  Built-in type intended to hold "wide characters," which are big enough to
hold characters for languages such as Japanese.

using -declaration
using std::cout;

size_t

Unsigned integral type (from <cstddef> ) that can hold any object's size

string::size_type

Unsigned integral type that can hold the size of any string


chapter 3

The <ios> header defines streamsize , which is the type that the input-output library uses to represent sizes.

The <iomanip> header defines the manipulator setprecision , which lets us say how many significant digits we want our output to contain.

The endl manipulator is used so often that its definition appears in <iostream>

setprecision(3) , we ask the implementation to write grades with three significant digits, generally two before the decimal point and one after.


sort(homework.begin(), homework.end());
The sort function, defined in the <algorithm> header, rearranges the values in a container so that they are in nondecreasing order.

typedef type name ;    Defines name as a synonym for type.

 

vector<T> v;

Creates an empty vector that can hold elements of type T .

v.push_back(e)

Grows the vector by one element initialized to e .

s.precision(n)

Sets the precision of stream s to n for future output (or leaves it unchanged if n is omitted). Returns the previous precision.

setprecision(n)

Returns a value that, when written on an output stream s , has the effect of calling s.precision(n) . Defined in <iomanip> .

streamsize

The type of the value expected by setprecision and returned by precision . Defined in <ios> .


chapter 4

Saying that a name is a reference to an object says that the name is another name for the object
  const vector<double>& , that we specify for the third argument. This type is often called "reference to vector of const double ." 

// chw is a read-only synonym for 

homework
const vector<double>& chw = homework;
the const promises that we will not do anything to chw that might change its value.

we cannot make "a nonconst reference"  refer to a const object or reference
vector<double>& hw2 = chw;    // error: requests write access to 

chw

several functions with the same name is called overloading.

the clear member behaves completely differently for istream objects than it does for vector objects. For istream objects, it resets any error indications so that input can continue;

try statement. It tries to execute the statements in the { } that follow the try keyword. If a domain_error exception occurs anywhere in these statements, then it stops executing them and continues with the other set of { } -enclosed statements. These statements are part of a catch clause , which begins with the word catch , and indicates the type of exception it is catching.

A good rule of thumb is to avoid more than one side effect in a single statement.
// this example doesn't work




try {
streamsize prec = cout.precision();
cout << "Your final grade is  " << setprecision(3)
     << grade(midterm, final, homework) << setprecision(prec);
} ...
another method is better:
// compute and generate the final grade, if possible 
    try  {
        double final_grade = grade(midterm, final, homework);
        streamsize prec = cout.precision();
        cout << "Your final grade is " << setprecision(3)
             << final_grade << setprecision(prec) << endl; 
    } catch (domain_error) {
        cout << endl << "You must enter your grades.  " 
                        "Please try again." << endl;
        return 1;
    }
 
This struct definition says that Student_info is a type, which has four data members.
struct Student_info {
    string name;
    double midterm, final;
    vector<double> homework;
};  // note the semicolon it's required
 


chapter 4

Exception classes: The library defines several exception classes whose names suggest the kinds of problems they might be used to report:

logic_error     domain_error      invalid_argument
length_error    out_of_range      runtime_error
range_error     overflow_error    underflow_error

e.what()

Returns a value that reports on what happened to cause the error.





分享到:
评论

相关推荐

    Accelerated C++ 中文英文两版高清

    《Accelerated C++》是一本深受程序员喜爱的C++进阶教程,旨在帮助已经具备C或C++基础知识的学习者快速提升编程技能。该书由Andrew Koenig和Barbara E. Moo共同撰写,以其独特的教学方法和高效的学习路径著称。 这...

    Accelerated C++: Practical Programming by Example

    That’s the approach that’s offered by Accelerated C++, a text that delves into more advanced C++ features like templates and Standard Template Library (STL) collection classes early on. This book ...

    Accelerated C++.chm

    《Accelerated C++》真的蛮经典的,被许多人称为小《C++ Primer》,也是公认的C++最佳。 个人认为,只要想学C++的朋友肯花几个月时间认真去掰以下这组书,我相信你的C++就算入门了: 《Accelerated C++》+《The C++ ...

    Accelerated C++ 中文版

    《Accelerated C++ 中文版》是一本专为想要快速掌握C++编程语言的初学者设计的经典教材。这本书以其独特的教学方法和深入浅出的讲解,使得学习C++的过程更为高效且直观。以下是对该书内容的详细概述: C++是一种强...

    Accelerated C++ 英文版

    而所谓的“现代C++风格”便是倡导正确利用C++的抽象机制和这些机制构建出来的现代C++库(以STL为代表)的,Bjarne也很早就倡导将C++当作一门不同于C的新语言来学习(就拿内存管理来说,使用现代C++的内存管理技术,...

    Accelerated C++中文 pdf

    《Accelerated C++》特别强调了学习编程的最佳实践,鼓励使用现代C++编程风格,包括STL(标准模板库)和RAII(资源获取即初始化)原则。 在本书中,读者将逐步学习到C++的基础知识,如变量、控制流、函数、数组和...

    Accelerated C++ PDF 中英文版

    作为一本入门书(Primer),它以教程的形式对C++语言进行清晰的讲解,并辅以丰富的示例和各种学习辅助手段。与大多数入门教程不同,本书对C++语言本身进行了详尽的描述,并特别着重介绍了目前通行的、行之有效的程序...

    Accelerated C++中文版.pdf

    Accelerated C++中文版.pdf

    Accelerated C++: Practical Programming by Example(解密PDF)

    综上所述,《Accelerated C++:通过实例学习实用编程》通过丰富的实例和深入浅出的讲解,为读者提供了一个全面学习和掌握C++编程的良好起点,适合各个层次的C++程序员使用,对于希望深入掌握C++编程的读者来说,这是...

    Accelerated c++中文版 源代码

    《Accelerated C++》是一本深受程序员喜爱的C++学习书籍,它以其高效、实践性强的特点,引导读者快速掌握C++编程基础。源代码是学习编程的重要组成部分,尤其是在阅读和理解书中实例时,能直接查看并运行代码对于...

    Accelerated C++课本源代码

    《Accelerated C++》是一本由Andrew Koenig和Barbara E. Moo共同编著的C++编程教材,它的核心理念是通过实践教学,让学习者快速掌握C++语言的关键概念和技术。这本书的设计旨在打破传统的编程教程模式,通过精心设计...

    Accelerated_C++中文版g.pdf

    从提供的文件信息来看,《Accelerated_C++中文版》是一本关于C++编程语言的教科书。考虑到标题和描述均为“Accelerated_C++中文版”,并且提供了大量的占位字符作为“部分内容”,这表明该文件可能是由OCR扫描技术从...

    Accelerated C++答案.rar

    《Accelerated C++》是Andrew Koenig和Barbara E. Moo合著的一本C++编程教材,以其独特的教学方式和高效的学习路径而备受推崇。这本书旨在帮助初学者快速掌握C++语言的核心概念,同时也适合有经验的程序员作为进阶...

    Accelerated C++ 源代码

    学习《Accelerated C++》的源代码,不仅能够巩固理论知识,还能提升编程技巧,培养良好的编程习惯。通过阅读和分析这些代码,可以更深入地理解C++的内在机制,提高编程效率,为后续的软件开发打下坚实的基础。对于...

    Accelerated C++中文.pdf

    根据提供的信息,我们可以推断这份文档是关于《Accelerated C++》这本书的中文版内容。虽然实际的内容似乎是由无法解析的字符组成,但从标题、描述和标签中,我们可以推测这是一份关于加速学习C++编程语言的指南。...

    Accelerated C++中文版

    《Accelerated C++中文版》是一本专注于C++编程语言学习的高效辅导教材,旨在帮助读者在短时间内迅速掌握C++的核心概念与编程技巧。本书不仅仅是一本普通的编程指南,它采用了独特的教学方法,通过实际代码示例和...

    Accelerated C++中文版(扫描版)

    《Accelerated C++中文版》是一本专注于实践编程并通过实例学习C++语言和标准库的教材。这本书的作者是Andrew Koenig和Barbara E. Moo,由Andrew Koenig本人负责翻译,中国电力出版社出版发行。该书的特色在于其独特...

    Accelerated C++ 习题答案

    "Accelerated C++ 习题答案"这个压缩包文件很可能包含了书中的所有练习题及其解答,对于正在学习或已经学过这门课程的人来说,是极有价值的参考资料。 C++是一种静态类型的、编译式的、通用的、大小写敏感的、不仅...

Global site tag (gtag.js) - Google Analytics