0% found this document useful (0 votes)
100 views14 pages

Vectors Adt

Vectors provide a growable array implementation of lists. They allow index-based access to elements in constant time O(1) but insertion and deletion takes O(n) time. The vector class is defined in the STL library header <vector> and implements common container methods like size(), clear(), and empty() as well as vector-specific methods like at() and capacity(). Iterators represent positions in containers and are used to traverse elements. Vector and list classes must implement the big five - default constructor, copy constructor, move constructor, copy assignment, and destructor.

Uploaded by

veda shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views14 pages

Vectors Adt

Vectors provide a growable array implementation of lists. They allow index-based access to elements in constant time O(1) but insertion and deletion takes O(n) time. The vector class is defined in the STL library header <vector> and implements common container methods like size(), clear(), and empty() as well as vector-specific methods like at() and capacity(). Iterators represent positions in containers and are used to traverse elements. Vector and list classes must implement the big five - default constructor, copy constructor, move constructor, copy assignment, and destructor.

Uploaded by

veda shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Vectors

• Vector provides a growable array implementation of List ADT.


• Indexable in Constant time O(1).
• Inserting and deleting takes O(n).
• Printing the vector contents takes O(n) time.
• Vector is used to implement Lists using arrays.
• Vector class is present in STL library. <vector>
• <vector> header file defines lot of functions for implementing
vectors.
• <vector> and <list> are implemented using class templates.
• The list is always Doubly Linked list implementation of List
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

• void push_back (const Object & x)


– Add x to end of list
• void pop_back ()
– Remove object at end of list
• const Object & back () const
– Return object at end of list
• const Object & front () const
– Return object at front of list

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

You might also like