[go: up one dir, main page]

0% found this document useful (0 votes)
33 views8 pages

Data Structure and Object-Oriented Programming: Vectors in C++

Vectors in C++ are dynamic arrays that can resize automatically. They allow flexible insertion and deletion of elements at different positions compared to static arrays. Common vector operations include push_back, pop_back, insert, erase, swap and clear to modify a vector, and size, capacity, empty and iterators like begin and end for accessing vector elements.

Uploaded by

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

Data Structure and Object-Oriented Programming: Vectors in C++

Vectors in C++ are dynamic arrays that can resize automatically. They allow flexible insertion and deletion of elements at different positions compared to static arrays. Common vector operations include push_back, pop_back, insert, erase, swap and clear to modify a vector, and size, capacity, empty and iterators like begin and end for accessing vector elements.

Uploaded by

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

Data Structure and Object-Oriented

Programming

Vectors in C++
• Vectors in C++ are the dynamic
arrays that are used to store data.
• Unlike arrays, which are used to
store sequential data and are static
in nature, Vectors provide more
flexibility to the program. 
Introduction • Vectors can resize
automatically when an element is
itself

inserted or deleted depending on


the need of the task to be executed.
• It is not the same in an array where
only a given number of values can
be stored under a single variable
name.
In vectors, data is inserted at the end.
Inserting at the end takes differential time, as
sometimes there may be a need of extending
the array
Removing the last element takes only constant
time because no resizing happens
Introduction

Inserting and erasing at the beginning or in the


middle is linear in time
Modifiers
• push_back(): The function pushes the elements into a vector from the back. If the type of object passed as a
parameter in the push_back() is not same as that of the vector an exception is thrown.
• assign(): It assigns a new value to the vector elements by replacing old ones.
• pop_back(): The pop_back() function is used to pop or remove elements from a vector from the back. It
reduces the size of the vector by one element.
• insert(): This function inserts new elements before the element before the position pointed by the iterator.
We can also pass a third argument count, that counts the number of times the element is to be inserted
before the pointed position.
• erase(): erase() function is used to remove elements from a container from the specified position or range.
We can either pass the position of the specific elements needs need to remove or we can pass the starting
point and endpoint of a range of elements. 
• swap(): swap() function is used to swap the contents of one vector with another vector of the same type.
Sizes may differ.
• clear(): clear() function is used to remove all the elements of the vector container
Iterators
• begin(): This function returns an iterator pointing to the first element
in the vector.
• end(): The end() function returns an iterator pointing to
the last element in the vector.
Capacity
• size(): This function returns the number of elements in the vector.
• max_size(): The max_size() function returns the maximum number of elements that
the vector can hold.
• capacity(): The capacity() function returns the size of the storage space currently
allocated to the vector expressed as number of elements based on the memory
allocated to the vector.
• resize(): This function resizes the container so that it contains ‘n’ elements. If the
current size of the vector is greater than n then the back elements are removed from
the vector and id the current size is smaller than n then extra elements are inserted
at the back of the vector.
• empty(): Returns whether the container is empty, it return true if vector is empty
else returns false.
• Vector is dynamic array, where you can add a new element on the go
• Vector is a LIST? What is List – List is type of Data Structure
• LIST – Is a data structure
• Pushback an element
• Popback an element
• Insert an element
• Swap two different list
• Clear or delete a list
• Access different element of a LIST
• SEQUENCE in a LIST is important and its address

You might also like