C++ STL Vector Container

Slides:



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

Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
 2006 Pearson Education, Inc. All rights reserved Templates.
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.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
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
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
1 Iterators Good reference site:
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
Lecture 7 : Intro. to STL (Standard Template Library)
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CS212: Object Oriented Analysis and Design Lecture 28: Functors.
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.
Chapter 1 C++ Templates (Sections 1.6, 1.7). Templates Type-independent patterns that can work with multiple data types. Function Templates  These define.
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.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Standard Template Library
CS212: Object Oriented Analysis and Design
Programming with ANSI C ++
Standard Template Library
Vectors Holds a set of elements, like an array
Chapter 8 Arrays, Strings and pointers
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Collections Intro What is the STL? Templates, collections, & iterators
Chapter 22: Standard Template Library (STL)
C++ Templates L03 - Iterator 10 – Iterator.
10 – Iterators C++ Templates 4.6 The Iterator pgs
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
Sorting CSCE 121 J. Michael Moore
Chapter 18: Linked Lists.
C++ STRINGS string is part of the Standard C++ Library
CS212: Object Oriented Analysis and Design
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
C++ STL List Container C++ STL list container Examples
priority_queue<T>
CS212: Object Oriented Analysis and Design
Constructors and Other Tools
Standard Template Library Find
Doubly Linked List Implementation
Recursive Linked List Operations
The Standard Template Library
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Lecture 8 : Intro. to STL (Standard Template Library)
C++ STL Stack, Queue, and Deque
the Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Doubly Linked List Implementation
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
SPL – PS1 Introduction to C++.
A dictionary lookup mechanism
Standard Template Library
Presentation transcript:

C++ STL Vector Container And C++ STL Array container, A few STL algorithms

STL Vector Container Header file <vector> Important member functions begin() and end(): return iterators clear(): delete all elements push_back(): insert at end pop_back(): delete last element size(): number of elements empty(): if vector is empty resize(): change vector size

Maximum and Average Values See max_avg.txt and max_avg.cpp To compile: make max_avg.x In general, in order to compile a program with name.cpp, you can type: make name.x, due to the way we write our makefile

Word Puzzle Problem description Explaining the source code word_puzzle.pdf Explaining the source code word_puzzle_vector.h, word_puzzle_vector.cpp Pay attention to the use of I/O streams, stringstreams, strings, and vector To compile: make

C++ STL Array Container New in C++11 Similar to array in C/C++ in that Array has fixed size memory Will not grow or shrink like other containers such as Vector Support similar interfaces as other containers But with notable difference (see next slide) Likely more efficient than Vector Use vector if you need to grow or shrink container Use vector if you are not sure We do not want to over-emphasize the efficiency of array over vector. Despite the efficiency of array over vector, it is likely that you should still prefer to use vector whenever you are not sure which one (array or vector) to use.

STL Array Container Header file <array> template < class T, size_t N > class array; Important member functions begin(), end(), for iterator support size(), max_size(), size of array empty(), test if size of array is zero Index operator[], at(), access element at specified position front(), back(), refer to first and last element, respectively data(), return a pointer to internal data (C/C++ pointer) fill(), set all elements in array to the specified value

STL Array Container See array_sort.cpp To compile: make array_sort.x Copy is an STL algorithm in <algorithm> http://www.cplusplus.com/reference/algorithm/copy/ Similarly, sort is an STL algorithm http://www.cplusplus.com/reference/algorithm/sort/ Note also the behavior of size(), max_size(), and fill()

STL Algorithm sort() Function signature See r3/example2.cpp void sort(RandomAccessIterator first, RandomAccessIterator last); void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp); Sort elements in the range [first, last) Note that containers must be random access containers The first version using operator<() overloaded for the corresponding data type The second version using function object of type Compare You can also use lambda function for the comparison http://www.cplusplus.com/reference/algorithm/sort/ See r3/example2.cpp

STL Algorithm find() Function signature See examples/r3/example1.cpp InputIterator find (InputIterator first, InputIterator last, const T& val); Search val in the range [first, last) Return iterator to first appearance of val in the range Return last if not found in the range http://www.cplusplus.com/reference/algorithm/find/ See examples/r3/example1.cpp

STL Algorithm max_element() Function signature ForwardIterator max_element(ForwardIterator first, ForwardIterator last); ForwardIterator max_element(ForwardIterator first, ForwardIterator last, Compare comp); Return largest value in the range [first, last) The first version using operator<() overloaded for the corresponding data type The second version using function object of type Compare You can also use lambda function for the comparison http://www.cplusplus.com/reference/algorithm/max_element/ See r3/example3.cpp