Vectors. Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically.

Slides:



Advertisements
Similar presentations
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
Beginning C++ Through Game Programming, Second Edition
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Standard Containers: Vectors
 2006 Pearson Education, Inc. All rights reserved Templates.
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.
1 Standard Containers: Lists Gordon College Resource:
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.
Basic C++ Sequential Container Features
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
1 Software Design UML Basics Template Class Example (Chain) STL Vector Class Operator Overloading Ray class Example CSE Lecture 2 – Design & Some.
Templates and the STL.
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
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:
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
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.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Overview 18.1 Iterators 18.2 Containers 18.3 Generic Algorithms Slide
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan and A. Ranade.
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.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
The Standard Template Library Container Classes Version 1.0.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Standard Template Library a collection of useful tools.
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
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.
CS212: Object Oriented Analysis and Design
Overview 18.1 Iterators 18.2 Containers 18.3 Generic Algorithms.
Vectors Holds a set of elements, like an array
CSCE 210 Data Structures and Algorithms
Specifications What? Not how!.
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Collections Intro What is the STL? Templates, collections, & iterators
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Foundational Data Structures
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Collections Intro What is the STL? Templates, collections, & iterators
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
A dictionary lookup mechanism
Presentation transcript:

Vectors

Basics A vector is like an array, but more flexible A vector provides (constant time) random access to its elements A vector may be dynamically resized Constant time insertion or removal at the end Linear time ins/rem elsewhere

Example #include using namespace std; … vector v; assert(v.size == 0); v.insert(v.begin(), 3); assert(v.size() == 1);

Default constructor #include using namespace std; … /* create an empty vector v of specified type */ vector v;

Copy constructor #include using namespace std; … /* create an vector v1 which is a copy of v (types must be the same) */ vector v1(v);

Sized constructor #include using namespace std; … /* create an vector v which of specified size, using the default constructor */ vector v1(size);

Initialized constructor #include using namespace std; … /* create an vector v which of specified size, using the supplied value */ vector v1(size, value);

Range constructor #include using namespace std; … /* create an vector v of the specified size, us values all values from begin to end */ vector v(begin, end);

Removing elements v.clear();// deletes all elements v.erase(pos);// deletes element at pos

Inserting elements // insert val at pos v.insert(pos, val); // insert n copies of val at pos v.insert(pos, n, val); // a copy of v.beg.. v.end-1 is inserted // at pos v.insert(pos, beg, end);

Operations for all containers Default constructor /* Initialize the object to an empty state */

Parameterized constructors /* Various specialized ways to specify the initial contents of the container */

Copy constructors /* A container of the same type must be passed by value. */

destructor /* Executes when the container goes out of scope */

c.empty() /* Returns true if c is empty, false otherwise. */

c.size() /* Returns the number of elements in c. */

c.max_size() /* Returns the number of elements that can be inserted into c. */

c.swap(c1) /* Swaps the elements c and c1 */

c.begin() /* Returns an interator to the first element in c. */

c.end() /* Returns an interator succeeding the last element in c. */

c.rbegin() /* (reverse) Returns an interator pointing to the last element in c. */

c.rend() /* (reverse) Returns an interator pointing one past the first element in c */

c.insert(pos, val) /* Inserts val into pos (pos is an iterator) */

c.erase(begin, end) /* Deletes elements from begin to end-1 */

c.clear() /* Deletes all elements */

Operators /* =, ==, !=,, >=x */