1. Trang chủ
  2. » Ngoại Ngữ

Programming in c++ for engineering and science (1)

730 5 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 730
Dung lượng 20,92 MB

Nội dung

C++ for Engineering and Science Developed from the author’s many years of teaching computing courses, Programming in C++ for Engineering and Science guides readers in designing programs to solve real problems encountered in engineering and scientific applications These problems include radioactive decay, pollution indexes, digital circuits, differential equations, Internet addresses, data analysis, simulation, quality control, electrical networks, data encryption, beam deflection, and many other areas Nyhoff Features • Uses standard C++ throughout • Explains key concepts, such as functions and classes, through a “use it first, build it later” approach • Shows how to develop programs to solve real problems, emphasizing the proper techniques of design and style • Introduces the very powerful and useful Standard Template Library along with important class and function templates • Develops numeric techniques and programs for some engineering and science example problems • Highlights key terms, important points, design and style suggestions, and common programming pitfalls in the chapter summaries • Includes self-study questions and programming projects in each chapter • Provides ancillary materials on the book’s website Programming in C++ for Engineering and Science C++ To make it easier for novices to develop programs, the author uses an object-centered design approach that helps readers identify the objects in a problem and the operations needed; develop an algorithm for processing; implement the objects, operations, and algorithm in a program; and test, correct, and revise the program He also revisits topics in greater detail as the text progresses By the end of the book, readers will have a solid understanding of how C++ can be used to process complex objects, including how classes can be built to model objects Programming in Programming in for Engineering and Science Computer Science Larry Nyhoff K11207 K11207_Cover.indd 6/19/12 9:01 AM Programming in C++ for Engineering and Science K11207.indb 6/15/12 10:06 AM This page intentionally left blank Programming in C++ for Engineering and Science Larry Nyhoff K11207.indb 6/15/12 10:06 AM CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2012 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S Government works Version Date: 20120409 International Standard Book Number-13: 978-1-4398-2535-8 (eBook - PDF) This book contains information obtained from authentic and highly regarded sources Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint Except as permitted under U.S Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400 CCC is a not-for-profit organization that provides licenses and registration for a variety of users For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com Contents Preface, vii Acknowledgments, xi About the Author, xiii Chapter ■ Introduction to Computing Chapter ■ Programming and Problem Solving— Software Engineering 21 Chapter ■ Types in C++ 39 Chapter ■ Getting Started with Expressions 63 Chapter ■ Control Structures 107 Chapter ■ Functions and Libraries 149 Chapter ■ Using Classes 207 Chapter ■ More Selection Control Structures 261 Chapter ■ More Repetition Control Structures 295 Chapter 10 ■ Functions in Depth 351 Chapter 11 ■ Files and Streams 409 Chapter 12 ■ Arrays and the vector Class Template 451 Chapter 13 ■ Multidimensional Arrays and Vectors 503 v vi    ◾    Contents Chapter 14 ■ Building Classes 553 Chapter 15 ■ Pointers and Linked Structures 593 Chapter 16 ■ Data Structures 637 Answers to Test Yourself Questions, 677 Appendix A: ASCII Character Codes, 693 Appendix B: C++ Keywords, 697 Appendix C: C++ Operators, 699 Appendix D: Other C++ Features, 701 Index, 715 K11207.indb 6/15/12 10:06 AM Preface C ++ is a general-purpose programming language that has both high-level and low-level language features Bjarne Stroustrup developed it in 1979 at Bell Labs as a series of enhancements to the C programming language, which, although developed for system programming, has been used increasingly in engineering and scientific applications Because the first enhancement was the addition of classes, the resulting language was originally named Bjarne Stroustrup “C with Classes,” but was renamed C++ in 1983 Along with overcoming some of the dangers and disadvantages of C, these and subsequent enhancements have resulted in a very powerful language in which very efficient programs can be written and developed using the object-oriented paradigm A programming language standard for C++ (ISO/IEC148821998) was adopted in 1998 and revised in 2003 and is the basis for this text Background and Content This text grew out of many years of teaching courses in computing, including programming courses intended for students majoring in engineering and science Although the Fortran language was first used, these courses are now taught using C++ However, most C++ textbooks are written for the general college student and thus include examples and some content that is not aimed at or especially relevant to science and engineering students In this text, nearly all of the examples and exercises involve engineering and scientific applications, including the following (and many more): • Temperature conversion • Radioactive decay • Einstein’s equation • Pollution indexes • Digital circuits • Root finding, integration, differential equations vii viii    ◾    Preface • Internet addresses • A-C circuits • Simulation • Quality control • Street networks • Environmental data analysis • Searching a chemistry database • Oceanographic data analysis • Electrical networks • Coordinate transformations • Data encryption • Beam deflection • Weather data analysis • Oceanographic data analysis Some examples are described and solved in detail, while for others the presentation in the text outlines the solution and the complete development is available on the text’s website maintained by the author: http://cs.calvin.edu/books/c++/engr-sci This text also focuses on those features of C++ that are most important in engineering and science applications, with other features described in optional sections, appendices, or on the text’s website This makes it useable in a variety of courses ranging from a regular full-credit course to one with reduced credit such as a two-credit course that the author has taught many times, where the class lectures are supplemented by lab exercises—tutorial in nature—in which the students develop a program to solve some problem using the new language features presented in class Presentation The basic approach of the text is a spiral approach that revisits topics in increasingly more detail For example, the basic C++ operations used to build expressions are presented first, and then predefined functions provided in C++ libraries are added Once students have experience with functions, they learn how to define their own simple functions and then more complicated ones Later they learn how to incorporate these into libraries of their own, thus extending the C++ language with custom-designed libraries K11207.indb 6/15/12 10:06 AM Preface    ◾    ix Learning how to develop a program from scratch, however, can be a difficult and challenging task for novice programmers A methodology used in this text for designing programs to solve problems, developed over years of teaching C++ to computer science, engineering, and science students and coauthoring texts in C++, is called object-centered design (OCD): • Identify the objects in the problem that need to be processed • Identify the operations needed to this processing • Develop an algorithm for this processing • Implement these objects, operations, and algorithm in a program • Test, correct, and revise the program Although this approach cannot technically be called object-oriented design (OOD), it does focus on the objects and operations on these objects in a problem As new language constructs are learned, they are incorporated into the design process For example, simple types of objects are used in early chapters, but Chapter introduces students to some of the standard classes provided in C++ for processing more complex objects—those that have multiple attributes In subsequent chapters, more classes are introduced and explained, and students gain more practice in using them and understanding the structure of a class Once they have a good understanding of these predefined standard classes, in Chapter 14 they learn how to build their own classes to model objects, thus extending the C++ language to include a new custom-built type Important Features • Standard C++ is used throughout • A “use it first—build it later” approach is used for key concepts such as functions (use predefined functions first, build functions later) and classes (use predefined classes first, build classes later) Various other topics are similarly introduced early and used, and are expanded later—a spiral kind of approach • The very powerful and useful Standard Template Library (STL) is introduced and some of the important class templates (e.g., vector) and function templates (e.g., sort()) are presented in detail • C++’s language features that are not provided in C are noted • Engineering and science examples, including numeric techniques, are emphasized • Programs for some examples are developed in detail; for others, the design of a program is outlined and a complete development is available on the text’s website • Object-centered design (OCD) helps students develop programs to solve problems • Proper techniques of design and style are emphasized and used throughout K11207.indb 6/15/12 10:06 AM Appendix D: Other C++ Features C Libraries Many of the C++ libraries were originally C libraries The following describes some of the most useful items in the more commonly used libraries cassert (Formerly assert.h) void assert(bool expr) Tests the boolean expression expr and if it is true, allows execution to proceed If it is false, execution is terminated and an error message is displayed cctype (Formerly ctype.h) int isalnum(int c)  Returns true if c is a letter or a digit, false otherwise int isalpha(int c)  Returns true if c is a letter, false otherwise int iscntrl(int c)  R eturns  true  if  c   is  a  control  character,  fa ls e otherwise int isdigit(int c) Returns true if c is a decimal digit, false otherwise int isgraph(int c)  Returns true if c is a printing character except space, false otherwise int islower(int c) Returns true if c is lowercase, false otherwise int isprint(int c)  Returns true if c is a printing character including space, false otherwise int ispunct(int c)  Returns true if c is a punctuation character (not a space, an alphabetic character, or a digit), false otherwise 701 702    ◾    Appendix D: Other C++ Features int isspace(int c)  Returns true if c is a white space character (space, '\f', '\n', '\r', '\t', or '\v'), false otherwise Returns true if c is uppercase, false otherwise int isupper(int c) int isxdigit(int c)  Returns   true   if  c   is  a   hexadecimal   digit,   false otherwise int tolower(int c)  Returns lowercase equivalent of c (if c is uppercase) int toupper(int c)  Returns the uppercase equivalent of c (if c is lowercase) cfloat (Formerly float.h) The following constants specify the minimum value in the specified floating-point type FLT_MIN (≤ –1E+37) float DBL_MIN (≤ –1E+37) double LDBL_MIN (≤ –1E+37) long double The following constants specify the maximum value in the specified floating-point type FLT_MAX (≥ 1E+37) float DBL_MAX (≥ 1E+37) double LDBL_MAX (≥ 1E+37) long double The following constants specify the smallest positive value representable in the specified floating-point type FLT_EPSILON (≤ 1E–37) float DBL_EPSILON (≤ 1E–37) double LDBL_EPSILON (≤ 1E–37) long double climits (Formerly limits.h) The following constants specify the minimum and maximum values for the specified type SCHAR_MIN (≤ –127) signed char SCHAR_MAX (≥ 127) signed char UCHAR_MAX (≥ 255) unsigned char CHAR _ MIN (0 or SCHAR_MIN) char K11207.indb 702 6/15/12 10:09 AM Appendix D: Other C++ Features    ◾    703 CHAR_MAX (SCHAR_MAX or USHRT_MAX) char SHRT_MIN (≤ –32767) short int SHRT_MAX (≥ 32767) short int USHRT_MAX (≥ 65535) unsigned short int INT_MIN (≤ –32767) int INT_MAX (≥ 32767) int UINT_MAX (≥ 65535) unsigned int LONG_MIN (≤ –2147483647) long int LONG_MAX (≥ 2147483647) long int ULONG_MAX (≥ 4294967295) unsigned long int cmath (Formerly math.h) double acos(double x)  Returns the angle in [0, π] (in radians) whose cosine is x double asin(double x)  Returns the angle in [–π/2, π/2] (in radians) whose sine is x double atan(double x)  Returns the angle in (–π/2, π/2) (in radians) whose tangent is x double atan2(double y)  Returns the angle in (–π, π) (in radians) whose tangent is y/x double ceil(double x) Returns the least integer ≥ x double cos(double x) Returns the cosine of x (radians) double cosh(double x) Returns the hyperbolic cosine of x double exp(double x) Returns ex double fabs(double x) Returns the absolute value of x double floor(double x) Returns the greatest integer ≤ x double fmod(double x, double y) Returns the integer remainder of x / y double frexp(double x, Returns value v in [1/2, 1] and int & ex)  passes back ex such that x   =   v   *   2ex double ldexp(double x, int ex) K11207.indb 703 Returns x * 2ex 6/15/12 10:09 AM 704    ◾    Appendix D: Other C++ Features double log(double x) Returns natural logarithm of x double log10(double x) Returns base-ten logarithm of x double modf(double x, Returns fractional part of x and double & ip)  passes back ip = the integer part of x double pow(double x, double y) Returns xy double sin(double x) Returns the sine of x (radians) double sinh(double x) Returns the hyperbolic sine of x double sqrt(double x)  Returns the square root of x (provided x ≥ 0) double tan(double x) Returns the tangent of x (radians) double tanh(double x) Returns the hyperbolic tangent of x cstdlib (Formerly stdlib.h) int abs(int i) abs(i) and labs(li) return the int and long int long abs(long li)  absolute value of i and li, respectively double atof(char s[])  atof(s),  atoi(s),  and  atol(s)  return  the value int atoi(char s[])  obtained by converting the character string s to double, long atol(char s[]) int, and long int, respectively void exit(int status)  Terminates program execution and returns control to the operating system; status = signals successful termination and any nonzero value signals unsuccessful termination int rand()  Returns a pseudorandom integer in the range to RAND_MAX RAND_MAX  An integer constant (≥ 32767) which is the maximum value returned by rand() void srand(int seed) Uses seed to initialize the sequence of pseudorandom numbers returned by rand() int system(char s[])  Passes the string s to the operating system to be executed as a command and returns an implementationdependent value K11207.indb 704 6/15/12 10:09 AM Appendix D: Other C++ Features    ◾    705 The string Class The string class, which was described in Chapter 7, is defined by typedef basic_string string; The unsigned integer type size_type is defined in this class as is an integer constant npos, which is some integer that is either negative or greater than the number of characters in a string The following is a list of the major operations defined on a string object s; pos, pos1, pos2, n, n1, and n2 are of type size_type; str, str1, and str2 are of type string; charArray is a character array; ch and delim are of type char; istr is an istream; ostr is an ostream; it1 and it2 are iterators; and inpIt1 and inpIt2 are input iterators All of these operations except >>, s  Extracts characters from istr and stores them in s until s.max_size() characters have been extracted, the end of file occurs, or a ­­white-space character is encountered, in which case the white-space character is not removed from istr; returns istr ostr t, t > s relational operator; t may be a string or a s = s s == t, t == s s != t, t != s s.append(str)  Appends string str at the end of s; returns s s.append(str, pos, n)  Appends at the end of s a copy of the n characters in str, starting at position pos; if n is too large, characters are copied only until the end of str is reached; returns s s.append(charArray)  Appends charArray at the end of s; returns s s.append(charArray, n)  Appends the first n characters in charArray at the end of s; returns s s.append(n, ch)  Appends n copies of ch at the end of s; returns s s.append(inpIt1, inpIt2)  Appends copies of the characters in the range [inpIt1, inpIt2) to s; returns s s.assign(str) K11207.indb 706 Assigns a copy of str to s; returns s 6/15/12 10:09 AM Appendix D: Other C++ Features    ◾    707 s.assign(str, pos, n)  Assigns to s a copy of the n characters in str, starting at position pos; if n is too large, characters are copied only until the end of str is reached; returns s s.assign(charArray)  Assigns to s a copy of charArray; returns s s.assign(charArray, n)  Assigns to s a string consisting of the first n characters in charArray; returns s s.assign(n, ch)  Assigns to s a string consisting of n copies of ch; returns s s.assign(inpIt1, inpIt2)  Assigns to s a string consisting of the characters in the range [inpIt1, inpIt2); returns s s.at(pos) Returns s[pos] s.begin() Returns an iterator positioned at the first character in s s.c_str()  Returns (the base address of) a char array containing the characters stored in s, terminated by a null character s.capacity()  Returns the size (of type size_type) of the storage allocated in s s.clear()  Removes all the characters in s; return type is void s.compare(str)  Returns a negative value, 0, or a positive value according as s is less than, equal to, or greater than str s.compare(charArray)  Compares s and charArray as in the preceding method s.compare(pos, n, str)  Compares strings s and str as before, but starts at position pos in s and compares only the next n characters s.compare(pos, n, charArray)  Compares string s and charArray as in the preceding method s.compare(pos1, n1, str, Compares s and str as before, but pos2, n2)  starts at position pos1 in s, position pos2 in str, and compares only the next n1 characters in s and the next n2 characters in str K11207.indb 707 6/15/12 10:09 AM 708    ◾    Appendix D: Other C++ Features s.compare(pos1, n1, Compares strings s and charArray as before, charArray, n2)  but using only the first n2 characters in charArray s.copy(charArray, pos, n)  Replaces the string in s with n characters in charArray, starting at position pos or at position 0, if pos is omitted; if n is too large, characters are copied only until the end of charArray is reached; returns the number (of type size_type) of characters copied s.data()  Returns a char array containing the characters stored in s s.empty()  Returns true if s contains no characters, false otherwise s.end()  Returns an iterator positioned immediately after the last character in s s.erase(pos, n)  Removes n characters from s, beginning at position pos (default value 0); if n is too large or is omitted, characters are erased only to the end of s; returns s s.erase(it)  Removes the character at the position specified by it; returns an iterator positioned immediately after the erased character s.find(str, pos)  Returns the first position ≥ pos such that the next str.size() characters of s match those in str; returns npos if there is no such position; is the default value for pos s.find(ch, pos)  Searches s as in the preceding method, but for ch s.find(charArray, pos)  Searches s as in the preceding method, but for the characters in charArray s.find(charArray, pos, n)  Searches s as in the preceding method, but for the first n characters in charArray; the value pos must be given s.find_first_not_of(str, pos)  Returns the first position ≥ pos of a character in s that does not match any of the characters in str; returns npos if there is no such position; is the default value for pos K11207.indb 708 6/15/12 10:09 AM Appendix D: Other C++ Features    ◾    709 s.find_first_not_of (ch, pos) Searches s as in the preceding method, but for ch s.find_first_not_of (charArray, pos) Searches s as in the preceding method, but for the characters in charArray s.find_first_not_of (charArray, pos, n) Searches s as in the preceding method, but using the first n characters in charArray; the value pos must be given s.find_first_of(str, pos) eturns the first position ≥ pos of a character R in s that matches any character in str; returns npos if there is no such position; is the default value for pos s.find_first_of(ch, pos) Searches s as in the preceding method, but for ch s.find_first_of (charArray, pos) Searches s as in the preceding method, but for the characters in charArray s.find_first_of (charArray, pos, n) Searches s as in the preceding method, but using the first n characters in charArray; the value pos must be given s.find_last_not_of (str, pos) Returns the highest position ≤ pos of a character in s that does not match any character in str; returns npos if there is no such position; npos is the default value for pos s.find_last_not_of(ch, pos) Searches s as in the preceding method, but for ch s.find_last_not_of (charArray, pos) Searches s as in the preceding method, but using the characters in charArray s.find_last_not_of (charArray, pos, n) Searches s as in the preceding method, but using the first n characters in charArray; the value pos must be given s.find_last_of(str, pos) eturns the highest position ≤ pos of a charR acter in s that matches any character in str; returns npos if there is no such position; npos is the default value for pos s.find_last_of(ch, pos) Searches s as in the preceding method, but for ch s.find_last_of (charArray, pos) Searches s as in the preceding method, but using the characters in charArray K11207.indb 709 6/15/12 10:09 AM 710    ◾    Appendix D: Other C++ Features s.find_last_of (charArray, pos, n) Searches s as in the preceding method, but using the first n characters in charArray; the value pos must be given s.insert(pos, str)  Inserts a copy of str into s at position pos; returns s s.insert(pos1, str, pos2, n)  Inserts a copy of n characters of str starting at position pos2 into s at position pos; if n is too large, characters are copied only until the end of str is reached; returns s s.insert(pos, charArray, n)  Inserts a copy of the first n characters of charArray into s at position pos; inserts all of its characters if n is omitted; returns s s.insert(pos, n, ch) Inserts n copies of the character ch into s at position pos; returns s s.insert(it, ch) Inserts a copy of the character ch into s at the position specified by it and returns an iterator positioned at this copy s.insert(it, n, ch) Inserts n copies of the character ch into s at the position specified by it; return type is void s.insert(it, inpIt1, inpIt2)  Inserts copies of the characters in the range [inpIt1, inpIt2) into s at the position specified by it; return type is void s.length()  Returns the length (of type size_type) of s s.max_size()  Returns the maximum length (of type size_ type) of s s.rbegin() Returns a reverse iterator positioned at the last character in s s.rend() Returns a reverse iterator positioned immediately before the first character in s s.replace(pos1, n1, str)  Replaces the substring of s of length n1 beginning at position pos1 with str; if n1 is too large, all characters to the end of s are replaced; returns s s.replace(it1, it2, str)  Same as the preceding, but for the substring of s consisting of the characters in the range [it1, it2); returns s K11207.indb 710 6/15/12 10:09 AM Appendix D: Other C++ Features    ◾    711 s.replace(pos1, n1, str, pos2, n2) s.replace(pos1, n1, charArray, n2) s.replace(it1, it2, charArray, n2) Replaces a substring of s as in the preceding reference but using n2 characters in str, beginning at position pos2; if n2 is too large, characters to the end of str are used; returns s Replaces a substring of s as before but with the first n2 characters in charArray; if n2 is too large, characters to the end of charArray are used; if n2 is omitted, all of charArray is used; returns s Same as the preceding, but for the substring of s consisting of the characters in the range [it1, it2); returns s s.replace(pos1, n1, n2, ch) Replaces a substring of s as before, but with n2 copies of ch s.replace(it1, it2, n2, ch)  Same as the preceding but for the substring of s consisting of the characters in the range [it1, it2); returns s s.replace(it1, it2, inpIt1, inpIt2) Same as the preceding, but replaces with copies of the characters in the range [inpIt1, inpIt2); returns s s.reserve(n)  Changes the storage allocation for s so that s.capacity() ≥ n, if n is omitted; return type is void s.resize(n, ch)  If n ≤ s.size(), truncates rightmost characters in s to make it of size n; otherwise, adds copies of character ch to end of s to increase its size to n, or adds a default character value (usually a blank) if ch is omitted; return type is void s.rfind(str, pos)  Returns the highest position ≤ pos such that the next str.size() characters of s match those in str; returns npos if there is no such position; npos is the default value for pos s.rfind(ch, pos)  Searches s as in the preceding method, but for ch s.rfind(charArray, pos)  Searches s as in the preceding method, but for the characters in charArray K11207.indb 711 6/15/12 10:09 AM 712    ◾    Appendix D: Other C++ Features s.rfind(charArray, pos, n)  Searches s as in the preceding method, but for the first n characters in charArray; the value pos must be given s.size() Returns the length (of type size_type) of s s.substr(pos, n)  Returns a copy of the substring consisting of n characters from s, beginning at position pos (default value 0); if n is too large or is omitted, characters are copied only until the end of s is reached s.swap(str)  Swaps the contents of s and str; return type is void swap(str1, str2) Swaps the contents of str1 and str2; return type is void The list Class Template The list class template is provided in the Standard Template Library The following is a list of the operations defined on list objects; n is of type size_type; l, l1, and l2 are of type list; val, val1, and val2 are of type T; ptr1 and ptr2 are pointers to values of type T; it1 and it2 are iterators; and inpIt1, and inpIt2 are input iterators Constructors: list l;  This declaration invokes the default constructor to construct l as an empty list list l(n);  This declaration initializes l to contain n default values of type T list l(n, val);  This declaration initializes l to contain n copies of val list l(ptr1, ptr2);  This declaration initializes s to contain the copies of all the T values in the range [ptr1, ptr2) list l(l1);  This declaration initializes l to contain a copy of l1 l = l1 Assigns a copy of l1 to l l1 == l2  Returns true if l1 and l2 contain the same ­values, and false otherwise K11207.indb 712 6/15/12 10:09 AM Appendix D: Other C++ Features    ◾    713 l1 < l2  Returns true if l1 is lexicographically less than l2—l1.size() is less than l2.size() and all the elements of l1 match the first elements of l2; or if val1 and val2 are the first elements of l1 and l2, respectively, that are different, val1 is less than val2—and it returns false otherwise l.assign(n, val)  Erases l and then inserts n copies of val (default T value if omitted) l.assign(inpIt1, inpIt2) Erases l and then inserts copies of the T values in the range [inpIt1, inpIt2) l.back() Returns a reference to the last element of l l.begin() Returns an iterator positioned at the first element of l l.empty()  Returns true if l contains no elements, false otherwise l.end() Returns an iterator positioned immediately after the last element of l l.erase(it)  Removes from l the element at the position specified by it; return type is void l.erase(it1, it2)  Removes from l the elements in the range [it1, it2); return type is void l.front() Returns a reference to the first element of l l.insert(it, val) Inserts a copy of val (default T value if omitted) into l at the position specified by it and returns an iterator positioned at this copy l.insert(it, n, val) Inserts n copies of val into l at the position specified by it; return type is void l.insert(it, inpIt1, inpIt2)  Inserts copies of the T values in the range [inpIt1, inpIt2) into l at the position specified by it; return type is void l.insert(ptr1, ptr2) Inserts copies of all the T values in the range [ptr1, ptr2) at the position specified by it; return type is void l.max_size()  Returns the maximum number (of type size_ type) of values that l can contain K11207.indb 713 6/15/12 10:09 AM 714    ◾    Appendix D: Other C++ Features l.merge(l1)  Merges the elements of l1 into l so that the resulting list is sorted; both l and l1 must have been already sorted (using l.size(), copies of val (default T value if omitted) are appended to l; if n < l.size(), the appropriate number of elements is removed from the end of l l.reverse()  Reverses the order of the elements of l; return type is void l.size()  Returns the number (of type size_type) of elements l contains l.sort()  Sorts the elements of l using

Ngày đăng: 10/10/2022, 21:21

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN