the Standard Template Library

Slides:



Advertisements
Similar presentations
Chapter 21 The STL (maps and algorithms) Bjarne Stroustrup
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Beginning C++ Through Game Programming, Second Edition
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.
Rossella Lau Lecture 2, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure.
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.
C++ for Engineers and Scientists Third Edition
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structures Using C++ 2E
Containers Overview and Class Vector
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Generic Programming Using the C++ Standard Template Library.
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.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Lecture 7 : Intro. to STL (Standard Template Library)
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
Standard Template Library
Module 20/21/22: The Standard Template Library
CS212: Object Oriented Analysis and Design
Pointers and Linked Lists
Pointers and Linked Lists
Programming with ANSI C ++
Standard Template Library
Vectors Holds a set of elements, like an array
CSCE 210 Data Structures and Algorithms
Chapter 8 Arrays, Strings and pointers
Standard Template Library (STL)
Chapter 4 Linked Lists.
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Collections Intro What is the STL? Templates, collections, & iterators
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
Associative Structures
Introduction to the Standard Template Library
Vectors.
C++ STL Vector Container
14 – Sequential Containers
Chapter 9 One-Dimensional Arrays
7. 11 Introduction to C++ Standard Library Class Template vector (Cont
Object Oriented Programming in java
Standard Template Library Model
Recursive Linked List Operations
Standard Version of Starting Out with C++, 4th Edition
Chapter 17: Linked Lists.
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
CS 144 Advanced C++ Programming February 7 Class Meeting
Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
14 – Sequential Containers
The List Container and Iterators
Introduction to Computer Science
Presentation transcript:

the Standard Template Library Vector Operations and the Standard Template Library (STL)

Review The standard template library (STL) provides the vector, a container for storing multiple values of the same type. A vector is an indexed container, like a string. The values in a vector can be accessed using the subscript operator ([]). 13 21 08 14 19 1 2 3 4

Vector Attributes A vector has several attributes, including its data -- the values it contains. size -- the number of values it contains; and capacity -- the number of values it can store before having to grow. 13 21 08 14 19 1 2 3 4 data 5 size 8 capacity 6 7

size() and capacity() From this, it should be obvious that the vector function member size() simply returns the value of its size attribute: 13 21 08 14 19 1 2 3 4 data 5 size 8 capacity 6 7 A vector also has a function member named capacity() that returns the value of its capacity attribute.

Declarations: 3 Forms vector<double> aVector; // empty data size capacity const int N = 8; // N default vector<double> aVector(N); // values 1 2 3 4 data 8 size capacity 5 6 7 const int N = 8; // N specified vector<double> aVector(N, 1); // values 1 2 3 4 data 8 size capacity 5 6 7

Vector Declarations You can pass any type to a vector when it is declared: const int N = 8; vector<bool> boolVec(N, false); 1 2 3 4 data 8 size capacity 5 6 7 const int N = 8; vector<string> stringVec(N, “Hi”); Hi 1 2 3 4 data 8 size capacity 5 6 7

Vector push_back() Values can be appended to a vector using the push_back() function member, which causes the vector to “grow” if necessary. vector<double> aVector; // empty f data size capacity aVec.push_back(13); 13 1 2 3 4 data size 8 capacity 5 6 7

Appending (Ct’d) Subsequent calls to push_back() continue to append: aVec.push_back(6); aVec.push_back(21); 13 6 21 1 2 3 4 data size 8 capacity 5 7 When the size equals the capacity, a call to push_back() causes the vector to grow again. The only limit on a vector’s growth is the amount of memory available.

Vector pop_back() To remove the most recently appended value, the vector function member pop_back() can be used: 13 6 21 1 2 3 4 data size 8 capacity 5 7 aVec.pop_back(); 13 6 1 2 3 4 data size 8 capacity 5 7 Note that push_back() and pop_back() alter size.

Vector front() and back() The first and last values in a vector can be retrieved with the front() and back() function members: 13 6 1 2 3 4 data size 8 capacity 5 7 double alpha = aVec.front(); // alpha == 13 double omega = aVec.back(); // omega == 6 The front() and back() function members provide a convenient way to access the ends of a vector without finding using subscripts.

Vector Subscript Like a string, the individual values in a vector can be accessed using the subscript ([]) operator. 13 6 1 2 3 4 data size 8 capacity 5 7 double first = aVec[0]; // first == 13 double second = aVec[1]; // second == 6 The subscript operation aVec[i] provides a convenient way to access the value in aVec whose index is i, where i is a for loop control variable.

Safe Vector Element Access The subscript operator does not check that the index being accessed is valid. The at() function member does check the index for validity, making it safer, but slower. 13 6 1 2 3 4 data size 8 capacity 5 7 double first = aVec.at(0); // first == 13 double second = aVec.at(1); // second == 6

Vector Iterators Each STL container provides 2 function members: begin(), that points to the first data value; and end(), that points beyond the last data value. 13 6 21 1 2 3 4 data size 8 capacity 5 7 aVec aVec.begin() aVec.end() STL uses such “pointers” to iterate through the values of a container, and so they are called iterators.

STL Algorithms In addition to containers like class vector, the STL defines a rich set of algorithms that can be applied to any STL container, just by using the directive: #include <algorithm> To make these algorithms independent of any particular container, they take as arguments a container’s iterators, rather than the container itself.

Algorithm accumulate() The accumulate() algorithm returns the result of applying + to each of an STL container’s values: 13 6 21 1 2 3 4 data size 8 capacity 5 7 aVec aVec.begin() aVec.end() double sum = accumulate(aVec.begin(), // sum aVec.end(), 0); // == // 40 The third argument to accumulate() lets you sum starting with a base value other than zero.

Algorithm sort() The sort() algorithm arranges the values of an STL container in ascending order: 13 6 21 1 2 3 4 data size 8 capacity 5 7 aVec aVec.begin() aVec.end() sort(aVec.begin(), aVec.end()); 6 13 21 1 2 3 4 data size 8 capacity 5 7 aVec aVec.begin() aVec.end()

Algorithm find() The find() algorithm searches an STL container for a particular value, returning an iterator to it: 13 6 21 1 2 3 4 data size 8 capacity 5 7 aVec aVec.begin() aVec.end() vector<double>::iterator pos = find(aVec.begin(), aVec.end(), 6); 13 6 21 1 2 3 4 data size 8 capacity 5 7 aVec aVec.begin() aVec.end() pos

Vector erase() The erase() function member lets you remove a value at a position pointed to by an iterator: vector<double>::iterator pos = find(aVec.begin(), aVec.end(), 6); 13 6 21 1 2 3 4 data size 8 capacity 5 7 aVec aVec.begin() aVec.end() pos aVec.erase(pos); 13 21 1 2 3 4 data size 8 capacity 5 6 7 aVec aVec.begin() aVec.end()

Vector erase() Alternatively: aVec 13 6 21 1 2 3 4 data size 8 capacity 5 7 aVec aVec.begin() aVec.end() aVec.erase(find(aVec.begin(), aVec.end(), 6)); 13 21 1 2 3 4 data size 8 capacity 5 6 7 aVec aVec.begin() aVec.end()

Vector insert() The insert() function member lets you insert a value within a vector, before an iterator: 13 21 1 2 3 4 data size 8 capacity 5 6 7 aVec aVec.begin() aVec.end() aVec.insert(aVec.begin(), 9); 9 13 21 1 2 3 4 data size 8 capacity 5 6 7 aVec aVec.begin() aVec.end()

erase() and insert() The erase() and insert() function members are both expensive in terms of time, because of the extensive copying each performs. insert() must copy all values to the right of the insertion point, to “make room” for the new value. erase() must copy all values to the right of the erased element, to “fill in” the empty spot. As a result, these should be used with caution.

Summary The vector class provides many function members for operating on vectors. The C++ standard template library (STL) provides containers, algorithms, and iterators to connect algorithms and containers. A vector is an STL container, so that many STL algorithms can be used to operate on vectors.

Summary (Ct’d) There are many more STL algorithms beyond the ones presented here (we barely scratched the surface). For a complete list, see the most recent edition of “The C++ Programming Language”, by Bjarne Stroustrup, Addison-Wesley.