Vectors Holds a set of elements, like an array

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 8 Arrays.
1 Standard Containers: Lists Gordon College Resource:
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Data Structures Using C++ 2E
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
Arrays Chapter 8.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Lecture 7 : Intro. to STL (Standard Template Library)
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Chapter 7 Arrays Csc 125 Introduction to C++. Topics Arrays Hold Multiple Values Array Operations Arrays as function arguments Two-dimensional arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 7: Arrays.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 7: Arrays.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Chapter 8: Arrays. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
CS212: Object Oriented Analysis and Design
Alternate Version of Starting Out with C++, Third Edition
CS505 Data Structures and Algorithms
Chapter 7: Arrays.
Lecture 6 Arrays and Vectors
Chapter 7: Arrays.
CSCE 210 Data Structures and Algorithms
Chapter 8 Arrays, Strings and pointers
Dr. Bernard Chen Ph.D. University of Central Arkansas
Standard Template Library (STL)
7 Chapter Arrays.
Chapter 8: Arrays Starting Out with C++ Early Objects Eighth Edition
Array Lists Chapter 6 Section 6.1 to 6.3
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
Chapter 18: Linked Lists.
C++ STL Vector Container
Vectors the better arrays.
C++ STRINGS string is part of the Standard C++ Library
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 9 One-Dimensional Arrays
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Doubly Linked List Implementation
Recursive Linked List Operations
Standard Version of Starting Out with C++, 4th Edition
Chapter 17: Linked Lists.
The Standard Template Library
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Lecture 8 : Intro. to STL (Standard Template Library)
STL (Standard Template Library)
C++ Programming: chapter 10 – STL
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Doubly Linked List Implementation
Chapter 3 Lists, Stacks, and Queues
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Vectors the better arrays.
A dictionary lookup mechanism
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Presentation transcript:

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>

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

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

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

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

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

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

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

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

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

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