Presentation is loading. Please wait.

Presentation is loading. Please wait.

Vectors Holds a set of elements, like an array

Similar presentations


Presentation on theme: "Vectors Holds a set of elements, like an array"— Presentation transcript:

1 Vectors Holds a set of elements, like an array
Flexible number of elements - can grow and shrink No need to specify size when defined Automatically adds more space as needed Defined in the Standard Template Library (STL) Covered in a later chapter Must include vector header file to use vectors #include <vector>

2 Vectors Can hold values of any type
Type is specified when a vector is defined vector<int> scores; vector<double> volumes; Can use [] to access elements

3 Vectors - Selected Public Member Functions
(constructor) - construct vector (destructor) - Vector destructor operator= - assign content Iterators: begin - return iterator to beginning end - return iterator to end

4 Vectors - Selected Public Member Functions
Capacity: size_type size() - return size size_type max_size() const; - return maximum size void resize (size_type n, value_type val = value_type()); - change size size_type capacity() const; - return size of allocated storage capacity bool empty() const; - test whether vector is empty void reserve( size_type new_cap ) - request a change in capacity void shrink_to_fit() - Requests the removal of unused capacity

5 Vectors - Selected Member Functions
Element access: operator[] - access element at - access element at specified location, with bounds checking reference front(); - returns a reference to the first element reference back(); - returns a reference to the last element T* data(); - access data

6 Vectors - Selected Public Member Functions
Modifiers: assign - assign vector content push_back - add element at the end pop_back - delete last element insert - insert elements erase - erase elements swap - swap content clear - clear content emplace - construct and insert element emplace_back - construct and insert element at the end

7 Vectors - Basic Operators
= assignment replaces a vector's contents with the contents of another == an element by element comparison of two vectors

8 Defining Vectors Define a vector of integers (starts with 0 elements)
vector<int> scores; Define int vector with initial size 30 elements vector<int> scores(30); See pr8-22.cpp

9 Defining Vectors Define 20-element int vector and initialize all elements to 0 vector<int> scores(20, 0); Define int vector initialized to size and contents of vector finals vector<int> scores(finals); See pr8-22.cpp

10 Growing a Vector’s Size
Use push_back member function to add an element to a full array or to an array that had no defined size // Add a new element holding a 75 scores.push_back(75); Use size member function to determine number of elements currently in a vector howbig = scores.size(); See pr8-23.cpp and pr8-24.cpp

11 Removing Vector Elements
Use pop_back member function to remove last element from vector scores.pop_back(); To remove all contents of vector, use clear member function scores.clear(); To determine if vector is empty, use empty member function while (!scores.empty()) ... See pr8-25.cpp, pr8-26.cpp, and pr8-27.cpp


Download ppt "Vectors Holds a set of elements, like an array"

Similar presentations


Ads by Google