Vectors - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Vectors

Description:

The vector container resembles a C array in that it holds zero or more ... #include vector.h should not be used as it is deprecated. Vector Declaration(1) ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 25
Provided by: Wael9
Category:

less

Transcript and Presenter's Notes

Title: Vectors


1
Vectors
  • CSE 1341Wael Kdouh

2
Agenda
  • Go Through The Exercise Solution
  • Introduction to Vectors

3
Exercise
  • Write a function to return the sum of the two
    largest elements in an array.

4
Overview
  • Definition of Vectors
  • Vectors VS Arrays
  • Some Vector Operations

5
Note
  • For consistency, through the presentation I am
    always going to assume that I have a variable
  • myVector of type vector.

6
What Is A Vector?
  • The vector container resembles a C array in
    that it holds zero or more objects of the same
    type, and that each of these objects can be
    accessed individually.

7
When To Use Vectors?
  • Vectors are good when we have an unknown sequence
    of similar items to store and we want to access
    them by their sequence numbers.

8
How Can We Use Vectors?
  • Vectors are part of the STL.
  • You can use it by including the following into
    your program include ltvectorgt
  • include ltvector.hgt gt should not be used as
    it is deprecated.

9
Vector Declaration(1)
  • In order to declare a vector variable you use the
    following syntaxvectorlttypegt myVector
    (initial_size)
  • -lttypegt could be integer, double, float,char,
    or any other supported type.
  • -initial_size is the initial size( could be
    extended throughout the lifetime of
    the program).
  • -myVector is the name of the variable.
  • Example
  • vectorltdoublegt myVector()
  • Compared to
  • double myArray10

Vector
Array
10
Vector Declaration(2)
  • Here are some different ways to declare vectors
  • - vectorltintgt myVector(100, 1) This will
    declare a vector of size 100 with all
    of them initialized to 1.
  • - vectorltintgt myVector( const vector c )This
    will declare a vector that is a
    copy of the given vector c.

11
Vectors vs. Arrays(1)
  • Both serve as a way to collect elements of the
    same type .
  • Vectors are a recent addition to C, so many
    older programs use arrays instead.

12
Vectors vs. Arrays(2)
  • The array size must be set when the program is
    compiled. (You can't ask the user how many
    elements and then allocate a sufficient number).
    But with vectors the size can change at run time.
  • Example
  • const int size 100
  • int myArraysize
  • for (int i0 iltsize i) myArrayii
  • Same code using a vector
  • const int size 100
  • vectorltintgt myVector(size) //Only line
    that changed
  • for (int i0 iltsize i)
    myVectorsizei
  • myVector.resize(200)

Array
Vector
What is dangerous about this?
13
Question?
  • What if the new size is smaller than the number
    of elements in the vector?

14
Answer
  • All the additional elements are truncated.

15
Vector Operations(1)
  • myVector. back ()Returns a reference to the last
    element in the vector.
  • myVector.front() Returns a reference to first
    element of vector.
  • myVector.push_back() Appends (inserts) an
    element to the end of a vector,
    allocating memory for it if
    necessary.
  • myVector.pop_back() Erases the last element of
    the vector, possibly reducing
    capacity
  • Example
  • vectorltintgt myVector
  • for( int i 0 i lt 5 i )
  • myVector.push_back(i)
  • cout ltlt "The first element is " ltlt
    myVector.front()
  • ltlt " and the last element is " ltlt
    myVector.back() ltlt endl

How does it differ from vi?
16
Vector Operations(2)
  • myVector. size () Returns the number of
    elements in the current vector.
  • myVector.empty() Returns true if the vector's
    size is 0.
  • myVector. capacity () Returns the number of
    elements that the vector can hold before it will
    need to allocate more space.
  • Example
  • vectorltintgt v(10)
  • cout ltlt "The capacity of v is " ltlt
    v.capacity() ltlt endl
  • cout ltlt "The size of v is " ltlt v.size()
    ltlt endl
  • v.resize(4)
  • cout ltlt "The capacity of v is " ltlt
    v.capacity() ltlt endl
  • cout ltlt "The size of v is " ltlt v.size()
    ltlt endl

What is the output?
What about now?
17
Vector Operations(3)
  • What is wrong with this code?
  • vectorltintgt v( 5, 1 )
  • for( int i 0 i lt 10 i )
  • cout ltlt "Element " ltlt i ltlt " is " ltlt vi ltlt
    endl

18
Vector Operations(3.1)
  • Answer It overruns the end of the vector.
  • Solution Use the at operation.
  • vectorltintgt v( 5, 1 )
  • for( int i 0 i lt 10 i )
  • cout ltlt "Element " ltlt i ltlt " is " ltlt vi ltlt
    endl
  • The following code would be much safer
  • vectorltintgt v( 5, 1 )
  • for( int i 0 i lt 10 i )
  • cout ltlt "Element " ltlt i ltlt " is " ltlt v.at(i)
    ltlt endl

19
Vectors vs. Arrays(3)
  • When defining an array, you must guess on the
    maximum number of elements you need to store.
    With Vectors you DONT.
  • You must keep a constant to hold the capacity of
    the array. With Vectors you DONT, but rather you
    can use myVector.size()

20
Passing Vectors As Function Parameters(1)
  • Unlike arrays when you pass a vector by value it
    will not reflect the changes after you return
    from the function. You have to pass it by
    reference.
  • Example 1
  • vectorltdoublegt myVector(10,50)
  • ProcessInput(myVector)
  • coutltltmyVector0ltlt\tltltmyVector1
  • void ProcessInput( vectorltdoublegt invector)
  • invector0100

What will the output be after it returns from the
function
21
Passing Vectors As Function Parameters(2)
  • Example 2
  • vectorltdoublegt myVector(10,50)
  • ProcessInput(myVector)
  • coutltltmyVector0ltlt\tltltmyVector1
  • void ProcessInput( vectorltdoublegt invector)
  • invector0100

What about now?
22
Vectors vs. Arrays(4)
  • In spite the fact that vectors have a lot of
    advantages and flexibility over arrays there are
    some cases where arrays are still superior.
  • 1) When efficiency is vitally important.
  • 2) One can't afford the overhead of a huge
    library like vector. Example Embedded
    Systems.

23
Conclusion
  • Main advantages of vectors over arrays
  • 1)They can increase/decrease in size at
    run-time.
  • 2)They allocate memory from the free space
    when increasing in size.
  • 3)They can do range checking (using at()).

24
  • Thank You
Write a Comment
User Comments (0)
About PowerShow.com