Vectors the better arrays.

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Lecture 20 “Standard” Template Library. What is the Standard Template Library? A collection of C++ classes (templates) containers vectors lists stacks.
Rossella Lau Lecture 2, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Data Structures Using C++ 2E
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Vectors CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact) l they serve the same purpose as arrays but using.
Standard Template Library a collection of useful tools.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Motivation for Generic Programming in C++
Generic Programming in C
Pointers and Dynamic Arrays
C++ Programming:. Program Design Including
Concepts of Programming Languages
Pointers and Linked Lists
Pointers and Linked Lists
Standard Template Library
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Introduction to Linked Lists
Lecture 7-2 : STL Iterators
Exceptions, Templates, and the Standard Template Library (STL)
Vectors Holds a set of elements, like an array
CSCE 210 Data Structures and Algorithms
C++ Standard Library.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Pointers.
STL Common tools for C++.
CS Computer Science IB: Object Oriented Programming
Pointers Revisited What is variable address, name, value?
Starting Out with C++ Early Objects Eighth Edition
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Introduction to Linked Lists
Dynamic Memory Allocation
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Lecture 7-2 : STL Iterators
ADT Implementations: Templates and Standard Containers
7 Arrays.
Vectors the better arrays.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 9 One-Dimensional Arrays
Constructors and Other Tools
STL Iterators Separating Container from Data Access.
The Standard Template Library
Objectives You should be able to describe: Addresses and Pointers
Lists - I The List ADT.
Lists - I The List ADT.
Standard Template Library (STL)
Lecture 8-2 : STL Iterators and Algorithms
C++ Templates L03 - Iterator 10 – Iterator.
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
Lesson 25 Miscellaneous Topics
Collections Intro What is the STL? Templates, collections, & iterators
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Presentation transcript:

Vectors the better arrays

Why Vectors vectors are implemented as a class (a template to be exact) they serve the same purpose as arrays but using object oriented mechanisms (such as operator overloading) making them more powerful vectors can grow and shrink as needed be compared and assigned as a whole be passed by reference/value/returned vector size is always available still more … part of the so called Standard Template Library (STL) once you have learned vectors, you do not want to use (raw) arrays anymore.

Declaration include <vector> header file using std::vector; to declare vector<type_parameter> vectorName; where type_parameter – type/class of vector elements corresponds to base type of the array examples vector<int> items; // declares vector with no elements vector<double> nums(5); // declares vector with 5 elements vector<myclass> things(5); // declares a vector of 5 objects of myclass

Typical Array Operations indexing: items[3] still cannot refer past vector size: items[6]++; // range error checking size: items.size() careful with this one: returns value of type size_t example: for(size_t i=0; i < items.size(); ++i) cout << items[i];

Atypical Array Operations adding element: items.push_back(55); removing element: items.pop_back(); checking if empty: items.empty(); accessing first/last element: items.front(); items.back(); changing size: items.resize(newSize); emptying: items.erase(); example: int num=0; cin >> num; while(num !=0){ items.push_back(num); } while(!items.empty()){ cout << items.back(); items.pop_back();

With Functions, Other Aggregate Operations can pass by value/reference, return void myfunc(vector<int>); void myfunc(vector<int>&); vector<int> myfunc(void); can assign vector<int> v; v = items; can swap: items.swap(v); can compare: if(v == items) … 6

With Objects class intData{ // elementary class definition public: void set(int n){data=n;}; // simple setter int get(void) const{return data;}; // simple accessor private: int data; }; … vector<intData> v(5); // declaring a vector of objects for(int i=0; i<5; i++){ // using vector as array int num; cin >> num; v[i].set(num); // invoking a method on vector element }

Iterators vector<type_parameter>::iterator iteratorName; iterator is a generalization of pointer not a pointer but usually implemented using pointers further enhances the power of vector declaring iterator vector<type_parameter>::iterator iteratorName; where type_parameter – type/class of elements example vector<int>::iterator p;

Iterators Operations operations ++ (pre- and postfix) and -- to advance to the next (previous) data item +5 -3 advance certain number of positions forward/backward == and != operators to test whether two iterators point to the same data item < > to compare if two elements are before and after in the vector dereferencing (*) provides data item access functions v.begin() returns an iterator pointing to the first element of container v v.end() returns an iterator pointing past the last element of container v useful to compare for end of iteration inserting: v.insert(ip, 22); // inserts one element at // iterator ip position what does this do? v.insert(v.end()-2, 55); erasing: v.erase(ip); // erases (deletes) one element at // iterator ip position sorting elements: sort(items.begin(),items.end()); need algorithm header caution: after altering a vector (inserting/erasing/sorting), iterators are invalidated and cannot be used unless given a value again (reassigned).

Iterator Usage Example vector <int> v; // declare vector … for ( // declare and initialize iterator vector<int>::iterator p = v.begin(); p < v.end(); // check if end is not reached ++p // move iterator to next element ){ cout << *p; // manipulate element in loop body }

Typedef variable declarations can get long and obscure program meaning typedef – allows to give names to already defined types and classes note: no new types declared benefits: shortens the code, gives more intuitive names, adds portability – can change defined type later if needed syntax: typedef existingType newType; example typedef vector<int> myVector; typedef myvector::iterator myVectorIterator; myVector v; for (myVectorIterator p=v.begin(); p < v.end(); p++){ cout << *p; do not overdo – typedefs hide original types: give new types intuitive names, do not use for short/understandable types

Questions on Vectors why use vectors instead of arrays? what header file is needed to use vectors? how is vector declared? how to declare a vector of five integers? how can one determine the size of a vector? What is the size of this vector vector<int> myvector(4); how does one add/remove elements from a vector? how does one get the first/last element from a vector? can vectors be assigned to each other? What happens to their size? contents? compared with each other? when are they equal? can vectors be passed by value? reference? returned? can vectors contain objects? what is an iterator? how is it used? what is typedef? How is it used? What are begin() and end()?