Vectors in C++ Programming

chapter 8 vectors n.w
1 / 13
Embed
Share

"Learn how vectors work in C++ programming, allowing for dynamic size adjustments, flexible element storage, and easy access. Master vector definitions, initialization, basic operators, and size manipulation with practical examples and C++ 11 features."

  • C++
  • Programming
  • Vectors
  • Data Structures
  • C++ 11

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Chapter 8 Vectors

  2. 8.12 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> Must use namespace std

  3. Vectors Can hold values of any type Type is specified when a vector is defined vector<int> scores; vector<double> volumes; You can specify initial size if desired vector<int> scores(24); Can use [] to access elements

  4. 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); 8-4

  5. 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); 8-5

  6. C++ 11 Features for Vectors C++ 11 supports vector definitions that use an initialization list vector<int> scores {88, 67, 79, 84}; Note: no = operator between the vector name and the initialization list A range-based for loop can be used to access the elements of a vector for (int test : scores) cout << test << " "; 8-6

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

  8. Growing a Vectors 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();

  9. 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()) ...

  10. Vectors - Selected Public Member Functions at(position) - returns value located at the position size() - returns number of elements in the vector max_size() const - return maximum size resize (n) - change size resize (n, value) - change size and initialize each of the additional elements to value capacity() - return size of allocated storage capacity empty() - test whether vector is empty (returns true if empty) clear() - clears a vector of all its elements reverse() - reverses order of the elements swap(vector 2) - swaps the contents of the vector with the contents of vector2 reserve(new_cap) - request a change in capacity shrink_to_fit() - requests the removal of unused capacity

  11. Vectors Iterators What is an iterator? An object that points to an element inside the vector. Can be used to move through the contents of the vector. Have similar functionality as that of pointers. Iterators with vectors are not limited to moving sequentially; they can randomly access any element inside the vector.

  12. Vector Iterators begin - returns an iterator to beginning end - returns and iterator to just beyond the last element // without an iterator vector<int> v = {1, 2, 3}; for (int i = 0; i < 3; i++) cout << v[j] << ; // if have an iterator, don t need to know or // keep track of the number of elements vector<int>::iterator iter; for (iter = v.begin(); iter != v.end(); iter++) cout << *iter << ;

  13. Vector Iterators // another example vector<int> v = {1, 2, 3}; vector<int>::iterator iter; for (iter = v.begin(); iter != v.end(); iter++) { if (iter == v.begin()) iter = v.insert(iter, 5); // insert 5 at beginning } // v now contains 5 1 2 3 for (iter = v.begin(); iter != v.end(); iter++) { if (iter == v.begin() + 1) iter = v.erase(iter); // iter now points to element // after the deleted element } // v now contains 5 2 3

More Related Content