Containers & Iterators

Slides:



Advertisements
Similar presentations
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Advertisements

1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE Lecture 15 – Maps.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
1 Associative Containers Gordon College Prof. Brinton.
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.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
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
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
1 Joe Meehean.  List of names  Set of names  Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: Bill:
Lecture 7 : Intro. to STL (Standard Template Library)
A gentle introduction to the standard template library (STL) Some references:
1 STL Containers Copyright Kip Irvine, All rights reserved. Only students enrolled in a class at Florida International University may copy or print.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
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.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
CS212: Object Oriented Analysis and Design
Concepts of Programming Languages
Overview 18.1 Iterators 18.2 Containers 18.3 Generic Algorithms.
Vectors Holds a set of elements, like an array
C++ Standard Library.
Standard Template Library
Standard Template Library (STL)
STL Common tools for C++.
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Starting Out with C++ Early Objects Eighth Edition
Collections Intro What is the STL? Templates, collections, & iterators
What remains Topics Assignments Final exam
Basic Data Structures.
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
Associative Structures
A Sorted, Unique Key Container
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Introduction to C++ STL
priority_queue<T>
Chapter 9 One-Dimensional Arrays
Elements are always copied when they are put into a container
Collections Framework
Lists - I The List ADT.
Lists - I The List ADT.
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library (STL)
STL (Standard Template Library)
C++ Programming: chapter 10 – STL
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Collections Intro What is the STL? Templates, collections, & iterators
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
Recitation Outline Hash tables in C++ STL Examples Recursive example
The List Container and Iterators
Chapter 9 – Sets and Maps 9.1 Associative Container
A dictionary lookup mechanism
Presentation transcript:

Containers & Iterators

STL Containers Container: object that holds other objects Sequence containers in STL: vector: dynamic array deque: double-ended queue list: linear list Associative containers: permit efficient retrieval of values based on keys – elements referenced by key, not position Unique keys – no two elements can have equivalent keys map: key-value pairs, like a dictionary set: no duplicate elements

Iterators Objects that act like pointers increment, decrement, dereference Use to cycle through container's elements Depending on type of iterator, may permit: moving forward and/or backward through elements retrieve elements modify elements Declare using iterator type defined by container Example: vector<int>::iterator iter = ...;

vector Review vector<int> v; // empty vector vector<int> vec(1000); // 1000 elements, all initialized to 0 cout << vec.size() << endl; // 1000 vec.clear(); // remove all elements cout << vec.size() << endl; // 0

vector resize() for vectors: specify size only (elements initialized to 0) Specify size and value for new elements new elements added at end of vector if vector shrinks, elements removed from end vector<int> v; // empty v.resize(10, 0); // Add 10 new elements to end, set all of them to 0 v.resize(5); // remove 5 elements from end v.resize(8, 1); // add three new elements to end, all initialized to 1 pop_back(): remove element from end v.pop_back();

vector iteration begin() iterator points to 1st element of vector end() iterator points one position past end of vector for(vector<int>::iterator it = myVector.begin(); it != myVector.end(); it++) cout << *itr << endl; begin() end() 120 42 3 -9 16

vector front(): return reference to first element back(): return reference to last element insert(): add element at specified position vec.insert(vec.begin() + n, element); // Add element at index n vec.insert(vec.begin() + n; numCopies, element); // add multiple copies at index n pop_back(): delete last element vector<int> v(5, 3); // 5 elements with value 3 v.push_back(7); int num = v.back(); // 7 v.pop_back(); // remove 7 from end v.insert(v.begin(), 9); // add 9 at beginning of vector

vector begin(): returns iterator pointing to first element end(): returns iterator pointing past the end of the vector doesn't point to an element in vector (don't dereference) erase(): remove a single element or range of elements from vector erase(iterator): iterator points to element to remove erase(iterator start, iterator end): remove elements in range [start, end) invalidates iterators pointing to or after removed element(s) returns iterator pointing at element following last element erased vec.erase(vec.begin() + 3); // remove element at index 3 vec.erase(vec.begin(), vec.begin() + 2); // erase first two elements

