Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.

Slides:



Advertisements
Similar presentations
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Advertisements

Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
CSE Lecture 3 – Algorithms I
CHAPTER 7 Queues.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
More on the STL vector list stack queue priority_queue.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Main Index Contents 11 Main Index Contents Week 4 – Stacks.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
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
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
C++ STL CSCI 3110.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 8 Collection.
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS342 Data Structures End-of-semester Review S2002.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.
Lecture 7 : Intro. to STL (Standard Template Library)
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
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.
C++ Review STL CONTAINERS.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
Object-Oriented Programming (OOP) Lecture No. 41
Cpt S 122 – Data Structures Abstract Data Types
Homework 4 questions???.
Standard Template Library (STL)
Chapter 4 Linked Lists
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Standard Template Library
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
The List Container and Iterators
Presentation transcript:

Main Index Contents 11 Main Index Contents Week 3 – The Vector Container

Main Index Contents 2 

Main Index Contents 33 Main Index Contents C++ Arrays An array is a fixed-size collection of values of the same data type. An array is a container that stores the n (size) elements in a contiguous block of memory. int arr[] = {1,2,3,4,5}; cout << arr[4]; int arr[] = {1,2,3,4,5}; cout << arr[4];

Main Index Contents 44 Main Index Contents Evaluating an Array as a Container The size of an array is fixed at the time of its declaration and cannot be changed during the runtime. – An array cannot report its size. A separate integer variable is required in order to keep track of its size. C++ arrays do not allow the assignment of one array to another. – The copying of an array requires the generation of a loop structure with the array size as an upper bound.

Main Index Contents Variable-sized array Use dynamically allocated memory to determine the size of the array at run time Programmers need to handle array resize manually STL makes this easier STL 5 int h, *b; cin >> h; b = new int[h];... delete [] b;

Main Index Contents 6 1.Allow for dynamic resizing 2.Have a way to store the size internally 3.Allow for assignment of one object to another 6 Main Index Contents Vectors A container is a class that stores a collection of datacontainer It has operations that allow a programmer to insert, erase, and update elements in the collection A container is a class that stores a collection of datacontainer It has operations that allow a programmer to insert, erase, and update elements in the collection

Main Index Contents 77 Main Index Contents CLASS vector Constructors

Main Index Contents 8 Declaring Vector Objects // vector of size 5 containing the integer // value 0 vector intVector(5); // After assigning value for each element: // vector of size 10; each element // contains the empty string vector strVector(10);

Main Index Contents 99 Main Index Contents CLASS vector Operations T& back(); Return the value of the item at the rear of the vector. Precondition:The vector must contain at least one element. bool empty() const; Return true if the vector is empty and false otherwise.

Main Index Contents 10 Main Index Contents CLASS vector Operations T& operator[] (int i); Allow the vector element at index i to be retrieved or modified. Precondition:The index, i, must be in the range 0  i < n, where n is the number of elements in the vector.

Main Index Contents 11 Main Index Contents CLASS vector Operations void push_back(const T& value); Add a value at the rear of the vector. Postcondition:The vector has a new element at the rear and its size increases by 1. void pop_back(); Remove the item at the rear of the vector. Precondition: The vector is not empty. Postcondition:The vector has a new element at the rear or is empty.

Main Index Contents 12 Adding and Removing Vector Elements

Main Index Contents 13 Main Index Contents CLASS vector Operations void resize((int n, const T& fill = T()); Modify the size of the vector. If the size is increased, the value fill is added to the elements on the tail of the vector. If the size is decreased, the original values at the front are retained. Postcondition: The vector has size n. int size() const; Return the number of elements in the vector.

Main Index Contents 14 Resizing a Vector int arr[5] = {7, 4, 9, 3, 1}; vector v(arr,arr+5); // v initially has 5 integers v.resize(10); // list size is doubled v.resize(4); // list is contracted. data // is lost

Main Index Contents 15 Output with Vectors // number of elements in list is v.size() template void writeVector(const vector & v) { // capture the size of the vector in n int i, n = v.size(); for(i = 0; i < n; i++) cout << v[i] << " "; cout << endl; }

Main Index Contents C++ interview questions on “Vector” 16 What do vectors represent? a) Static arrays b) Dynamic arrays c) Stack d) Queue Answer: b Explanation: Vectors are sequence containers representing arrays that can change in size. More questions here

Main Index Contents 17

Main Index Contents 18

Main Index Contents 19

Main Index Contents 20

Main Index Contents 21

Main Index Contents 22

Main Index Contents 23

Main Index Contents 24

Main Index Contents 25

Main Index Contents 26

Main Index Contents 27

Main Index Contents Insertion Sort Animation Pseudo-code 28

Main Index Contents Insertion Sort "swap" operation in-place as temp ← A[j]; A[j] ← A[j-1]; A[j-1] ← temp; 29

Main Index Contents 30 Main Index Contents Insertion Sort Implementation insertionSort(): // sort a vector of type T using insertion // sort template void insertionSort(vector & v) { int i, j, n = v.size(); T temp; // place v[i] into the sublist v[0]... // v[i-1], 1 <= i < n, so it is in the // correct position

Main Index Contents 31 Main Index Contents Insertion Sort Implementation for (i = 1; i < n; i++) { // index j scans down list from v[i] // looking for correct position to // locate target. assigns it to v[j] j = i; temp = v[i]; // locate insertion point by scanning // downward as long as temp < v[j-1] // and we have not encountered the // beginning of the list

Main Index Contents 32 Main Index Contents Insertion Sort Implementation while (j > 0 && temp < v[j-1]) { // shift elements up list to make // room for insertion v[j] = v[j-1]; j--; } // the location is found; insert temp v[j] = temp; }

Main Index Contents Time Complexity (Big O) 33

Main Index Contents Worst case Best case: input is an array that is already sorted. Linear running time (i.e., Θ(n)).Θ Worst case: input is an array sorted in reverse order. Quadratic running time (i.e., O(n 2 )). 34

Main Index Contents 35 Underlying storage structure Container classes The storage structure should be selected so that the container operations can be implemented efficiently

Main Index Contents 36 Main Index Contents Container Types Sequence Containers Adapter Containers Associative Containers VectorStackSet, Multiset DequeQueue Map, Mutltimap ListPriority Queue

Main Index Contents Details on STL Containers 37

Main Index Contents 38 Main Index Contents Sequence Containers A sequence container stores data by position in linear order 1st element, 2nd element, and so forth.

Main Index Contents 39 Main Index Contents The List Container Inserting into a List Container

Main Index Contents 40 Adapter Classes An adapter contains a sequence container as its underlying storage structure. The programmer interface for an adapter provides only a restricted set of operations from the underlying storage structure.

Main Index Contents 41 Main Index Contents Stack Containers (LIFO) A stack allows access at only one end of the sequence, called the top.

Main Index Contents 42 Main Index Contents Queue Containers (FIFO) A queue is a container that allows access only at the front and rear of the sequence.

Main Index Contents 43 Main Index Contents Associative Containers Associative containers store elements by key. – Ex: name, social security number, or part number. A program accesses an element in an associative container by its key, which may bear no relationship to the location of the element in the container.

Main Index Contents 44 Main Index Contents Set Containers A set is a collection of unique values, called keys or set members.

Main Index Contents 45 Main Index Contents Map Containers A map is a storage structure that implements a key- value relationship.