Vectors CSCE 121 J. Michael Moore.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

C Language.
Introduction to arrays
Chapter 6 Data Structures
Chapter 7:: Data Types Programming Language Pragmatics
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
Pointers OVERVIEW.
Recap Visual Perception and Data Visualization Types of Information Display Examples of Diagrams used for Data Display Planning Requirement for Data Visualization.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
CSC 533: Programming Languages Spring 2016
“Generic Programming” ECE 297
CSC 533: Programming Languages Spring 2015
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Motivation and Overview
EKT120 : Computer Programming
C Tutorial (part 5) CS220, Spring 2012
CSE 303 Concepts and Tools for Software Development
Numeric Arrays Numeric Arrays Chapter 4.
STL Common tools for C++.
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Pointers and References
Dynamic Memory CSCE 121 J. Michael Moore.
This pointer, Dynamic memory allocation, Constructors and Destructor
Collections Intro What is the STL? Templates, collections, & iterators
C++ Data Types Simple Structured Address Integral Floating
Pointers and References
Operator Overloading CSCE 121 J. Michael Moore
C-Programming, continued
C Passing arrays to a Function
Visual Basic .NET BASICS
Computer Organization & Compilation Process
Introduction to the Standard Template Library
Compound Data CSCE 121 J. Michael Moore.
Linked List Intro CSCE 121 J. Michael Moore.
Data Representation Bits
Array & Pointers CSCE 121 J. Michael Moore.
EKT150 : Computer Programming
Return by Reference CSCE 121 J. Michael Moore.
Pointers and References
COMP26120: Algorithms and Imperative programming
EKT120: Computer Programming
STL and Example.
Arrays ICS2O.
STL: Traversing a Vector
Vectors.
Bingo Example: Design (Print Card Algorithm)
Essential Class Operations
Destructor CSCE 121 J. Michael Moore.
Polymorphism & Pointers
EECE.2160 ECE Application Programming
Exam 2 Exam 2 Regrading Average: 69 TA: Fardad
EECE.2160 ECE Application Programming
TUTORIAL 7 CS 137 F18 October 30th.
Java SE 7 One and Multi Dimensional Arrays Module 6
Data Structures and Algorithms Introduction to Pointers
Python: Sorting – Selection Sort
COP 3330 Object-oriented Programming in C++
EECE.2160 ECE Application Programming
CIS 110: Introduction to Computer Programming
EECE.2160 ECE Application Programming
Computer Organization & Compilation Process
point when a program element is bound to a characteristic or property
Collections Intro What is the STL? Templates, collections, & iterators
Linked List Intro CSCE 121.
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
Vectors CSCE 121.
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Presentation transcript:

Vectors CSCE 121 J. Michael Moore

Group of homogeneous elements accessed by an index. Array/Vector Group of homogeneous elements accessed by an index. Vector access with ary.at(index) Bounds checking Array and Vector access with ary[index] No bounds checking

Array Really a pointer Variable name is a pointer to the first element For ary[2] Conceptually: address of ary + 2*sizeof(array datatype) Actually: ary + 2 (math is done automatically) Pointer arithmetic Must know size of array at compile time Allows access to elements outside of array bounds Security problem

Vector Abstraction (An array on the heap.) Preferred over arrays If you use .at() Bounds checking Prevents access outside array bounds Resizable #include <vector>

Vector Must say what type it is e.g. vector<int> Define size at beginning vector<int> vec = vector<int>(SIZE); vector<int> vec = vector<int>(SIZE,INIT_VAL); Let vector grow as needed vector<int> vec; vec.push_back(7); Increases size by one

Vector vector<int> vec; vec.push_back(7); vec.erase(vec.begin()+index); vec.at(index); // range checking & harder to read vec[index]; // no range checking & easier to read

Multi-dimensional Vectors A vector of vectors vector< vector<int> > ary2d; Access ary2d.at(i).at(j) // safer ary2d[i][j]