tailieunhanh - Absolute C++ (4th Edition) part 30

Absolute C++ (4th Edition) part 30. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | Vectors A Preview of the Standard Template Library 293 The vector definition is given in the library vector which places it in the std namespace. Thus a file that uses vectors would include the following or something similar include vector using namespace std Vectors Vectors are used very much like arrays but a vector does not have a fixed size. If it needs more capacity to store another element its capacity is automatically increased. Vectors are defined in the library vector which places them in the std namespace. Thus a file that uses vectors would include the following lines include vector using namespace std The vector class for a given Base_Type is written vector Base_Type . Two sample vector declarations are as follows vector int v default constructor producing an empty vector. vector AClass record 20 vector constructor uses the default constructor for AClass to initialize 20 elements. Elements are added to a vector using the member function push_back as illustrated below 42 Once an element position has received its first element either with push_back or with a constructor initialization that element position can then be accessed using square bracket notation just like an array element. Pitfall Using Square Brackets beyond the Vector Size If v is a vector and i is greater than or equal to then the element v i does not yet exits and needs to be created by using push_back to add elements up to and including position i. If you try to set v i for i greater than or equal to as in v i n then you may or may not get an error message but your program will undoubtedly misbehave at some point. 294 Constructors and Other Tools Tip Vector Assignment Is Well Behaved The assignment operator with vectors does an element-by-element assignment to the vector on the left-hand side of the assignment operator increasing capacity if needed and resetting the size of the vector on the left-hand side of the assignment operator . Thus provided the assignment

TỪ KHÓA LIÊN QUAN