Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A vector is an Abstract Data Type ( ADT ) that offers the following functions: size ( ) empty ( ) at ( i )

A vector is an Abstract Data Type (ADT) that offers the following functions: size ()
empty ()
at (i) returns the element at index i set (i, e) replaces the element at index with value e insert (i, e) inserts value e at index i erase (i) removes the element at index i
Other C++ STL containers are the following: list stack queue priority_queue deque set map
STL vector is a C++ container, which is a collection of elements.
#include
using std::vector
vector myVector (100);
STL vector is similar to a C++ array with the following additions:
1. uses indexing ([]) to access vector elements but checks if index is within the array
index range.
2. STL vectors can be dynamically resized to add or remove elements.
3. STL destruction automatically invokes the destructor for each of the elements.
4. Offers functions that operate on the entire vector (not just elements), like copying
and comparing entire vectors, or inserting and erasing multiple elements.
Methods: vector (n) : constructor. Creates a vector of n elements. An empty vector is
created if no argument is present. size () : returns the number of elements in the vector.
Page 2
empty () : true if the vector is empty.
resize (n) : resizes the vector so that it can hold n elements.
reserve (n) : allocated storage space should be able to hold n elements.
at (i) : returns a reference to the element at index i but also checks if index i is
within the correct bounds of the vector.
front () : returns a reference to the first element of the vector.
back () : returns a reference to the last element of the vector.
push_back (e) : adds a copy of element e at the end of the vector.
pop_back () : removes the last element of the vector.
Use the C++ STL vector to solve the following two exercises:
1. Devise a C++ program that uses each of the above methods for a vector of
integers. Display the results after every method call.
2. Implement bubble sort to sort the vector of integers.
3. Given N decimal numbers, find the mean, maximum, minimum. Your program
will first ask for N, the number of integers in the list, which the user will input.
Then the user will input N more numbers. Use vector to store the numbers.
Submit the main program with comprehensive tests.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions