Vectors Adt
Vectors Adt
ADS_RV 1
Common Container Methods
• The following are the common
methods for containers.
– int size() const
• Return number of elements in container
– void clear()
• Remove all elements from container
– bool empty()
• Return true is container has no elements, otherwise
returns false
ADS_RV 2 2
Vector and List Methods- which operates in
constant time
ADS_RV 3 3
Vector-only Methods
• Object & operator[] (int idx)
– Return object at index idx in vector with no bounds-checking
• Object & at (int idx)
– Return object at index idx in vector with bounds-checking
• int capacity () const
– Return internal capacity of vector
• void reserve (int newCapacity)
– Set new capacity for vector (avoid expansion)
ADS_RV 4 4
Iterators
• Represents position in container
• Getting an iterator
– iterator begin ()
• Return appropriate iterator representing first item in
container
– iterator end ()
• Return appropriate iterator representing end marker in
container
• Position after last item in container
ADS_RV 5 5
Iterator Methods or operations
• itr++ and ++itr
– Advance iterator itr to next location
• *itr
– Return reference to object stored at iterator itr’s location
• itr1 == itr2
– Return true if itr1 and itr2 refer to same location; otherwise
return false
• itr1 != itr2
– Return true if itr1 and itr2 refer to different locations; otherwise
return false
ADS_RV 6 6
Constant Iterators- iterators which is
only used to read the container
• iterator begin ()
• const_iterator begin () const
• iterator end ()
• const_iterator end () const
• Appropriate version above returned based on
whether container is const
• If const_iterator used, then *itr cannot
appear on left-hand side of assignment (e.g.,
*itr=0 )
ADS_RV 7 7
Vector/List Class Big Five
• Vector/List class has Big Five Implementation:
A vector class has to provide implementation
for following:
1. Constructors: Default, parameterized,
2. Copy Constructors
3. Move Constructors
4. Operator = overloaded to perform deep copy
semantics
5. Destructor to delete objects
ADS_RV 8
Simple IntCell class : Big Five
ADS_RV 9
Implementing Vector class
ADS_RV 10
ADS_RV 11
ADS_RV 12
ADS_RV 13
Using vector class for creating lists
ADS_RV 14