1. Trang chủ
  2. » Công Nghệ Thông Tin

The C++ Programming Language Third Edition phần 7 pptx

102 835 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 102
Dung lượng 350,1 KB

Nội dung

Section 20.6 Exercises 603 §20.6[7]? Specify and add them Compare the expressiveness of your regular expression matcher to that of a widely distributed one Compare the performance of your regular expression matcher to that of a widely distributed one (∗2.5) Use a regular expression library to implement pattern-matching operations on a S tr in g St ri ng class that has an associated S ub st ri ng class Su bs tr in g 10 (∗2.5) Consider writing an ‘‘ideal’’ class for general text processing Call it T ex t What faciliTe xt ties should it have? What implementation constraints and overheads are imposed by your set of ‘‘ideal’’ facilities? 11 (∗1.5) Define a set of overloaded versions for i sa lp is al ph a(), i sd ig it is di gi t(), etc., so that these functions work correctly for c r, u ns ig ne d c r, and s ig ne d c r ch ar un si gn ed ch ar si gn ed ch ar 12 (∗2.5) Write a S tr in g class optimized for strings having no more than eight characters ComSt ri ng pare its performance to that of the S tr in g from §11.12 and your implementation’s version of the St ri ng standard library s tr in g Is it possible to design a string that combines the advantages of a string st ri ng optimized for very short strings with the advantages of a perfectly general string? 13 (∗2) Measure the performance of copying of s tr in gs Does your implementation’s implementast ri ng tion of s tr in g adequately optimize copying? st ri ng 14 (∗2.5) Compare the performance of the three c om pl et e_ na me co mp le te _n am e() functions from §20.3.9 and §20.3.10 Try to write a version of c om pl et e_ na me co mp le te _n am e() that runs as fast as possible Keep a record of mistakes found during its implementation and testing 15 (∗2.5) Imagine that reading medium-long strings (most are to 25 characters long) from c in is ci n the bottleneck in your system Write an input function that reads such strings as fast as you can think of You can choose the interface to that function to optimize for speed rather than for convenience Compare the result to your implementation’s >> for s tr in gs st ri ng 16 (∗1.5) Write a function i to s(i nt that returns a s tr in g representing its i nt argument it os in t) st ri ng in t The C++ Programming Language, Third Edition by Bjarne Stroustrup Copyright ©1997 by AT&T Published by Addison Wesley Longman, Inc ISBN 0-201-88954-4 All rights reserved 604 Strings Chapter 20 The C++ Programming Language, Third Edition by Bjarne Stroustrup Copyright ©1997 by AT&T Published by Addison Wesley Longman, Inc ISBN 0-201-88954-4 All rights reserved 21 Streams What you see is all you get – Brian Kernighan Input and output — o st re am — output of built-in types — output of user-defined types os tr ea ms — virtual output functions — i st re am — input of built-in types — unformatted input is tr ea ms — stream state — input of user-defined types — I/O exceptions — tying of streams — sentries — formatting integer and floating-point output — fields and adjustments — manipulators — standard manipulators — user-defined manipulators — file streams — closing streams — string streams — stream buffers — locale — stream callbacks — p ri nt f() — advice — exercises pr in tf 21.1 Introduction [io.intro] Designing and implementing a general input/output facility for a programming language is notoriously difficult Traditionally, I/O facilities have been designed exclusively to handle a few built-in data types However, a nontrivial C++ program uses many user-defined types, and the input and output of values of those types must be handled An I/O facility should be easy, convenient, and safe to use; efficient and flexible; and, above all, complete Nobody has come up with a solution that pleases everyone It should therefore be possible for a user to provide alternative I/O facilities and to extend the standard I/O facilities to cope with special applications C++ was designed to enable a user to define new types that are as efficient and convenient to use as built-in types It is therefore a reasonable requirement that an I/O facility for C++ should be provided in C++ using only facilities available to every programmer The stream I/O facilities presented here are the result of an effort to meet this challenge: §21.2 Output: What the application programmer thinks of as output is really the conversion of objects of types, such as i nt c r*, and E mp lo ye e_ re co rd into sequences of characin t, ch ar Em pl oy ee _r ec or d, ters The facilities for writing built-in and user-defined types to output are described The C++ Programming Language, Third Edition by Bjarne Stroustrup Copyright ©1997 by AT&T Published by Addison Wesley Longman, Inc ISBN 0-201-88954-4 All rights reserved 606 Streams Chapter 21 §21.3 Input: The facilities for requesting input of characters, strings, and values of other builtin and user-defined types are presented §21.4 Formatting: There are often specific requirements for the layout of the output For example, i nt may have to be printed in decimal and pointers in hexadecimal or in ts floating-point numbers must appear with exactly specified precision Formatting controls and the programming techniques used to provide them are discussed §21.5 Files and Streams: By default, every C++ program can use standard streams, such as standard output (c ou t), standard input (c in and error output (c er r) To use other co ut ci n), ce rr devices or files, streams must be created and attached to those files or devices The mechanisms for opening and closing files and for attaching streams to files and s tr in gs st ri ng are described §21.6 Buffering: To make I/O efficient, we must use a buffering strategy that is suitable for both the data written (read) and the destination it is written to (read from) The basic techniques for buffering streams are presented §21.7 Locale: A l oc al e is an object that specifies how numbers are printed, what characters are lo ca le considered letters, etc It encapsulates many cultural differences Locales are implicitly used by the I/O system and are only briefly described here §21.8 C I/O: The p ri nt f() function from the C library and the C library’s relation pr in tf st di o.h to the C++ library are discussed io st re am Knowledge of the techniques used to implement the stream library is not needed to use the library Also, the techniques used for different implementations will differ However, implementing I/O is a challenging task An implementation contains examples of techniques that can be applied to many other programming and design tasks Therefore, the techniques used to implement I/O are worthy of study This chapter discusses the stream I/O system to the point where you should be able to appreciate its structure, to use it for most common kinds of I/O, and to extend it to handle new userdefined types If you need to implement the standard streams, provide a new kind of stream, or provide a new locale, you need a copy of the standard, a good systems manual, and/or examples of working code in addition to what is presented here The key components of the stream I/O systems can be represented graphically like this: i os _b as e: io s_ ba se locale independent format state b as ic _i os ba si c_ io s< >: locale dependent format state stream state b as ic _i os tr ea m< >: ba si c_ io st re am formatting (, etc.) setup/cleanup b as ic _s tr ea mb uf ba si c_ st re am bu f< >: buffering l oc al e: lo ca le format information character buffer real destination/source The C++ Programming Language, Third Edition by Bjarne Stroustrup Copyright ©1997 by AT&T Published by Addison Wesley Longman, Inc ISBN 0-201-88954-4 All rights reserved Section 21.1 Introduction 607 The dotted arrow from b as ic _i os tr ea m indicates that b as ic _i os ba si c_ io st re am ba si c_ io s is a virtual base class; the solid arrows represent pointers The classes marked with are templates parameterized by a character type and containing a l oc al e lo ca le The streams concept and the general notation it provides can be applied to a large class of communication problems Streams have been used for transmitting objects between machines (§25.4.1), for encrypting message streams (§21.10[22]), for data compression, for persistent storage of objects, and much more However, the discussion here is restricted to simple character-oriented input and output Declarations of stream I/O classes and templates (sufficient to refer to them but not to apply operations to them) and standard t yp ed ef are presented in This header is occasionally ty pe de fs io sf wd needed when you want to include some but not all of the I/O headers 21.2 Output [io.out] Type-safe and uniform treatment of both built-in and user-defined types can be achieved by using a single overloaded function name for a set of output functions For example: p ut ce rr x = "); // cerr is the error output stream pu t(c er r,"x p ut ce rr x); pu t(c er r,x p ut ce rr \n ; pu t(c er r,´\ n´) The type of the argument determines which p ut function will be invoked for each argument This pu t solution is used in several languages However, it is repetitive Overloading the operator

Ngày đăng: 12/08/2014, 19:21

TỪ KHÓA LIÊN QUAN