STL !!!generic programming!!! Anar Manafov

Slides:



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

Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Introduction to the STL
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
More on the STL vector list stack queue priority_queue.
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.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Templates and the STL.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
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:
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
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
Containers and Iterators CNS 3370 Copyright 2003, Fresh Sources, Inc.
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
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.
Generic Programming Using the C++ Standard Template Library.
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Intro to the C++ Std Library.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
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.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
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.
 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 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.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
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 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
1 The Standard Template Library Drozdek Section 3.7.
Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.
Lecture 7-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Object-Oriented Programming (OOP) Lecture No. 42.
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.
Object-Oriented Programming (OOP) Lecture No. 41
Standard Template Library
Introduction to olympic programming
Standard Template Library (STL)
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Chapter 22: Standard Template Library (STL)
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
STL Библиотека стандартных шаблонов
Standard Template Library
Standard Template Library
An Introduction to STL.
Presentation transcript:

STL !!!generic programming!!! Anar Manafov

STL (Standard Template Library) or STL stands for Stepanov and Lee The Evolution STL is not a new library as compared to other libraries. Originally, the development of the STL was started by Alexander Stepanov at HP in Later, he was joined by David Musser and Meng Lee. In 1994, STL was included into ANSI and ISO C++. recommended link (Al Stevens Interviews Alex Stepanov, March 1995 issue of Dr. Dobb's Journal, ):

STL Components Here is a list of elements of the STL. The first three of them are fundamental items. - Container (An object that holds other objects) - Algorithm (A function that acts on containers) - Iterator (A pointer-like object) - Allocator (This item manages memory allocation in a container) - Adaptor(Transforms one object into another) - Predicate (A function that returns a boolean value, true or false.) - Function Object (A class that defines operator().) Recommended link: and

STL Components

containers, iterators, algorithms vector list sort find

containers, iterators, algorithms #include using namespace std; int ia[10]={33,17,11,88,43,99,6,9,12,7}; int main(){ vector x(ia,ia+10); sort(x.begin(), x.end()); begin() end()

// grab value to search for int s_value; cin >> s_value; // search for an element vector ::iterator found; found=find(x.begin(),x.end(),s_value); if(found != x.end()) { cout << "search value found!\n"; } else { cout << "search value not found!\n"; }

list instead of vector #include using namespace std; int ia[10]={33,17,11,88,43,99,6,9,12,7}; int main(){ list x(ia,ia+10); sort(x.begin(), x.end());

// grab value to search for int s_value; cin >> s_value; // search for an element list ::iterator found; found=find(x.begin(),x.end(),s_value); if(found != x.end()) { cout << "search value found!\n"; } else { cout << "search value not found!\n"; }

Algorithms with built-in arrays #include using namespace std; int ia[10]={33,17,11,88,43,99,6,9,12,7}; int main(){ int s_value; cout << "enter search value: "; cin >> s_value; int *found; found=find(&ia[0],&ia[10],s_value); if(found != ia+10)...

Containers Sequence Containers Vector List Deque Stack Queue Adaptor Priority_queue Almost Containers Built-in arrays, strings, valarrays, bitsets Associative Containers Map Multimap Set Multiset

Containers

Container Vector: Deque: List: Set/Multiset:Map/Multimap:

string Type #include using namespace std; string s1("Hello"); string s2("World"); cout << s1 + " " + s2 + '\n'; string s3(s1 + " " + s2); cout << '\"'<< s3 << "\" has " << s3.length() << " characters" << endl; replace(s3.begin(), s3.end(), 'W', 'w'); cout << s3 << endl;

Constructors container() container(n) (not for ass. cont.) container(n,x) (not for ass. cont.) container(first,last) container(c) ~container()

Stack, Queue, List Operations push_back() pop_back() push_front() pop_front() insert(p,x) insert(p,n,x) insert(p,first,last) erase(p) erase(first,last) clear()

Algorithms copy sort find fill partition insert, delete union, intersection accumulate...

Iterators The glue that makes it possible to use generic algorithms and orthogonalize those algorithms from the data structures An iterator i is a generalized means of traversing a data structure: –for an array, an array index or a pointer Dereferencing an iterator, *i, is guaranteed to give the item, but an iterator does not obey all pointer operations

Iterators, Element Access begin() end() rbegin() rend() front() back() [] at() Points to first element Points to one-past-last element Points to first element of reverse seq. Points to one-past-last element of rev. seq. First element Last element Subscripting, unchecked access Subscripting, checked access

Iterator Operations

Iterators (continued) container ::iterator first=c.begin(); container ::iterator last=c.end(); [first,last) ++i; first == last; i != last; i + n; (long jump) *i = x; x = *i;

Nonmodifying Sequence Operations for_each() find() find_if() find_first_of() adjacent_find() count() count_if() mismatch() equal() search() find_end() search_n() list all;... for_each(all.begin(),all.end(),Print(cout));

Function objects class bThan{ public: bThan(int x): testVal(x) {} const int testVal; bool operator()(int val){return val>testVal;} }; list ::iterator firstBig = find_if(aList.begin(),aList.end(),bThan(12));

OO ? STL is not OO, is OOP dead? Not all problems are best solved in OOP fashion! Many problems are best solved in an OOP manner. Know as much as you can about as many styles of programming as you can, an use the style most appropriate to the problem.

#include using namespace std; int main() { map > directory; directory["Anar"] = 2128; directory["Victor"] = 1395; directory["Demo"] = 12344; string name; while (cin >> name) { if ( directory.find(name) != directory.end() ) cout << "The phone number for " << name << " is " << directory[name] << endl; else cout << "Sorry, no listing for " << name << endl; }

#include using namespace std; typedef map > Directory_t; typedef Directory_t::value_type Entry; int main() { Directory_t directory; // Will change the value of the key or insert if the key is exist already directory["Anar"] = 2128; // will insert or fail if the key is exist already directory.insert ( make_pair ("Victor", 1395) ); directory.insert( Entry("Demo", 12344) ); string name; while (cin >> name) { Directory_t::iterator iter = directory.find(name); if ( iter != directory.end() ) cout first << " is " << (*iter).second << endl; else cout << "Sorry, no listing for " << name << endl; }

/*! \fn void TrimRight(std::basic_string &_str, const std::basic_string &_Val) \brief Trims trailing characters from the string. \param _Val - [in] The target characters to be trimmed. */ template void TrimRight(std::basic_string &_str, const std::basic_string &_Val) { _str.resize( _str.find_last_not_of( _Val ) + 1 ); } /*! \fn void TrimLeft(std::basic_string &_str, const std::basic_string &_Val) \brief Trims leading characters from the string. \param _Val - [in] The target characters to be trimmed. */ template void TrimLeft(std::basic_string &_str, const std::basic_string &_Val) { _str.erase( 0, _str.find_first_not_of( _Val ) ); } Some real life examples small, but very handy!