Standard Containers: Vectors

Slides:



Advertisements
Similar presentations
Lists: An internal look
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Data Structures Using C++ 2E
The Standard Template Library provides the framework for building generic, highly reusable algorithms and data structures A reference implementation of.
More on the STL vector list stack queue priority_queue.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementations:
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
Templates and the STL.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
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
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
C++ STL CSCI 3110.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementations:
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
1 Read § Templates Templates allow functions and classes to be parameterized so that the ______________ data being operated upon (or stored)
1 Read § Templates Templates allow functions and classes to be _________________ so that the Templates provide a means to ________________—
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
Templates&STL. Computer Programming II 2 Introduction They perform appropriate operations depending on the data type of the parameters passed to them.
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.
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
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.
1 The Standard Template Library Drozdek Section 3.7.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Standard Template Library a collection of useful tools.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
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
Standard Template Library
C++ Templates.
CSCE 210 Data Structures and Algorithms
Standard Template Library
Standard Template Library (STL)
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
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
ADT Implementations: Templates and Standard Containers
Templates and Standard Containers
Vectors.
Searching: Hash Tables
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
Presentation transcript:

Standard Containers: Vectors Adapted from Nyhoff, ADTs, Data Structures and Problem Solving with C++

STL (Standard Template Library) A library of class and function templates Components: Containers: Generic "off-the-shelf" class templates for storing collections of data Algorithms: Generic "off-the-shelf" function templates for operating on containers Iterators: Generalized "smart" pointers provide a generic way to access container elements Stopped 2/24/2006

Standard Template Library Example of a specific container class iterator algorithm Algorithm must be able to operator on any container – therefore the containers provide iterators for the algorithms to use. (iterators – interface that is needed by STL algorithms to operate on STL containers)

STL's 10 Containers Kind of Container STL Containers Sequential: deque, list, vector Associative: map, multimap, multiset, set Adapters: priority_queue, queue, stack Container Adapters – adapt STL containers to fit special needs.

The vector Container A type-independent pattern for an array class capacity can expand self contained Declaration template <typename T> class vector { . . . } ;

The vector Container Constructors vector<T> v, // empty vector v1(100), // 100 elements of type T v2(100, val), // 100 copies of val v3(fptr,lptr); // contains copies of // elements in memory // locations fptr to lptr Vector<double>dubvector(a,a+4); where a is an array…. int intArray[5] = {9,2,7,3,12}; Int arrSize = sizeof(intArray)/sizeof(int); vector<int> intVector(intArray, intArray+arrSize);

vector Operations Information about a vector's contents v.size() v.empty() v.capacity() v.reserve(n) Adding, removing, accessing elements v.push_back(value) v.pop_back() v.front() v.back() See p. 478 Capacity – return the no of places vector has Reserve – grow the vector to N

vector Operations Assignment v1 = v2 Swapping v1.swap(v2) Relational operators == implies element by element equality less than < behaves like string comparison

Increasing Capacity of a Vector When vector v becomes full capacity increased automatically when item added Algorithm to increase capacity of vector<T> Allocate new array to store vector's elements (how big) use T copy constructor to copy existing elements to new array (therefore your class must have copy constructor) Store item being added in new array Destroy old array in vector<T> Make new array the vector<T>'s storage array Expansion by addition is possible but costly What happens when the vector must grow…

Increasing Capacity of a Vector Allocate new array Capacity doubles when more space needed Elements copied to new array

Increasing Capacity of a Vector Item being added now stored Destroy old array Make new array the vector's storage area

Iterators A subscript operator is provided BUT … this is not a generic way to access container elements STL provides objects called iterators can point at an element can access the value within that element can move from one element to another They are independent of any particular container … thus a generic mechanism

Iterators Given a vector which has had values placed in the first 4 locations: v.begin() will return the iterator value for the first slot, v.end() for the next empty slot vector<int> v 9 4 15 3 v.begin() v.end()

Iterators Each STL container declares an iterator type can be used to define iterator objects To declare an iterator object the identifier iterator must be preceded by name of container scope operator :: Example: vector<int>::iterator vecIter = v.begin()

Iterators Basic operators that can be applied to iterators: increment operator ++ decrement operator -- dereferencing operator * Assignment = Addition, subtraction +, -, +=, -= vecIter + n returns iterator positioned n elements away Subscript operator [ ] vecIter[n] returns reference to nth element from current position

Iterators Contrast use of subscript vs. use of iterator for (vector<double>::iterator it = v.begin(); it != v.end(); it++) out << *it << " "; ostream & operator<<(ostream & out, const vector<double> & v) { for (int i = 0; i < v.size(); i++) out << v[i] << " "; return out; }

Iterator Functions Operators: ++, --, *, =. ==, !=, +, -, [ ] Vector Functions: v.begin(), v.end(), v.rbegin(), v.rend(), v.insert(iter, value), v.insert(iter,n,value), v.erase(iter), v.erase(iter1,iter2) Note the capability of the last two groupings Possible to insert, erase elements of a vector anywhere in the vector Must use iterators to do this Note also these operations are as inefficient as for arrays due to the shifting required v.insert(iter, n, value) v.erase(iter ) v.erase(iter1, iter2)

Contrast Vectors and Arrays Capacity can increase A self contained object Is a class template Has function members to do tasks Fixed size, cannot be changed during execution (unless using dynamic alloc) Cannot "operate" on itself Must "re-invent the wheel" for most actions