C++ Standard Template Library

Slides:



Advertisements
Similar presentations
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Vectors, lists and queues
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Standard Containers: Vectors
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
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.
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
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.
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:
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Data Structures Using C++ 2E
Containers Overview and Class Vector
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.
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,
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
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)
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
Generic Programming and Library Design Brian Bartman
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.
Introduction to olympic programming
Concepts of Programming Languages
C++ Programming Standard Template Library Homework 5,6,7.
CSCE 210 Data Structures and Algorithms
Standard Template Library (STL)
STL Common tools for C++.
C++ Programming Standard Library
The C++ Algorithm Libraries
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
C++ Standard Template Library CSE 333 Spring 2018
abstracting away the data structure
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
ADT Implementations: Templates and Standard Containers
Today’s Learning Objective
Introduction to C++ STL
priority_queue<T>
ITERATORS Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
CSE 303 Lecture 25 Loose Ends and Random Topics
Vectors.
The Standard Template Library
Standard Template Library
Lecture 8 : Intro. to STL (Standard Template Library)
STL Библиотека стандартных шаблонов
STL (Standard Template Library)
Standard Template Library
Lesson 25 Miscellaneous Topics
C++ Programming: chapter 10 – STL
Collections Intro What is the STL? Templates, collections, & iterators
An Introduction to STL.
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
14 – Sequential Containers
SPL – PS1 Introduction to C++.
Standard Template Library
Presentation transcript:

C++ Standard Template Library Based on slides by Marat Dukhan

Standard Template Library (STL) Part of C++ standard Each C++ compiler ships with STL Provides template container classes vector (array) list (double-linked list) Map (hash table) set stack deque (double-ended queue) valarray (vector with defined arithmetic operations) New C++ standard adds few more Provides template algorithm functions initialization (constant, with specified function) sort, partial sort, nth element (e.g. median) search (linear and binary) minimum, maximum, sum, dot product, 1st difference

Getting Started with STL STL headers: Headers for containers have the same name as container E.g. use #include <vector> for vector container #include <algorithm> for most algorithmic functions #include <numeric> for sum, dot product, and 1st difference algorithms All STL functions and classes are defined in namespace std Either prepend names with std:: E.g. std::vector<int> v Or add using namespace std; before you refer to STL names

STL vector container std::vector<int> v; /* Reserve space for 100 elements */ v.reserve(100); for (size_t i = 0; i < v.capacity(); i++) { /* Adds element i to the end of a vector */ v.push_back(i); } for (size_t i = 0; i < v.size(); i++) { std::cout << "Element " << i << " is " << v[i] << std::endl; }

More STL vector examples #include <vector> std::vector<int> v; /* Resize to 100 elements. New elements will be created with default constructor */ v.resize(100); for (size_t i = 0; i < v.size(); i++) { /* Change value of element i */ v[i] = i; }

Traversing STL Vector with Iterators std::vector<int> array; array.resize(10); for (std::vector<int>::iterator current = array.begin(); current != array.end(); ++current) { *current = 0 } for (std::vector<int>::const_iterator current = array.begin(); current != array.end(); ++current) { std::cout << (*current) << std::endl; } 1 2 3 4 5 6 7 8 9 10 array.begin() array.end()

Auto keyword With new C++ standard (C++11) you can replace std::vector<int>::iterator with auto. auto variable_name = expression Creates a variable of the same type as expression for (auto current = array.begin(); current != array.end(); ++current) { *current = 0 } for (auto current = array.cbegin(), current != array.cend(); ++current) { std::cout << (*current) << std::endl; }

STL Algorithms std::vector<double> v = ... const double firstElement = v.front(); const double lastElement = v.back(); /* Sort all elements but the first one */ std::sort(v.begin() + 1, v.end()); /* Linear search */ std::vector<double>::const_iterator iterator = std::find(v.begin(), v.end(), firstElement); if (iterator != v.end()) { std::cout << "Found the first element" << std::endl; } /* Sort in reverse order */ std::sort(v.rbegin(), v.rend()); iterator = std::binary_search(v.rbegin(), v.rend(), lastElement); if (iterator != v.rend()) { std::cout << "Found the last element" << std::endl; }

More STL Algorithms const double sum = std::accumulate(v.begin(), v.end(), 0.0); const double minimum = std::min(v.begin(), v.end()); std::vector<double>::const_iterator maximumElementIterator = std::max_element(v.begin(), v.end()); const size_t maximumElementIndex = std::distance(v.begin(), maximumElementIterator); /* Initialize all elements to 1.0 */ std::fill(v.begin(), v.end(), 1.0);

More STL Containers std::string text = "Hello" text += ", World!" std::cout << text << std::endl; std::map<std::string, unsigned int> wordCounts; wordCounts["Hello"] = 1; wordCounts["World"] = 1; std::cout << wordCounts["Hello"] << std::endl; assert(wordCounts.find("C++") == wordCounts.end()); std::vector<std::vector<double> > vectorOfVectors;

Copy/paste example #include <iostream> #include <algorithm> #include <vector> using namespace std; #define VECSIZE 100 int main() {     vector<int> v;     v.resize(VECSIZE);     for (int i=0; i<VECSIZE; i++)     {         v[i] = rand() % 100000;     }     sort(v.begin(), v.end());         cout << v[i] << "\n";     }         return 0; }