C++ primer笔记(英文版,部分)

Chapter 3. Strings, Vectors, and Arrays

3.1. Namespace using Declarations

  • using std::cout which is similar to the syntax in python from std import cout

3.2. Library string Type

3.2.1. Defining and Initializing strings

  • String initializer:
  • Distinguish copy initialization and direct initialization
    copy initialization: Form of initialization that uses an =. The newly created
    object is a copy of the given initializer. E.g. string s3 = "hiya";
    direct initialization: Form of initialization that does not include an =. E.g. string s4(10, 'c');

3.2.2. Operations on strings

  • String operations

    Notice that == operator for string compares the content rather than the addresses of two operands.
  • getline(is, s) read a line of string, including the whitespace, until seeing a newline.
    Notice that the newline that causes getline to return is discarded; the newline is not stored in the string.
  • size_type is a machine-independent type acting like unsigned, and it is big enough to hold the size of any string. Use auto or decltype to simplify the typing.
    Note: do NOT compare a size_type value to any NEGATIVE value. Or it would yield true at any time.
  • Comparing strings
    Two rules:
  1. If two strings have different lengths and if every character in the shorter
    string is equal to the corresponding character of the longer string, then the
    shorter string is less than the longer one.
  2. If any characters at corresponding positions in the two strings differ, then
    the result of the string comparison is the result of comparing the first
    character at which the strings differ.
    E.g.
  • Note: When using + operator, at least one of the operands must be of string type.
    E.g. string s5 = "hello" + ", "; and string s7 = "hello" + ", " + s2 are illegal, while string s6 = s1 + ", " + "world"; is legal.
    This is because string literals are NOT standard library strings in order for compatibility with C.

3.2.3. Dealing with the Characters in a string

  • cctype functions
  • Range-based for
for (declaration : expression)
	statement
  • Check if a string is empty before getting a character in it.
    The result of using an index outside this range is undefined.
    By implication, subscripting an empty string is undefined.

3.3. Library vector Type

  • Some old compilers might use vector<vector<int> > rather than vector<vector<int>> (notice the whitespace).

3.3.1. Defining and Initializing vectors

  • Ways to Initialize a vector
  • Be care of the differences between list initializer and element count
    E.g.
vector<int> v1(10); // v1 has ten elements with value 0
vector<int> v2{10}; // v2 has one element with value 10
vector<int> v3(10, 1); // v3 has ten elements with value 1
vector<int> v4{10, 1}; // v4 has two elements with values 10 and 1
vector<string> v5{"hi"}; // list initialization: v5 has one element
vector<string> v6("hi"); // error: can't construct a vector from a string literal
vector<string> v7{10}; // v7 has ten default-initialized elements
vector<string> v8{10, "hi"}; // v8 has ten elements with value "hi"
I find using C++more enjoyable than ever.C++’s support for design and programming has improved dramatically over the years,and lots of new helpful techniques have been developed for its use.However,C++is not just fun.Ordinary practical programmers have achieved significant improvements in productivity,maintainability,flexibility,and quality in projects of just about any kind and scale.By now,C++has fulfilled most of the hopes I originally had for it,and also suc- ceeded at tasks I hadn’t even dreamt of. This book introduces standard C++?and the key programming and design techniques supported by C++.Standard C++is a far more powerful and polished language than the version of C++intro- duced by the first edition of this book.New language features such as namespaces,exceptions, templates,and run-time type identification allow many techniques to be applied more directly than was possible before,and the standard library allows the programmer to start from a much higher level than the bare language. About a third of the information in the second edition of this book came from the first.This third edition is the result of a rewrite of even larger magnitude.It offers something to even the most experienced C++programmer;at the same time,this book is easier for the novice to approach than its predecessors were.The explosion of C++use and the massive amount of experience accu- mulated as a result makes this possible. The definition of an extensive standard library makes a difference to the way C++concepts can be presented.As before,this book presents C++independently of any particular implementation, and as before,the tutorial chapters present language constructs and concepts in a‘‘bottom up’’ order so that a construct is used only after it has been defined.However,it is much easier to use a well-designed library than it is to understand the details of its implementation.Therefore,the stan- dard library can be used to provide realistic and interesting examples well before a reader can be assumed to understand its inner workings.The standard library itself is also a fertile source of pro- gramming examples and design techniques. This book presents every major C++language feature and the standard library.It is organized around language and library facilities.However,features are presented in the context of their use. That is,the focus is on the language as the tool for design and programming rather than on the lan- guage in itself.This book demonstrates key techniques that make C++effective and teaches the fundamental concepts necessary for mastery.Except where illustrating technicalities,examples are taken from the domain of systems software.A companion,The Annotated C++Language Stan- dard,presents the complete language definition together with annotations to make it more compre- hensible. The primary aim of this book is to help the reader understand how the facilities offered by C++ support key programming techniques.The aim is to take the reader far beyond the point where he or she gets code running primarily by copying examples and emulating programming styles from other languages.Only a good understanding of the ideas behind the language facilities leads to mastery.Supplemented by implementation documentation,the information provided is sufficient for completing significant real-world projects.The hope is that this book will help the reader gain new insights and become a better programmer and designer. Acknowledgments In addition to the people mentioned in the acknowledgement sections of the first and second edi- tions,I would like to thank Matt Austern,Hans Boehm,Don Caldwell,Lawrence Crowl,Alan Feuer,Andrew Forrest,David Gay,Tim Griffin,Peter Juhl,Brian Kernighan,Andrew Koenig, Mike Mowbray,Rob Murray,Lee Nackman,Joseph Newcomer,Alex Stepanov,David Vandevo- orde,Peter Weinberger,and Chris Van Wyk for commenting on draft chapters of this third edition. Without their help and suggestions,this book would have been harder to understand,contained more errors,been slightly less complete,and probably been a little bit shorter. I would also like to thank the volunteers on the C++standards committees who did an immense amount of constructive work to make C++what it is today.It is slightly unfair to single out indi- viduals,but it would be even more unfair not to mention anyone,so I’d like to especially mention Mike Ball,Dag Br. .u ck,Sean Corfield,Ted Goldstein,Kim Knuttila,Andrew Koenig,Josée Lajoie, Dmitry Lenkov,Nathan Myers,Martin O’Riordan,Tom Plum,Jonathan Shopiro,John Spicer, Jerry Schwarz,Alex Stepanov,and Mike Vilot,as people who each directly cooperated with me over some part of C++and its standard library. Murray Hill,New Jersey Bjarne Stroustrup
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值