The Standard Template Library

Slides:



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

C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Data Structures Using C++ 2E
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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);
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Templates “Generic Programming” ECE Templates A way to write code once that works for many different types of variables –float, int, char, string,
Lecture 7 : Intro. to STL (Standard Template Library)
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
On dynamic memory and classes ● We previously had only discussed dynamic memory in regards to structs and dynamic arrays. ● However, they can be used (to.
Standard Template Library
C++ Standard Template Library
CS212: Object Oriented Analysis and Design
“Generic Programming” ECE 297
Lecture 6 of Computer Science II
Memory Management with Classes
CS 215 Final Review Ismail abumuhfouz Fall 2014.
ECE 264 Object-Oriented Software Development
Motivation and Overview
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Vectors Holds a set of elements, like an array
C++ Standard Library.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Standard Template Library (STL)
Chapter 4 Linked Lists.
STL Common tools for C++.
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
C++ Programming Standard Library
This pointer, Dynamic memory allocation, Constructors and Destructor
The Bag and Sequence Classes with Linked Lists
Collections Intro What is the STL? Templates, collections, & iterators
CMSC 341 Lecture 5 Stacks, Queues
10 – Iterators C++ Templates 4.6 The Iterator pgs
Array Lists Chapter 6 Section 6.1 to 6.3
Object Oriented Programming COP3330 / CGS5409
Chapter 18: Linked Lists.
Vectors.
C++ STL Vector Container
Vectors the better arrays.
14 – Sequential Containers
4.9 Multiple-Subscripted Arrays
7. 11 Introduction to C++ Standard Library Class Template vector (Cont
Doubly Linked List Implementation
Linked List and Selection Sort
Chapter 17: Linked Lists.
STL Iterators Separating Container from Data Access.
STL Algorithms Examples of how to use some STL algorithms.
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library
STL List.
Data Structures and Algorithms Memory allocation and Dynamic Array
Collections Intro What is the STL? Templates, collections, & iterators
An Introduction to STL.
CS 144 Advanced C++ Programming April 30 Class Meeting
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Video: The Sound of Raining
An Introduction to Programming though C++
14 – Sequential Containers
15 – Sequential Containers
4.1 Introduction Arrays A few types Structures of related data items
SPL – PS2 C++ Memory Handling.
STL List.
Data Structures & Programming
Presentation transcript:

The Standard Template Library Lego Blocks Example A brief introduction to the very expansive Standard Template Library (STL)

Today’s Questions Why should I use the STL? What is an std::vector? How can I use an std::vector? How does an std::vector work?

Why use the STL? Writing templates can be difficult Using templates is much easier and more productive E.g. write your own sort function versus use a templated sort function The STL has you covered std::swap std::sort std::pair std::min And so much more!

The Containers Library Several data structures are available std::vector – a dynamic contiguous array of elements of some type std::list – a doubly linked-list some type The data structures are templated std::vector<double> – a dynamic array that stores doubles std::list<std::string> – a linked-list that stores std::strings http://en.cppreference.com/w/cpp/container

Motivating std::vector We want to read integers from the user to an array until EOF 1 -20 3 31 55 <Ctrl+D> Then do some work on the array Problem: How big of an array should we allocate? We won’t know until after all the input is read. Solution: linked list (std::list) But lists are inefficient – accessing an element requires O(n) Better Solution: an array that can grow dynamically (std::vector) Accessing an element requires O(1)

Using std::vector – some of its member functions Accessing elements operator[] – same syntax as a C-style array Inserting new elements push_back(…) Modifying existing elements operator[] – still like a C-style array Querying number of elements size() int value = my_vector[42]; my_vector[42] = 64; http://en.cppreference.com/w/cpp/container/vector

Accepting Integer Input into a Vector #include <iostream> #include <vector> std::vector<int> get_integers(std::istream& stream) { std::vector<int> integers; int value; while(stream >> value) { integers.push_back(value); } return integers; input_to_vector.cpp

Accepting Integer Input into a Vector Include header to use vector #include <iostream> #include <vector> std::vector<int> get_integers(std::istream& stream) { std::vector<int> integers; int value; while(stream >> value) { integers.push_back(value); } return integers; Returns a vector of integers Calls vector’s default constructor: the vector is now empty input_to_vector.cpp

Accepting Integer Input into a Vector #include <iostream> #include <vector> std::vector<int> get_integers(std::istream& stream) { std::vector<int> integers; int value; while(stream >> value) { integers.push_back(value); } return integers; Insert a new int element into the vector. Will grow in size to accommodate new elements. input_to_vector.cpp

Printing a Vector’s Elements int main() { std::vector<int> integers = get_integers(std::cin); for(std::size_type i = 0; i < integers.size(); ++i) { std::cout << integers[i] << " "; } std::cout << "\n"; Use a for loop and print out each element in the vector. Remember that you can use operator[] and size()! input_to_vector.cpp

Printing a Vector’s Elements int main() { std::vector<int> integers = get_integers(std::cin); for(int i = 0; i < integers.size(); ++i) { std::cout << integers[i] << " "; } std::cout << "\n"; input_to_vector.cpp

Printing a Vector’s Elements int main() { std::vector<int> integers = get_integers(std::cin); for(int i = 0; i < integers.size(); ++i) { std::cout << integers[i] << " "; } std::cout << "\n"; int is signed size() returns size_t, which is unsigned Comparing signed and unsigned numbers will cause a compiler warning! input_to_vector.cpp warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

Printing a Vector’s Elements… Correctly int main() { std::vector<int> integers = get_integers(std::cin); for(size_t i = 0; i < integers.size(); ++i) { std::cout << integers[i] << " "; } std::cout << "\n"; input_to_vector.cpp

Vector Behind the Scenes Input: 1 3 8 7 2 <Ctrl + D> #include <iostream> #include <vector> std::vector<int> get_integers(std::istream& stream) { std::vector<int> integers; int value; while(stream >> value) { integers.push_back(value); } return integers; int * array = int size = 0 int capacity = 3 integers ? Default constructor creates a contiguous array of capacity N input_to_vector.cpp

Vector Behind the Scenes Input: 1 3 8 7 2 <Ctrl + D> #include <iostream> #include <vector> std::vector<int> get_integers(std::istream& stream) { std::vector<int> integers; int value; while(stream >> value) { integers.push_back(value); } return integers; int * array = int size = 1 int capacity = 3 integers 1 ? A new element is added, so the array is updated. Size increases by one. input_to_vector.cpp

Vector Behind the Scenes Input: 1 3 8 7 2 <Ctrl + D> #include <iostream> #include <vector> std::vector<int> get_integers(std::istream& stream) { std::vector<int> integers; int value; while(stream >> value) { integers.push_back(value); } return integers; int * array = int size = 2 int capacity = 3 integers 1 3 ? A new element is added, so the array is updated. Size increases by one. input_to_vector.cpp

Vector Behind the Scenes Input: 1 3 8 7 2 <Ctrl + D> #include <iostream> #include <vector> std::vector<int> get_integers(std::istream& stream) { std::vector<int> integers; int value; while(stream >> value) { integers.push_back(value); } return integers; int * array = int size = 3 int capacity = 3 integers 1 3 8 A new element is added, so the array is updated. Size increases by one. input_to_vector.cpp

Vector Behind the Scenes Input: 1 3 8 7 2 <Ctrl + D> #include <iostream> #include <vector> std::vector<int> get_integers(std::istream& stream) { std::vector<int> integers; int value; while(stream >> value) { integers.push_back(value); } return integers; int * array = int size = 3 int capacity = 3 integers 1 3 8 ? A new element to add, but capacity == size. Create a new array of capacity 2*size, then copy the old array’s values into it. Delete the old array to avoid memory leaks. input_to_vector.cpp

Vector Behind the Scenes Input: 1 3 8 7 2 <Ctrl + D> #include <iostream> #include <vector> std::vector<int> get_integers(std::istream& stream) { std::vector<int> integers; int value; while(stream >> value) { integers.push_back(value); } return integers; int * array = int size = 3 int capacity = 3 integers 1 3 8 1 3 8 ? A new element to add, but capacity == size. Create a new array of capacity 2*size, then copy the old array’s values into it. Delete the old array to avoid memory leaks. input_to_vector.cpp

Vector Behind the Scenes Input: 1 3 8 7 2 <Ctrl + D> #include <iostream> #include <vector> std::vector<int> get_integers(std::istream& stream) { std::vector<int> integers; int value; while(stream >> value) { integers.push_back(value); } return integers; int * array = int size = 3 int capacity = 6 integers 1 3 8 ? A new element to add, but capacity == size. Create a new array of capacity 2*size, then copy the old array’s values into it. Delete the old array to avoid memory leaks. input_to_vector.cpp

Vector Behind the Scenes Input: 1 3 8 7 2 <Ctrl + D> #include <iostream> #include <vector> std::vector<int> get_integers(std::istream& stream) { std::vector<int> integers; int value; while(stream >> value) { integers.push_back(value); } return integers; int * array = int size = 4 int capacity = 6 integers 1 3 8 7 ? Add new element to array. Size increases by one. input_to_vector.cpp

Vector Behind the Scenes Input: 1 3 8 7 2 <Ctrl + D> #include <iostream> #include <vector> std::vector<int> get_integers(std::istream& stream) { std::vector<int> integers; int value; while(stream >> value) { integers.push_back(value); } return integers; int * array = int size = 5 int capacity = 6 integers 1 3 8 7 2 ? A new element is added, so the array is updated. Size increases by one. input_to_vector.cpp

Vector’s Algorithmic Complexity Efficiently add elements with push_back() When there isn’t enough space, capacity grows (usually by 2x) So, even for a large number of N, push_back() will only generate a few array copies On average push_back() has O(1) complexity

Vector’s Different Constructors std::vector<int> vec1; // default constructor. size = 0 std::vector<int> vec2(10); // size = 10, values unspecified std::vector<int> vec3(10, -1); // size = 10, all values -1 vector_constructors.cpp

Vectors Versus C-Style Arrays int * a = new int[10]; int * b = new int[10]; std::vector<int> v1(10), v2(10); // access elements the same way a[0] = 5; v1[0] = 5; // creating copies a = b; v1 = v2; // destroying delete [] a; delete [] b; What happens when a = b? Shallow copy – memory leak! What happens when v1 = v2? Deep copy – v2’s elements are copied to v1 Do we need to delete v1 and v2? No, they are not pointers std::vector has a destructor, it will clean up after itself Tip: avoid pointers, let std::vector manage memory for you. vector_versus_arrays.cpp

Bounds Checking with Vectors operator[] does not create values operator[] does not check bounds Out-of-bounds indices will cause your program to crash! For the milestones, you can use DebugCheck to do bounds checking It will (also) make your program run more slowly std::vector<int> v1; // size 0 v1.push_back(-3); // size 1 v[0] = -2; // replace -3 with -2 v[1] = 5; // ? vector_bounds_checking.cpp

Vector’s Algorithmic Complexity Efficient random access to data with operator[] Internally, the vector stores a contiguous data array Accesses have O(1) complexity

Learning More About C++ and the STL Books Problem Solving in C++, Chapter 18 (Walter Savitch) C++ Primer, Lippman, Lajoie, and Moo Online http://en.cppreference.com/w/ http://www.cplusplus.com/ Google