vector empty(): returns true if vector is empty, false otherwise insert(): add new element(s) at specified position vec.insert(vec.begin() + n, element); // add element at index n vec.insert(vec.begin() + n, numCopies, element); // add numCopies at index n invalidates existing iterators vector<int> vec(3, 50); // [50, 50, 50] vec.insert(vec.begin() + 1, 100); // add 100 at index 1: [50, 100, 50, 50] vec.insert(vec.begin() + 2, 2, 40); // [50, 100, 40, 40, 50, 50]

vector vector<string> v; v.push_back("hello"); v.push_back("bye"); vector<string>::iterator it = v.begin(); cout << itlength() << endl;

Iterating Backwards To traverse container's elements in reverse order, use a reverse_iterator For a reverse_iterator object: ++revIterator moves to the previous element rbegin(), rend() for(vector<int>::reverse_iterator it = vec.rbegin(); it != vec.rend(); it++) cout << *it << end; rend() rbegin() 18 -4 522 17

set set: represents an unordered collection without duplicates efficient insertion and removal, determining if element is in set insert(), erase(), count() count(): returns 1 if element in set, 0 otherwise elements must be comparable using < underlying implementation: binary search tree set<int> set1; set1.insert(120); // {120} set1.insert(32); // {32, 120} set1.insert(4); // {4, 32, 120} set1.insert(32); // {4, 32, 120} (no dupes) if(set1.count(32)) cout << "32 is in the set" << endl; cout << set1.size() << endl; // 3 set1.clear(); // removes all elements

set iterator set<int> mySet = ...; for(set<int>::iterator it = mySet.begin(); it != mySet.end(); it++) cout << *it << endl; The set's elements are traversed in sorted (increasing) order Example: #include<set> #include<cstdlib> set<int> randNums; for(int k = 0; k < 10; k++) randNums.insert(rand() % 100); // print random numbers in sorted order for(set<int>::iterator it = randomNums.begin(); it != randomNums.end(); it++) cout << *it << " ";

