Lecture 7 : Intro. to STL (Standard Template Library)

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
More on the STL vector list stack queue priority_queue.
Stacked Deque Gordon College Stacked Decks - get it?
1 Standard Containers: Lists Gordon College Resource:
1 Queues and Priority Queues Chapter 8. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank,
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
Basic C++ Sequential Container Features
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.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Standard Template Library There are 3 key components in the STL –Containers –Iterators –Algorithms Promote reuse More debugged May be more efficient.
Data Structures Using C++ 2E
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
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.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
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,
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.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
The Standard Template Library Container Classes Version 1.0.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
C++ Review STL CONTAINERS.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Vectors CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
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
Standard Template Library
Standard Template Library (STL)
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Object Oriented Programming COP3330 / CGS5409
priority_queue<T>
Recursive Linked List Operations
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Lecture 8 : Intro. to STL (Standard Template Library)
STL (Standard Template Library)
Standard Template Library
C++ Programming: chapter 10 – STL
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
Presentation transcript:

Lecture 7 : Intro. to STL (Standard Template Library)

STL a set of C++ template classes to provide common programming data structures and functions Data structures (container class) expandable arrays (vector) doubly linked lists (list) (priority) queue, stack, set, map, … Functions (algorithms) on the data structures sort(), search(), merge(), min(), max(), swap(), … Set operations (union, difference, intersection, …) included in C++ standard library

3 categories of STL Container class A holder object that stores a collection of other objects with any (user defined or built-in) data types Iterator similar to pointer (point to element of container) Implemented for each type of container Elements of containers can be accessed through iterators Algorithm Perform operations(e.g. sort, search, …) on STL objects

Container Classes Sequences : sequential collection vector, list, deque (double ended queue) Associative Container : set (duplicate data are not allowed) : Collection of ordered data in a balanced BST. Fast search map : associate key-value pair held in balanced BST Ex) person[“hongkildong”] = person_object; hash_set, hash_set : uses hash (fast but no order) Container Adapters Implemented on top of another container stack, queue, priority queue

Iterators Iterators provide common interface to step through the elements of any arbitrary type STL containers. “Algorithms” can have ways to access any types of “Containers” through “Iterators”. There are iterators for going sequentially forward and/or backward as well as random access iterators. Iterators are used by the STL algorithms. Iterator syntax uses the ++, --, and * operators which are familiar to users of pointers.

Iterators Containers Algorithms Iterators

algorithms The header defines a collection of functions (such as searching and sorting) especially designed to be used on ranges of container elements. Access container elements thru iterators therefore will work on any container which provides an interface by iterators

Let’s look at examples of Sequence Containers (vector, list, deque, …)

Sequence Containers: Access, Add, Remove Element access for all: front() back() Element access for vector and deque: [ ]: Subscript operator, index not checked Add/remove elements for all push_back(): Append element. pop_back(): Remove last element Add/remove elements for list and deque: push_front(): Insert element at the front. pop_front(): Remove first element.

Sequence Containers: Other Operations Miscellaneous operations for all: size() : Returns the number of elements. empty() : Returns true if the sequence is empty. resize(int i) : Change size of the sequence. Comparison operators ==, !=, < etc. are also defined. i.e., you can compare if two containers are equal. “List” operations are fast for list, but also available for vector and deque: insert(p, x) : Insert an element at a given position. erase(p) : Remove an element. clear() : Erase all elements.

vector Dynamic array of variables, struct or objects. elements are stored in contiguous storage locations Able to resize itself automatically when inserting or erasing a vector element vector is good at Accessing individual elements by their position index, O(1). (random access) Iterating over the elements in any order (linear time). Add and remove elements from its end What about inserting/erasing an element in the middle?

Vector declaration Header file include #include Declare : vector variable_name; Ex) vector vec_int; Dynamic allocation Ex) vector * vec_intp = new vector ;

vector member functions

vector example1 // constructing vectors #include using namespace std; int main () { unsigned int i; // constructors used in the same order as described above: vector first; // empty vector of ints vector x(10); // vector with size=10; vector second (4,100); // four ints (size=4) with value 100 vector third (second.begin(),second.end()); // iterating through second vector fourth (third); // a copy of third // the iterator constructor can also be used to construct from arrays: int myints[] = {16,2,77,29}; vector fifth (myints, myints + sizeof(myints) / sizeof(int) ); cout << "The contents of fifth are:"; for (i=0; i < fifth.size(); i++) cout << " " << fifth[i]; x[0]=7; x[1]=x[0]+5; return 0; } The contents of fifth are:

vector example2 // vector::begin #include using namespace std; int main () { vector myvector; for (int i=1; i<=5; i++) myvector.push_back(i); vector ::iterator it; cout << "myvector contains:"; for ( it=myvector.begin() ; it < myvector.end(); it++ ) cout << " " << *it; cout << endl; return 0; } myvector contains:

vector example 3 // vector::pop_back #include using namespace std; int main () { vector myvector; int sum (0); myvector.push_back (100); myvector.push_back (200); myvector.push_back (300); while (!myvector.empty()) { sum+=myvector.back(); myvector.pop_back(); } cout << "The elements of myvector summed " << sum << endl; return 0; } The elements of myvector summed 600

vector example4 // comparing size, capacity and max_size #include using namespace std; int main () { vector myvector; // set some content in the vector: for (int i=0; i<100; i++) myvector.push_back(i); cout << "size: " << (int) myvector.size() << "\n"; cout << "capacity: " << (int) myvector.capacity() << "\n"; cout << "max_size: " << (int) myvector.max_size() << "\n"; return 0; } size: 100 capacity: 141 max_size:

vector example5 // erasing from vector #include using namespace std; int main () { unsigned int i; vector myvector; // set some values (from 1 to 10) for (i=1; i<=10; i++) myvector.push_back(i); // erase the 6th element myvector.erase (myvector.begin()+5); // erase the first 3 elements: myvector.erase (myvector.begin(),myvector.begin()+3); cout << "myvector contains:"; for (i=0; i<myvector.size(); i++) cout << " " << myvector[i]; cout << endl; return 0; } myvector contains:

stack (LIFO) Member functions constuctor empty size top push pop

stack example // stack::push/pop #include using namespace std; int main () { stack mystack; for (int i=0; i<5; ++i) mystack.push(i); cout << "Popping out elements..."; while (!mystack.empty()) { cout << " " << mystack.top(); mystack.pop(); } cout << endl; return 0; } Popping out elements

list Doubly-linked list Fast access to front and back only Add and remove elements at any position

list member functions

list example 1 push_front, push_back pop_front, pop_back // list::push_front #include using namespace std; int main () { list mylist (2,100); // two ints with a value of 100 mylist.push_front (200); mylist.push_front (300); cout << "mylist contains:"; for (list ::iterator it=mylist.begin(); it!=mylist.end(); ++it) cout << " " << *it; cout << endl; return 0; }

list example 2 insert // inserting into a list #include using namespace std; int main () { list mylist; list ::iterator it; // set some initial values: for (int i=1; i<=5; i++) mylist.push_back(i); // it = mylist.begin(); ++it; // it points now to number 2 mylist.insert (it,10); // // "it" still points to number 2 mylist.insert (it,2,20); // it; // it points now to the second 20 vector myvector (2,30); mylist.insert (it,myvector.begin(),myvector.end()); // cout << "mylist contains:"; for (it=mylist.begin(); it!=mylist.end(); it++) cout << " " << *it; cout << endl; return 0; } mylist contains:

Standard Sequence Containers They differ in how quickly different access operations can be performed. n is the number of elements currently in the container.