Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 22 Implementing lists: linked implementations.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CSE Lecture 12 – Linked Lists …
Chapter 6 Queues and Deques.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Stack & Queues COP 3502.
Data Structures Using C++ 2E
Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
M180: Data Structures & Algorithms in Java
Beginning C++ Through Game Programming, Second Edition
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
More on the STL vector list stack queue priority_queue.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
C++ for Engineers and Scientists Third Edition
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Huffman code uses a different number of bits used to encode characters: it uses fewer bits to represent common characters and more bits to represent rare.
Templates and the STL.
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Information and Computer Sciences University of Hawaii, Manoa
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Suppose you have a problem involving N data points. Recursive solution of such problem is a follows: If the problem can be solved directly for N points.
C++ STL CSCI 3110.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing.
 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 7 : Intro. to STL (Standard Template Library)
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
The Standard Template Library Container Classes Version 1.0.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Standard Template Library
Standard Template Library (STL)
Lecture 8 : Intro. to STL (Standard Template Library)
STL (Standard Template Library)
Standard Template Library
C++ Programming: chapter 10 – STL
Standard Template Library
Presentation transcript:

Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing elements by sequence (not by index) -queues, stacks, linked lists -Require iterators to locate elementsContainers

In principle one can solve any problem with arrays or vectors. In practice queues, stacks and linked lists are more convenient and more efficient performance- wise for select applications. Why Sequential Containers?

Suppose we are dealing with N points organized in array. To copy them from one location to another will take an order of N operations – O(N). To get i-th point – pt[i] – from array would always take the same constant time – O(1). To sort the array via brute force approach would take order N*N operations – O(N 2 ). To sort the array via bubble sort would take order N*log 2 (N) – O(NlogN). Efficiency: Big-O Notation

Huge collection of generic classes at your disposal! string, vector, queue, dequeue, list, etc. Include files match class names. Type class name and click F1 for reference. Include header file then use IntelliSense. Standard Template Library (STL)

#include string s1, s2; cin >> s1 >> s2; cout << (s1 + s2); size() returns string size;, =, !=, and == comparison operators; > stream I/O operators; + Concatenates two string objects; [ ] index operator for character access. string class (STL)

#include vector v; v.push_back(1); size(), capacity() returns vector size, capacity; insert() inserts element at specified pos; erase() removes element at specified pos; push_back() adds element at the end; pop_back() removes element at the end. begin() returns position of the first element; end() returns position of the last element. [] index operator Generic vector class (STL)

vector is cool, but insert() and erase() operations operate in linear time – O(N). What if we need to insert and remove quickly? We can use linked list to insert and remove in constant time – O(1)! Linked List: Rationale

Linked List: Illustration Max Lex Nika Vlad Joe insert move down to make room vector Max Lex Nika Vlad Joe insert linked list Max Lex Nika Vlad Joe

Each list node holds data and a pointer to the next list node. In the last node the pointer to the next node is NULL. Linked List: Node Data SomeData Value; Node* Next; Node

Node* node = listHead; do { // do something with current node … // next node node = node->Next } while ( node != NULL); Linked List: Traversing

Double-Linked List: Illustration Max Lex Nika Vlad Double-linked list

Each list node holds data and a pointer to the next and previous list node. In the last node the pointer to the next node is NULL. In the first node the pointer to the previous node is NULL. Double-Linked List: Data SomeData Value; Node* Next; Node Node* Previous;

Double-linked list #include list myList; size() returns list size; insert() / erase() inserts / removes element at specified pos; push_front() adds element at the beginning; push_back() adds element at the end; pop_front() removes element at the beginning; pop_back() removes element at the end; begin() / end() returns position of the first / last element; end() returns position of the last element. Iterator - iterator for the list ++ next element in the list -- previous element in the list * current element Generic list Class (STL)

#include using namespace std; list myList; for ( list ::iterator pos = myList.begin(); pos != myList.end(); ++pos ) { // Current element string s = *myList; } list Traversal Example

// Prefix: ++iter; // Modifies self and returns the NEW value iterator& operator++(); // Postfix: iter++; // Modifies self, but returns a copy of OLD self created prior to the modification iterator operator++(int); for (; ; ++pos ) // More efficient (no copying) for (; ; pos++ ) // Equivalent to prefix++ in this loop MyFunc(pos++); // Firtst MyFunc is called then pos++ MyFunc(++pos); // First pos++ then MyFunc is called Postfix / Prefix ++ Operator

Using string and list -Read 5 strings from cin -store them in the list in alphabetically sorted order – HOW?? -Print the list (which must be sorted) TIP: You can use, = operators for string comparison. Todays Lab

Read chapter 4, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.Assignment