set Iterate over elements in a set in a specified range lower_bound(lower): returns iterator to first element >= lower upper_bound(upper): returns iterator to first element > upper To iterate over values in range [lower, upper]: for(set<int>::iterator it = mySet.lower_bound(100); it != mySet.upper_bound(200); it++) { cout << *it << endl; If set contains the odd numbers from 3 to 301, the loop will print 101, 103, ..., 199

pair parameterized helper class pair object holds two values of any type: pair<Type1, Type2> two fields: first (of Type1), second (of Type2) defined in <utility> pair<int, string> p1; p1.first = 312; p1.second = "I love C++"; pair<int, string> p2 = make_pair(422, "I love Java");

set Methods Method Description set<T> (iterator start, iterator stop) Constructs set containing copies of elements in range [start, stop). Duplicates are discarded. Elements are sorted. Accepts iterator from any source. set<int> theSet(vec.begin(), vec.end()); set<T>() Constructs empty set set<T>(const set<T>& other) Copy constructor set<int> set2 = set1; size_type size() const Returns number of elements in the set. int num = set1.size(); bool empty() const Returns true if set is empty, false otherwise iterator begin() Returns an iterator to the start of the set. set<int>::iterator it = set1.begin(); pair<iterator, bool> insert(const T& value) void insert(iterator begin, iterator end) first version: inserts value into set, returning a pair which contains an iterator to the element and true if value was inserted, false if it was already in set. second version: inserts range of elements into set, ignoring duplicates.

set Methods Method Description void clear() Removes all elements iterator end() Returns an iterator iterator find(const T& elt) Returns an iterator to specified element if it exists, and end otherwise size_type erase(const T& elt) void erase(iterator it) void erase(iterator start, iterator stop) if(mySet.erase(0)) {... // 0 erased mySet.erase(mySet.begin()); mySet.erase(mySet.begin(), mySet.end()); Removes an element from set. First version: 1 returned if element removed, 0 otherwise Second: removes element pointed at by it Third: Erases elements in range [start, stop) iterator lower_bound(const T& value) Returns iterator to first element >= value iterator upper_bound(const T& value) Returns iterator to first element > value

map key-value pairs map<string, string> dict; dict["thanks"] = "an expression of gratitude";

map mapping between set of keys and set of values elements are unordered – cannot be retrieved by position, only by key implemented using balanced binary tree keys must be comparable using < keys are unique – no two key-value pairs in map may have same key Useful operations: insert a key-value pair membership: check whether a key is in the map get value associated with a key remove a key-value pair

map: inserting elements myMap[key] = value; // add (key, value) to map map<string, int> myMap; // empty map myMap["apple"] = 2; //insert ("apple", 2) in myMap myMap["elephant"] = 3; square brackets can also be used to retrieve value associated with key: cout << myMap["apple"] << endl; // 2 cout << myMap["laugh"] << endl; // adds ("laugh", 0) and prints 0 find(): takes key and returns iterator that points to pair with that key, or end() if key doesn't exist in map map<string, int>::iterator it = myMap.find("laugh"); if(it == myMap.end()) cout << "key is not in map" << endl;

map iterator Dereferencing a map iterator produces a pair of type pair<const KeyType, valueType> Keys in a map cannot be modified, but they can be removed. Value can be modified. map<string, int>::iterator it = myMap.find("laugh"); if(it == myMap.end()) cout << "key is not in map" << endl; else cout << "Key " << itfirst << " has value " << itsecond << endl; To loop over a map: for(map<string, int>::iterator it = myMap.begin(); it != myMap.end(); it++) cout << itfirst << ": " << itsecond << endl; key-value pairs traversed in increasing sorted order of key (alphabetical order in our example) apple: 2 elephant: 3 laugh: 0 Is key in map?

map To remove an element with a specified key: myMap.erase(key); To remove all elements from map: clear() Number of elements in map: size() How to add a key-value pair: map<string, string> map1; map1["ee312"] = "happy"; map1["ee312"] = "sad"; // map1 is {("ee312", "sad")} map<string, string> map2; map2.insert(make_pair("ee312", "happy")); map2.insert(make_pair("ee312", "sad")); // map2 is {("ee312", "happy")} insert will not insert pair if a pair with the specified key is already in the map.

map: insert() A map stores a unique set of keys insert returns a value of type pair<iterator, bool> bool value: true if key-value pair inserted into map, false if key already existed in map iterator: points to key-value pair in map with specified key, whether or not it was newly added Using brackets to add a key-value pair often preferable pair<map<string, int>::iterator, bool> result = myMap.insert(make_pair("hi", 2)); if(!result.second) // if insertion failed, bool value is false result.firstsecond = 2; // update the value assigned to key update the second entry in the key-value pair to 2 iterator points to key-value pair with key "hi"

map Example Map a string to a set of synonyms // Map of <string, set<string>> map<string, set<string> > pairs; set<string> s1; s1.insert("hello"); s1.insert("hi"); s1.insert("hi ya"); pairs["greeting"] = s1; set<string> s2; s2.insert("bye"); s2.insert("later"); pairs["farewell"] = s2;

map Example Map a string to a set of synonyms for(map<string, set<string> >::iterator it = pairs.begin(); it != pairs.end(); it++) { cout << "Key: " << it->first << endl; for(set<string>::iterator it2 = (it->second).begin(); it2 != (it->second).end(); it2++) { cout << *it2 << " "; } cout << endl; Output: Key: farewell bye later Key: greeting hello hi hi ya Elements in pairs: ("greeting", {"hello", "hi", "hi ya"}) ("farewell", {"bye", "later"})