Introduction to C++ STL

Slides:



Advertisements
Similar presentations
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Advertisements

COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
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.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
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.
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.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Data Structures Using C++ 2E
LECTURE LECTURE 17 More on the Standard Template Library 23 A guided tour with common examples of the containers.
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.
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.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Lecture 7 : Intro. to STL (Standard Template Library)
CS 403: Programming Languages Lecture 24 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8.
Vectors CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
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.
1 Priority Queues (Heaps)  Sections 6.1 to Priority Queues  Regular queues which supports –First In, First Out –Enqueue(): add a new element.
Object-Oriented Programming (OOP) Lecture No. 41
Lecture 35a OOP The STL Standard Template Library
C++ Standard Template Library
Introduction to olympic programming
Concepts of Programming Languages
CSCE 3110 Data Structures & Algorithm Analysis
Priority Queues (Heaps)
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Standard Template Library
Standard Template Library (STL)
Basic Data Structures.
C++ Programming Standard Library
Starting Out with C++ Early Objects Eighth Edition
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Basic Data Structures.
Lecture 7-3 : STL Algorithms
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
ADT Implementations: Templates and Standard Containers
Standard Template Library
Containers & Iterators
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
Data structures and algorithms
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library (STL)
Containers: Queue and List
STL (Standard Template Library)
Standard Template Library
C++ Programming: chapter 10 – STL
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
CS 144 Advanced C++ Programming April 23 Class Meeting
Chapter 9 – Sets and Maps 9.1 Associative Container
Standard Template Library
Presentation transcript:

Introduction to C++ STL

STL Classes Container Classes Storage Class stack, queue, vector, map ... Iterator Classes Algorithm Classes frequently used algorithms sort, find, binary search, next_permutation ...

STL Containers

Vector random access ([] operator) like an array add / remove an element in constant time at the last of vector add / remove an element in linear time in the middle of vector automatic memory management needn’t specify quantity of elements

Vector Operations #include <vector> int main() { vector<int> v; v.push_back(10); cout<<v[0]<<endl; v.push_back(20); cout<<v[0]<<” “<<v[1]<<endl; cout << v.size() << “ “ << v.capacity() << endl; v.pop_back(); print(v); } /* Output: 10 10 20 2 2 1 2 */

String Class Operations string s = "hello"; string s1 = s.substr(0, 3), // "hel" s+=“ Ayan” s2 = s.substr(1, 6), // "ello A“ s4 = s.substr(1); // "ello Ayan"

List Sequence Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence. Doubly-linked list

List Operations #include <list> int main() { list <int> la, lb; la.push_back(0), la.push_back(1), la.push_back(3); lb.push_back(4), lb.push_front(2); la.sort(); lb.sort(); la.merge(lb); /*merge function merges 2 sorted lists*/ print(la); } /* Output: 0 1 2 3 4 */

Set Associative Elements in associative containers are referenced by their key and not by their absolute position in the container. Ordered The elements in the container follow a strict order at all times. All inserted elements are given a position in this order. Unique keys No two elements in the container can have equivalent keys. Implemented as balanced binary search trees. Insertion Searching complexity O(logn)

Set Operations /* Output: Ayan Nitin Sadhu Sagar Sasy */ #include <set> struct ltstr { bool operator() (const char* s1, const char* s2) const return (strcmp(s1, s2) < 0); } int main() const char* str[6] = {“Ayan”, “Sagar”, “Nitin” ,“Sasy”, “Sadhu”}; set<const char*, ltstr> s(str, str + 6); print(s); /* Output: Ayan Nitin Sadhu Sagar Sasy */

MultiSet Same as set But elements need not be unique.

Stack Operations #include <stack> int main() { stack<int> s; s.push(8); s.push(5); s.push(6); cout << s.top() << endl; s.pop(); } /* Output: 6 5 */

Queue Operations #include <queue> int main() { queue<int> q; q.push(8); q.push(5); q.push(6); cout << q.top() << endl; q.pop(); } /* Output: 8 5 */

priority queue Max Heap Maximum Element at top of queue

Priority_Queue Operations #include <queue> int main() { priority_queue< int > pq; int ary[6] = {1, 4, 2, 8, 5, 7}; for (int i = 0; i < 6; pq.push(ary[i++])); while (!pq.empty()) { cout << pq.top() << “ “; pq.pop(); } /* Output: 8 7 5 4 2 1 */

