Collections Intro What is the STL? Templates, collections, & iterators

Slides:



Advertisements
Similar presentations
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Advertisements

C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Standard Containers: Vectors
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.
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 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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Generic Programming Using the C++ Standard Template Library.
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Templates&STL. Computer Programming II 2 Introduction They perform appropriate operations depending on the data type of the parameters passed to them.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Lecture 7 : Intro. to STL (Standard Template Library)
A gentle introduction to the standard template library (STL) Some references:
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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.
Vectors. Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically.
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.
Motivation for Generic Programming in C++
Standard Template Library
C++ Standard Template Library
CS212: Object Oriented Analysis and Design
Concepts of Programming Languages
Regarding homework 9 Many low grades
Standard Template Library
Motivation and Overview
CSCE 210 Data Structures and Algorithms
C++ Standard Library.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Standard Template Library (STL)
Chapter 4 Linked Lists.
STL Common tools for C++.
Starting Out with C++ Early Objects Eighth Edition
This pointer, Dynamic memory allocation, Constructors and Destructor
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
ADT Implementations: Templates and Standard Containers
Vectors the better arrays.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 9 One-Dimensional Arrays
Level 1 STL – Linear DS fb.com/acmicpcfcicu. Static Arrays Creating 1D, 2D, ND Static Arrays. Accessing Time. Traversing a static array with N dimensions.
The Standard Template Library
Lists - I The List ADT.
Lists - I The List ADT.
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library (STL)
COP 3330 Object-oriented Programming in C++
Standard Template Library
STL List.
Collections Intro What is the STL? Templates, collections, & iterators
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Standard Template Library
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Vectors the better arrays.
An Introduction to Programming though C++
STL List.
Presentation transcript:

Collections Intro What is the STL? Templates, collections, & iterators Focus on vector, for now

Data Structures Collection or data structure: an object that stores data elements: objects that are stored collections can be ordered or unordered some collections allow duplicate elements Common operations: insert, size, isEmpty... Good to know how to build collections (implementation) Often use an available collection rather than implementing it yourself (and learn to use it effectively) You use an iphone proficiently, but you don't necessarily understand internal function.

Type Parameters (templates) Collection<type> name; specify type of elements in triangle brackets Called a type parameter – you get a parameterized class, or template class type can be any data type: int, char, string, user-defined type... Example: vector<int> v; If you pass an object in C++ (without the &) it's passed by value – C++ makes a complete copy of its contents, rather than sharing its location. Objects are passed by value by default, not be reference. Making copies of objects is usually slow – you probably don't want this. So: int computeSum(Something<int>& s) { ... Have to pass by reference if you want function tmodify object also.

STL: Standard Template Library STL: library of collections and algorithms for C++ Many use templates container classes (set, list, stack, vector, queue...) algorithms iterators STL

VECTOR

Won't Arrays Do? Arrays: fixed size – not easily resized array doesn't "know" its own size array index out of bounds errors not caught don't support useful operations like: inserting/deleting elements at front/middle/back of array reversing order of elements searching or sorting preventing duplicate elements from being added Arrays don't have many features. Not very powerful. Go right ahead and exceed bound of array and access random garbage in memory

Vector vector (aka list): collection of elements with 0-based indexing add elements anywhere in vector automatic resizing as needed vector has a size (current # of elements) template class – create vectors of different types No need to allocate/delete memory for vector Header: #include<vector> Recall: all of standard library in namespace std vector<int> myVector; // vector of ints vector<string> strVec; // vector of strings Don't have to worry about resizing yourself.

Example Vector member function at(): C++ Array Example: int size = 10; int myArr[10]; int *arrPtr = new int[size]; for(int i = 0; i < 10; i++){ myArr[i] = i; arrPtr[i] = i; } delete [] arrPtr; Vector member function at(): similar to indexing with [], but checks index validity Resizing is handled for you when necessary C++ Vector Example: #include <vector> using namespace std; ... int size = 10; // 10 ints in array, init to 0 vector<int> myArr(size); for(int i = 0; i < size; i++) myArr[i] = i; // Don't need to delete! Vectors have advantages of static and dynamically allocated array – it takes a non constant size like the dyn allocated array and ia automatically deleted like the static one. Vector uses [] to allow indexing like arrays.

STL vector vector<int> v; // empty vector Member functions: push_back(value): insert element at the end of the vector v.push_back(3); // 3 added at end of v v.push_back(5); // 5 added at end of v at(index): return element at specified index int x = v.at(0); // 3 size(): returns number of elements in vector int numElts = v.size(); // 2 front(): returns reference to first element of vector int& num = v.front(); // 3 back(): returns reference to last element of vector

STL iterator iterator: object that iterates over a data structure pointer-like object that can be incremented (++), dereferenced (*), compared to another iterator (!=) stores current position within data structure used to traverse over the elements in a collection iterator generated by STL member functions like begin() and end() begin(): returns iterator pointing to 1st element of ordered collection (e.g., vector) end(): returns iterator which is past the end of the collection

Iterator A collection has a begin and end iterator to front and back Advance one element: iter++ Go back one element: iter-- Access element the iterator is next to: *iter vector<int> v; ... // loop over vector elements and print them for(vector<int>::iterator it = v.begin(); it != v.end(); it++) { cout << *it << endl; }

Iterator Example // for-each loop – implicit iterator (C++11) vector<int> v; ... for(int k : v) { cout << k << endl; }

Iterator Example Some container member functions take an iterator argument to indicate position Example for vector: insert, erase vector<int> v; ... for(vector<int>::iterator it = v.end(); it != v.begin(); it--) { if(*it % 2 == 0) { it = v.erase(it); // delete element at this position }

STL Algorithms Many algorithms Example: sorting vectors #include<algorithm> vector<string> vec; vec.push_back("hello"); vec.push_back("world"); vec.push_back("bye"); sort(v.begin(), v.end()); http://www.cplusplus.com/reference/algorithm/