/
Collections Intro What Collections Intro What

Collections Intro What - PowerPoint Presentation

debby-jeon
debby-jeon . @debby-jeon
Follow
342 views
Uploaded On 2019-12-05

Collections Intro What - PPT Presentation

Collections Intro What is the STL Templates collections amp iterators Adapted from Dr Mary Eberlein Data Structures Collection or data structure an object that stores data elements objects that are stored ID: 769153

int vector elements iterator vector int iterator elements size element collection stl type push collections data returns array myarr

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Collections Intro What" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Collections Intro What is the STL? Templates, collections, & iterators Adapted from Dr. Mary Eberlein

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)

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;

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

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

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

Example 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 validityResizing 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!

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 vectorint numElts = v.size(); // 2front(): returns reference to first element of vectorint& num = v.front(); // 3back(): 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 1 st 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/