STL Vector - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

STL Vector

Description:

... elements by key, such as name, social security number, or part number. set, multiset, map, multimap. STL Vector API. The STL container Vector - API. Constructors ... – PowerPoint PPT presentation

Number of Views:94
Avg rating:3.0/5.0
Slides: 14
Provided by: yxie
Category:
Tags: stl | multimap | vector

less

Transcript and Presenter's Notes

Title: STL Vector


1
STL Vector
  • Dr. Ying Xie

2
STL Introduction
  • The STL (Standard Template Library) consists of
    10 container classes with three categories
  • sequence containers
  • stores data by position in linear order
  • vector, deque, list
  • adapter containers
  • contains another container as its underlying
    storage structure
  • stack, queue, priority_queue
  • associative containers
  • stores elements by key, such as name, social
    security number, or part number
  • set, multiset, map, multimap

3
STL Vector API
  • The STL container Vector - API
  • Constructors
  • vector()
  • vector(int n, const T value T())
  • vector(T first, T last)
  • initialize the vector using the address range
    first, last)
  • Operations (p.201)
  • T back() // return the value of the item at the
    rear
  • const T back() const // constant version of
    back()
  • bool empty() const
  • T operator (int i)
  • const T operator (int i) const // constant
    version
  • void push_back(const T value)
  • void pop_back()
  • void resize(int n, const T fill T())
  • int size() const

4
The STL container Vector - constructor
  • The first version of constructors
  • vector()
  • no argument is included
  • the resulting vector is empty, with size of 0
  • vectorltdoublegt dblVector
  • vectorltstringgt v

5
The STL container Vector - constructor
  • The second version of constructors
  • vector(int n, const T value T())
  • with one argument of size with the default value
    of T()
  • vectorltintgt intVector(5) // initial value is
    int()
  • vectorltstringgt strVector(10) // all
  • with two arguments of size and initial value
  • vectorltchargt line(80, ) // all bland char
  • vectorltAccountgt accounts(7, Account(, , 0))

6
The STL container Vector - constructor
  • A third version of the vector constructor
  • vector(T first, T last)
  • initialize its values from an array
  • int intArray5 9, 2, 7, 3, 12
  • vectorltintgt intVector(intArray, intArray5)
  • the array address
  • the array name intArray is the address of
    intArray0
  • intArray1 is the address of intArray1
  • a generic code
  • int intArray 9, 2, 7, 3, 12
  • int arrSize sizeof(intArray)/sizeof(int)
  • vectorltintgt intVector(intArray,
    intArrayarrSize)

7
The STL Vector - Adding and Removing
  • Operations to modify the rear of the vector
  • push_back()
  • adds an element of type T at the rear of the
    vector
  • it grows the size of the vector
  • cout ltlt v.back() // assume it is u
  • v.push_back(w)
  • v.back() y
  • pop_back()
  • deletes the last element in the vector at index
    (size()-1)
  • reduces the size by 1

8
The STL container Vector - Resizing
  • The function void resize(int n, const T fill
    T())
  • uses the argument n to change the size of the
    vector and
  • uses the second argument to be the fill value
  • if the new size is greater than the current size
  • the vector grows by adding n-size() new elements
  • all of the exiting elements remain fixed
  • the new elements are assigned the fill value
  • if the new size is less than the current size
  • it chops off elements at the tail of the vector
  • the first n elements remain fixed
  • the vector discards the other size()-n elements

9
Example of using STL vector
  • include ltiostreamgt
  • include ltvectorgt
  • using namespace std
  • int main()
  • vectorltintgt v1
  • cout ltlt "the size of v1 at t0 is
    "ltltv1.size()ltltendl
  • v1.push_back(1)
  • v1.push_back(2)
  • v1.push_back(3)
  • cout ltlt "the size of v1 at t1 is " ltlt
    v1.size() ltltendl
  • for (int i0 ilt v1.size() i)
  • cout ltlt v1i ltlt " "
  • cout ltlt endl
  • cout ltlt v1.back() ltltendl

10
Join one vector to another
  • template lttypename Tgt
  • void join (vectorltTgt vA, const vectorltTgt vB)
  • // capture the size of vB in sizeB
  • int i, sizeB vB.size()
  • // use index i to access the elements of vB and
    push_back()
  • // to add the elements to the rear of vA
  • for (i 0 i lt sizeB i)
  • vA.push_back(vBi)

11
Insertion Sort (1) - Illustration
pass 1
5
2
6
1
2
5
6
1
pass 2
2
5
6
1
2
5
6
1
pass 3
2
5
6
1
1
2
5
6
12
Insertion Sort (2)
  • template lttypename Tgt
  • void insertionSort(vectorltTgt v)
  • int i, j, n v.size()
  • T target
  • for (i 1 i lt n i)
  • j i
  • target vi
  • while (j gt 0 target lt vj-1)
  • vj vj-1
  • j--
  • vj target

13
Insertion Sort (3)
  • Time Complexity
  • T(n) 1/2 2/2 3/2 (n-2)/2 (n-1)/2
    n(n-1)/4
  • O(n2)
Write a Comment
User Comments (0)
About PowerShow.com