Vectors.

Slides:



Advertisements
Similar presentations
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
Advertisements

Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory.
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
Standard Containers: Vectors
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
Standard library types Practical session # 3 Software Engineering
Templates and the STL.
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:
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
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.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007.
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);
Templates&STL. Computer Programming II 2 Introduction They perform appropriate operations depending on the data type of the parameters passed to them.
Templates “Generic Programming” ECE Templates A way to write code once that works for many different types of variables –float, int, char, string,
A gentle introduction to the standard template library (STL) Some references:
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.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th, 2001.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
 2003 Prentice Hall, Inc. All rights reserved vector Sequence Container Declarations –std::vector v; type : int, float, etc. Iterators –std::vector.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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.
The Ohio State University
C++ Standard Template Library
“Generic Programming” ECE 297
Pointers & Arrays.
Vectors Holds a set of elements, like an array
CSCE 210 Data Structures and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
Vectors and Grids Chris Piech CS 106B Lecture 2 Jan 9, 2015.
CS Computer Science IB: Object Oriented Programming
Dynamic Memory CSCE 121 J. Michael Moore.
Collections Intro What is the STL? Templates, collections, & iterators
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
ADT Implementations: Templates and Standard Containers
Object Oriented Programming COP3330 / CGS5409
Compound Data CSCE 121 J. Michael Moore.
Vectors the better arrays.
Lecture 13 – Heaps Container Adapters priority_queue – impl. with heap
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
L2. Necessary Java Programming Techniques
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
STL: Traversing a Vector
Standard Version of Starting Out with C++, 4th Edition
L2. Necessary Java Programming Techniques
The Standard Template Library
Arrays.
ArrayLists 22-Feb-19.
Dynamic Objects.
CS 144 Advanced C++ Programming February 7 Class Meeting
Pointers & Arrays.
Collections Intro What is the STL? Templates, collections, & iterators
CS 113: Data Structures and Algorithms
ASEE / GradSWE C++ Workshop
Implementing a Priority Queue
Chapter 3 Lists, Stacks, and Queues
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Vectors the better arrays.
An Introduction to Programming though C++
Presentation transcript:

Vectors

Vectors Vectors are containers of objects of the same type that are stored contiguously in memory. In the <vector> library. Syntax: vector<double> vect(10); // initialize vector of 10 doubles.

Example Vector Usage vector<int> v(10); v.push_back(4); v.pop_back(); v.push_back(1); cout << v[0] << v[1];

Vector Operations Assume that v is a vector. - v.front() // return the first element - v.back() // return the last element - v[i] // return the element at index i - v.at(i) // return the element at index i - v.push_back(x) // add element x to the end, increase size if necessary - v.pop_back() // remove the element, reduce size by 1

Print a Vector void printVec(const vector<int> &vec) { for (int i=0; i < vec.size(); i++) cout << vec.at(i) << “ “; cout << “\n”; }

Out of Bounds Errors Accessing a vector outside of the allocated space leads to undefined behavior. vector<int> vec; for(int i=0; i<5; i++) vec.push_back(i*2+1); cout << vec.at(5);

Size vs. Capacity v.size() is the number of elements in v v.capacity() is the number of elements that can be stored in v in the space that is currently allocated

Insert into a vector Since a vector is stored contiguously in memory, if we want to insert at a specific position, we need to shift everything that comes after it over 1 position. void insert(vector<int> &vec, int pos, int x) { int n = vec.size(); vec.push_back(0); for (int i = n; i > pos; i--) vec[i] = vec[i-1]; vec[pos] = x; }