A Tour of the Standard Library

A Tour of the Standard Library

________________________________________________________________________________________________________________________________ _______________________________________ ________________________________ 3 ________________________________________________________________________________________________________________________________ _______________________________________ ________________________________ A Tour of the Standard Library Why waste time learning when ignorance is instantaneous? ± Hobbes Standard libraries Ð output Ð strings Ð input Ð vectors Ð range checking Ð lists Ð maps Ð container overview Ð algorithms Ð iterators Ð I/O iterators Ð traversals and predicates Ð algorithms using member functions Ð algorithm overview Ð complex numbers Ð vector arithmeticÐ standard library overview Ð advice. 3.1 Introduction No significant program is written in just a bare programming language. First, a set of supporting libraries are developed. These then form the basis for further work. Continuing Chapter 2, this chapter gives a quick tour of key library facilities to give you an idea what can be done using C++ and its standard library. Useful library types, such as sst tr ri in ng g , vve ec ct to or r , lli is st t , and mma ap p , are presented as well as the most common ways of using them. Doing this allows me to give better examples and to set better exercises in the following chapters. As in Chapter 2, you are strongly encouraged not to be distracted or discouraged by an incomplete understanding of details. The purpose of this chapter is to give you a taste of what is to come and to convey an understanding of the simplest uses of the most useful library facilities. A more detailed introduc- tion to the standard library is given in §16.1.2. The standard library facilities described in this book are part of every complete C++ implemen- tation. In addition to the standard C++ library, most implementations offer ``graphical user inter- face'' systems, often referred to as GUIs or window systems, for interaction between a user and a program. Similarly, most application development environments provide ``foundation libraries'' that support corporate or industrial ``standard'' development and/or execution environments. I do not describe such systems and libraries. The intent is to provide a self-contained description of C++ The C++ Programming Language, Special Edition by Bjarne Stroustrup. Copyright 2000 by AT&T. Published by Addison Wesley, Inc. ISBN 0-201-70073-5. All rights reserved. 46 A Tour of the Standard Library Chapter 3 as defined by the standard and to keep the examples portable, except where specifically noted. Nat- urally, a programmer is encouraged to explore the more extensive facilities available on most sys- tems, but that is left to exercises. 3.2 Hello, world! The minimal C++ program is iin nt t mma ai in n () { } It defines a function called mma ai in n , which takes no arguments and does nothing. Every C++ program must have a function named mma ai in n (). The program starts by executing that function. The iin nt t value returned by mma ai in n (), if any, is the program's return value to ``the system.'' If no value is returned, the system will receive a value indicating successful completion. A nonzero value from mma ai in n () indicates failure. Typically, a program produces some output. Here is a program that writes out HHe el ll lo o , wwo or rl ld d !: #iin nc cl lu ud de e <iio os st tr re ea am m > iin nt t mma ai in n () { sst td d :: cco ou ut t << "HHe el ll lo o , wwo or rl ld d !\\n n "; } The line #iin nc cl lu ud de e <iio os st tr re ea am m > instructs the compiler to include the declarations of the standard stream I/O facilities as found in iio os st tr re ea am m . Without these declarations, the expression sst td d :: cco ou ut t << "HHe el ll lo o , wwo or rl ld d !\\n n " would make no sense. The operator << (``put to'') writes its second argument onto its first. In this case, the string literal "HHe el ll lo o , wwo or rl ld d !\\n n " is written onto the standard output stream sst td d :: cco ou ut t . A string literal is a sequence of characters surrounded by double quotes. In a string literal, the back- slash character \ followed by another character denotes a single special character. In this case, \\n n is the newline character, so that the characters written are HHe el ll lo o , wwo or rl ld d ! followed by a newline. 3.3 The Standard Library Namespace The standard library is defined in a namespace (§2.4, §8.2) called sst td d . That is why I wrote sst td d :: cco ou ut t rather than plain cco ou ut t . I was being explicit about using the sst ta an nd da ar rd d cco ou ut t , rather than some other cco ou ut t . Every standard library facility is provided through some standard header similar to <iio os st tr re ea am m >. For example: #iin nc cl lu ud de e <sst tr ri in ng g > #iin nc cl lu ud de e <lli is st t > This makes the standard sst tr ri in ng g and lli is st t available. To use them, the sst td d :: prefix can be used: The C++ Programming Language, Special Edition by Bjarne Stroustrup. Copyright 2000 by AT&T. Published by Addison Wesley, Inc. ISBN 0-201-70073-5. All rights reserved. Section 3.3 The Standard Library Namespace 47 sst td d :: sst tr ri in ng g s = "FFo ou ur r lle eg gs s GGo oo od d ; ttw wo o lle eg gs s BBa aa aa ad d !"; sst td d :: lli is st t <sst td d :: sst tr ri in ng g > ssl lo og ga an ns s ; For simplicity, I will rarely use the sst td d :: prefix explicitly in examples. Neither will I always #iin nc cl lu ud de e the necessary headers explicitly. To compile and run the program fragments here, you must #iin nc cl lu ud de e the appropriate headers (as listed in §3.7.5, §3.8.6, and Chapter 16). In addition, you must either use the sst td d :: prefix or make every name from sst td d global (§8.2.3). For example: #iin nc cl lu ud de e <sst tr ri in ng g > // make the standard string facilities accessible uus si in ng g nna am me es sp pa ac ce e sst td d ; // make std names available without std:: prefix sst tr ri in ng g s = "IIg gn no or ra an nc ce e iis s bbl li is ss s !"; // ok: string is std::string It is generally in poor taste to dump every name from a namespace into the global namespace. However, to keep short the program fragments used to illustrate language and library features, I omit repetitive #iin nc cl lu ud de e s and sst td d :: qualifications. In this book, I use the standard library almost exclusively, so if a name from the standard library is used, it either is a use of what the standard offers or part of an explanation of how the standard facility might be defined. 3.4 Output The iostream library defines output for every built-in type. Further, it is easy to define output of a user-defined type. By default, values output to cco ou ut t are converted to a sequence of characters. For example, vvo oi id d f () { cco ou ut t << 110 0 ; } will place the character 1 followed by the character 0 on the standard output stream. So will vvo oi id d g () { iin nt t i = 110 0 ; cco ou ut t << i ; } Output of different types can be combined in the obvious way: vvo oi id d h (iin nt t i ) { cco ou ut t << "tth he e vva al lu ue e oof f i iis s "; cco ou ut t << i ; cco ou ut t << Â\\n n Â; } If i has the value 110 0 , the output will be tth he e vva al lu ue e oof f i iis s 110 0 The C++ Programming Language, Special Edition by Bjarne Stroustrup. Copyright 2000 by AT&T. Published by Addison Wesley, Inc. ISBN 0-201-70073-5. All rights reserved. 48 A Tour of the Standard Library Chapter 3 A character constant is a character enclosed in single quotes. Note that a character constant is out- put as a character rather than as a numerical value. For example, vvo oi id d k () { cco ou ut t << Âa Â; cco ou ut t << Âb Â; cco ou ut t << Âc Â; } will output aab bc c . People soon tire of repeating the name of the output stream when outputting several related items. Fortunately, the result of an output expression can itself be used for further output. For example: vvo oi id d hh2 2 (iin nt t i ) { cco ou ut t << "tth he e vva al lu ue e oof f i iis s " << i << Â\\n n Â; } This is equivalent to h (). Streams are explained in more detail in Chapter 21. 3.5 Strings The standard library provides a sst tr ri in ng g type to complement the string literals used earlier. The sst tr ri in ng g type provides a variety of useful string operations, such as concatenation. For example: sst tr ri in ng g ss1 1 = "HHe el ll lo o "; sst tr ri in ng g ss2 2 = "wwo or rl ld d "; vvo oi id d mm1 1 () { sst tr ri in ng g ss3 3 = ss1 1 + ", " + ss2 2 + "!\\n n "; cco ou ut t << ss3 3 ; } Here, ss3 3 is initialized to the character sequence HHe el ll lo o , wwo or rl ld d ! followed by a newline.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    22 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us