Map Associative Elements in associative containers are referenced by their key and not by their absolute position in the container. Ordered The elements in the container follow a strict order at all times. All inserted elements are given a position in this order. Map Each element associates a key to a mapped value: Keys are meant to identify the elements whose main content is the mapped value. Unique keys No two elements in the container can have equivalent keys. Implemented as balanced binary search trees. Insertion Searching complexity O(logn)

Map Operations #include <map> int main() { map<string, int> grade; grade[“Mark”] = 95; grade[“Edward”] = 87; grade[“Louise”] = 66; grade[“Allen”] = 76; for(map<string,int> iterator it=grade.begin();it!=grade.end();it++) cout<<*it.first<<“ “<<*it.second<<endl; cout << grade[“Allen”] << endl; } /* Output: Allen, 76 Edward, 87 Louise, 66 Mark, 95 76 */

Deque Double Ended Queue Functionality similar to vectors, but with efficient insertion and deletion of elements also at the beginning of the sequence, and not only at its end Sequence Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.

Deque Operations #include <deque> int main() { deque<int> dq; dq.push_back(3); dq.push_front(1); dq.insert(de.begin() + 1, 2); dq[2] = 0; } /* Output: 1 2 0 *

STL Iterators Every STL container holds a nested iterator class

Iterator Operations #include <vector> #include <iterator> #include <algorithm> int main() { vector<int> v; int ary[6] = {1, 4, 2, 8, 5, 7}; for (int i = 0; i < 6; v.push_back(ary[i++])); for (vector<int>::iterator it = v.begin(); it != v.end(); it++) cout << (*it) << “ “; cout << endl; } /* Output: 1 4 2 8 5 7 */

Common STL Algorithms

find int main () { int myints[] = { 10, 20, 30 ,40 }; int * p; // pointer to array element: p = find (myints,myints+4,30); ++p; cout << "The element following 30 is " << *p << '\n'; vector<int> myvector (myints,myints+4); vector<int>::iterator it; // iterator to vector element it = find (myvector.begin(), myvector.end(), 30); ++it; cout << "The element following 30 is " << *it << '\n'; return 0; } The element following 30 is 40

Next_permutation // next_permutation example #include <algorithm> // std::next_permutation, std::sort int main () { int myints[] = {1,2,3}; sort(myints,myints+3); cout<<"The 3! possible permutations with 3 elements:\n"; do { print(myints); } while(next_permutation(myints,myints+3)); return 0; } The 3! possible permutations with 3 elements: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1

sort() #include<algorithm> int main() { int ar[5]={3,2,4,1,5}; vector<int> v; for(int i=0;i<5;i++) v.push_back(ar[i]); sort(ar,ar+5); sort(v.begin(),v.end(),greater<int>); print(ar); print(v); } /* Output 1 2 3 4 5 5 4 3 2 1 */

Binary Search /*Output lower_bound at position 3 // lower_bound/upper_bound example #include <iostream> // std::cout #include <algorithm> // std::lower_bound, std::upper_bound, std::sort #include <vector> // std::vector int main () { int myints[] = {10,20,30,30,20,10,10,20}; std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector<int>::iterator low,up; low=std::lower_bound (v.begin(), v.end(), 20); up= std::upper_bound (v.begin(), v.end(), 20); std::cout << "lower_bound at position " << (low- v.begin()) << '\n'; std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; return 0; } /*Output lower_bound at position 3 upper_bound at position 6 */

A Small Example vector<pair<int,int> > v(5); for(int i=0;i<5;i++) v[i]=make_pair(5-i,i); sort(v.begin(),v.end()); for(vector<pair<int,int> >::iterator it=v.begin();it!=v.end();it++) cout<<*it.first<<“ “<<*it.second<<endl; for(int i=0;i<5;i++) cout<<v[i].first<<“ “v[i].second<<endl; Output 1 4 2 3 3 2 4 1 5 0

Longest Increasing Subsequence set<int> st; set<int>::iterator it; st.clear(); for(int i=0; i<n; i++) { it=st.find(array[i]); if(*it==array[i]); else { st.insert(array[i]); it++; if(it!=st.end()) st.erase(it); } for(set<int>::iterator it2=st.begin();it2!=st.end();it2++) printf("%d ",*it2); printf("\n"); cout<<st.size()<<endl;

Longest Increasing Subsequence Operations /*Input 5 1 2 1 */ /*Output 1 1 2 2 /*Input 5 1 4 2 4 3 */ /*Output 1 1 4 1 2 1 2 4 1 2 3 3