STL list - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

STL list

Description:

list T ::iterator iter = first; // compare list elements with item until either ... return iter; STL List API (continued) Iterator begin ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 21
Provided by: yxie
Category:
Tags: stl | iter | list

less

Transcript and Presenter's Notes

Title: STL list


1
STL list
  • Ying Xie

2
  • Why list (1)
  • Vector
  • A good data structure when an application
    needs a dynamic list with direct access to the
    elements.
  • Not an efficient data structure when an
    application needs to frequent insertion and
    deletion of items at an arbitrary position in the
    sequence.

Contiguous Block
vector
15
20
30
35
40
0 1 2 3 4 Additional space
Model of a vector
3
  • Why list (2)
  • List
  • Specially designed to store elements
    sequentially and still allow for very efficient
    adding and removing of items at any position.

list
front
back
Model of a list
4
  • The STL List - constructor
  • three constructors
  • default constructor creates an empty list
  • one version takes a size n and an value of type T
    as arguments and creates a list with n elements
    all set to the specified value
  • another version initializes the list object with
    distinct values
  • declares an array with the values and then passes
    the address range of the array as arguments

5
  • The STL List constructor
  • listltintgt intList
  • listltAccountgt accountList(6, Account(, , 0))
  • string strArr3 array, vector, list)
  • listltstringgt strList(strArr, strArr3)

6
  • All STL containers share a common set of member
    functions
  • copy constructor
  • destructor
  • overloaded assignment operator
  • size()
  • empty()
  • listltstringgt newList strList
  • cout ltlt newList.size() // output 3

7
  • Add and Remove elements from a list.
  • List allow adding or removing elements at the
    both ends of the sequence
  • push_front() add element at the front of the list
  • pop_front() delete element at the front of the
    list
  • front() is used to access or update the value of
    the first element
  • push_back() add element at the back of the list
  • pop_back() delete element at the back of the list
  • back() is used to access or update the value of
    the last element
  • General insertion and deletion at intermediate
    positions needs the understanding of iteration

8
  • STL list API (to Continue)
  • include ltlistgt
  • Constructor
  • list()
  • list(int n, const T value T())
  • list(T first, T last)
  • Operations
  • T back()
  • bool empty() const
  • T front()
  • void push_back(const T value)
  • void pop_back()
  • void push_front(const T value)
  • void pop_front()
  • int size() const

9
List Iterator (1) Problem A list has no index
Except for the front and the back, how do we
access an element at an intermediate
position. However, the items in a list contain
pointers that identify both the next and the
previous elements in the sequence. So we scan the
list in both directions through those pointers.
The iterator is such a scanner that slides
back and forth in the list. At any point in the
list, the iterator can access the value of the
corresponding element.
10
List Iterator (2) Interator is an object which
encapsulates a pointer pointing to a node of a
list. Interator class (a nested class of class
list) defines the following operations to ease
the operation on that encapsulated pointer.
accesses the value of the element referenced
by the iterator resets the iterator to
point at the next element - - resets the
iterator to point at the previous element
compare two iterator objects. It returns true if
both point to the same item in the list. !
compare two iterator objects. It returns true if
both do NOT point to the same item in the list.
11
Declaring and Using Iterators. int arr5
2,7,3,9,5 int arrSize sizeof (arr) /
sizeof (int) listltintgt intList (arr, arr
arrSize) listltintgtiterator iter
inList.begin() cout ltlt iter iter
cout ltlt iter iter inList.end()
iter -- cout ltlt iter
12
Constant Iterators (1) STL list class has a
second iterator, called constant iterator.
-- The programmer cannot apply the dereference
operator, , to a constant iterator on the left
side of an assignment statement. -- List
ltTgtconst_iterator iter -- Rules Use
a constant iterator to access and scan a constant
list. Use a nonconstant iterator to access and
scan a nonconstant list.
13
  • Const Iterator (2) - example

template lttypename Tgt void writeList(const
listltTgt alist, const string separator "
") listltTgtconst_iterator iter for (iter
alist.begin() iter ! alist.end()
iter) cout ltlt iter ltlt separator cout ltlt
endl
14
  • Sequential Search of a List
  • Sequential search of an array
  • template lttypename Tgt
  • int seqSearch (const T arr , int first,
    int last, const T target)
  • Sequential search of an vector.
  • template lttypename Tgt
  • int seqSearch (cost vector ltTgt V, int
    first, int last, cost T target)
  • Sequential search of an list.
  • template lttypename Tgt
  • list ltTgtiterator sequSearch (list
    ltTgtiterator first, list ltTgtiterator last,
    const T target)

15
  • Sequential Search of a List
  • template lttypename Tgt
  • listltTgtiterator seqSearch(listltTgtiterator
    first, listltTgtiterator last, const T target)
  • // start at location first
  • listltTgtiterator iter first
  • // compare list elements with item until either
  • // we arrive at last or locate item
  • while(iter ! last !(iter target))
  • iter
  • // iter either points at item or is last
  • return iter

16
  • STL List API (continued)
  • Iterator begin ( )
  • Returns an iterator that references the first
    position of the list
  • Const_iterator begin ( )
  • Returns a const_iterator that points to the
    first position (front) of a constant list.
  • Iterator end ( )
  • Returns an iterator that signifies a location
    immediately out of the range of actual elements.
  • Const_iterator ends ( )
  • Returns a const_iterator that signifies a
    location immediately out of the range of actual
    elements in a constant list.

17
  • List insert and erase operations (1)
  • Recall The desire to have a sequence
    container that allows for efficient insertion and
    deletion of elements is the motivation behind the
    design of the list container.
  • The STL list class provides insert ( ) and
    erase ( ) functions to add or remove an element
    at an position pointed to by a iterator.

18
  • List insert and erase operations (2)
  • int arr5 2,7,3,9,5
  • int arrSize sizeof (arr) / sizeof (int)
  • listltintgt intList (arr, arr arrSize)
  • list ltintgtiterator iter, newElt
  • iter intList.begin ( )
  • iter
  • newIter intList.insert (iter,4)
  • cout ltlt newIter ltlt ltlt iter

List object (before insertion)
List object (after insertion)
2
7
3
9
5
9
3
7
4
2
5
front
back
front
back
iter
iter
newIter
4
19
  • List insert and erase operations (3)
  • iter intList.begin ( )
  • iter
  • intList.erase (iter)
  • cout ltlt iter

List object (before erase)
List object (after erase)
2
7
3
9
5
9
3
2
5
front
back
front
back
??
iter
iter
20
  • STL List API (continued)
  • Void erase (iterator pos)
  • Erase the element pointed to by pos.
  • Void erase (iterator first, iterator last)
  • Erase all list elements within the iterator
    range first, last).
  • Iterator insert (iterator pos, const T value)
  • Insert value before pos, and return an
    iterator pointing to the position of the new
    value in the list. The operation does not affect
    any existing iterators.
Write a Comment
User Comments (0)
About PowerShow.com