Loading....
Coupon Accepted Successfully!

 

Vectors

A vector is a more useful container. Similar to an array, it also stores elements in neighboring memory locations. Each element can be directly accessed using the operator[]. A vector is able to enlarge its size if an extra element is assigned to it. A class vector has various constructors that can be used to create vector objects.

The vector class supports various functions and they are listed in Table 20.7.

Table 20.7 Functions of vector class

Function

Use

swap( )

Swap the elements in the given two vector

size( )

Provides the number of elements

resize( )

Changes the size of the vector

push_back( )

Appends an element to the end

pop_back( )

Erases the last elements

insert( )

Inserts items (elements) in the vector

erase( )

Erases given elements

end( )

Provides reference to the end of the vector

empty( )

Determine whether the vector is empty

clear( )

Erases entire elements from the vector

capacity( )

Provides the present capacity (limit) of the vector

begain( )

Provides a reference to the starting element

back( )

Provides a reference to the last element

at( )

Provides a reference to an element

 

20.4 Write a program to add and display elements in the vector object.

 

#include<iostream>

#include<vector>

using namespace std;

int main()

{

vector<int> e;

e.push_back(5);

e.push_back(8);

e.push_back(9);

cout<<“\n The elements are : ”<<“\n”;

cout<<e[0]<<“”;

cout<<e[1]<<“”;

cout<<e[2]<<“”;

return 0;

}

 

OUTPUT

The elements are :

5 8 9

 

 

Explanation: In the above program, e is an object of the vector class that can hold data of integer type. The member function push_back() is used to add the element in the vector object. Three integer elements 5, 8, and 9 are added to the vector. These elements are stored in contiguous memory locations. These elements can be accessed in the same manner as we access the array elements. The vector object name followed by the element number in the [ ] operator displays the specified number. Here, the overloaded operator [ ] is used.

 

20.5 Write a program to insert an element in the vector object. Use member function and iterator.

#include<iostream>

#include<vector>

using namespace std;

int main()

{

vector<int> e;

for (int x=0;x<3;x++)

e.push_back(x+1);

cout<<“\n The elements are:”<<“\n”;

for (x=0;x<3;x++)

cout<<e[x]<<“”;

vector <int> :: iterator it =e.begin();

it=it+1;

e.insert(it,7);

cout<<“\n The elements after insertion:”<<“\n”;

for (x=0;x<4;x++)

cout<<e[x]<<“”;

return 0;

}

OUTPUT

The elements are :

1 2 3

The elements after insertion :

1 7 2 3 Press any key to continue

 

 

 

Explanation: In the above program, the first for loop and the function push_back() add elements to the object e. The elements indicated by the loop variable x are stored in the object e. The second for loop and cout() statement display the contents of the object e.

The it is an iterator. The statement vector <int> :: iterator it = e.begin() declares the iterator. The iterator acts similar to a pointer. The begin() function is used to assign the reference (address) of the first element. The statement it = it+1 assigns the address of the second element where the element is to be inserted. The member function is invoked with two arguments as per the g statement e.insert (it, 7).

The first argument is an iterator that indicates the location, and the second argument is an integer value. The number 7 is inserted at the location indicated by the iterator it. The last for loop displays the contents of the vector object e after insertion.

 

20.6 Write a program to display the elements of vector object in ascending and descending orders.

#include<iostream>

#include<vector>

using namespace std;

int main()

{

vector<int> e;

cout<<“Elements in ascending order:”;

for (int x=0;x<8;x++)

{

e.push_back(x+1);

cout<<x+1 <<“”;

}

int s=e.size()-1;

cout<<“\n Elements in descending order:”;

for (int j=s;j>=0;j--)

cout<<e[j]<<“”;

cout<<“\n”;

return 0;

}

 

OUTPUT

Elements in ascending order : 1 2 3 4 5 6 7 8

Elements in descending order : 8 7 6 5 4 3 2 1

 

 

Explanation: In the above program, the first for loop and the member function push_back() are used to add numbers in the vector element e. The cout() statement in the same loop displays the elements. All the elements are stored and displayed in ascending order. The member functionsize() returns the total number of the elements. The total number of the elements is stored in the integer variable s. The second for loop is executed in reverse order, and the value of the loop variable j is used with the object e to display the number. Thus, all the numbers are displayed in the reverse order.

 

20.7 Write a program to remove elements from vector object.

#include<iostream>

#include<vector>

#include<math.h>

using namespace std;

int main()

{

vector<float> e;

cout.precision(2);

cout<<“\n Original elements:”;

for (int k=5;k<12;k++)

{

e.push_back(sqrt(k));

cout<<sqrt(k)<<“ ”;

}

vector <float> ::iterator ir=e.begin();

e.erase (ir+3,ir+5);

int s=e.size();

cout<<“\n The elements after erase():”;

for (k=0;k<s;k++)

cout<<e[k]<<“ ”;

cout<<“\n”;

return 0;

}

OUTPUT

Original elements : 2.2 2.4 2.6 2.8 3 3.2 3.3

The elements after erase() : 2.2 2.4 2.6 3.2 3.3

 

 

 

Explanation: In the above program, e is an object of the vector that is capable of storing float values. The square roots of 5 to 12 numbers are stored in the object e. The erase() function erases the 4th and 5th elements. The variable ir is an iterator that gives the reference of the element. The erase() can be used with one or more argument. The last for loop displays the elements after deletion.

 

20.8 Write a program to declare a vector object. Add elements and display them. Use member functions.

#include<iostream>

#include<vector>

using namespace std;

{

for (int j=0;j<e.size();j++)

{ cout<<e[j];

cout<<“\t”;

}

}

int main()

{

vector<int> e;

cout<<“Initial size of e= <<e.size() <<“\n”;

int k;

cout<<“\n Enter five integers values:”;

for (int j=0;j<3;j++)

{

cin>>k;

e.push_back(k);

}

cout<<“ Size of e after adding 3 values:”;

cout<<e.size();

cout<<“\n current contents:”<<“\n”;

show(e);

return 0;

}

 

OUTPUT

Initial size of e= 0

Enter five integers values : 7 3 4

Size of e after adding 3 values : 3

Current contents :

7 3 4

 

 

Explanation: In the above program, e is an object of the vector class that is capable of storing integer values. The size() member function gives the number of elements stored in the object e. The for loop and the cin statement within it reads three integers through the keyboard. The member function push_back() adds the element to the object e. It adds the element at the end. After adding elements again, the size() function displays the number of elements. It displays 3, that is the number of total elements added in the object e. The show() function is used to display the contents of the object e. The for loop and the statement cout <<e[j] are used to display the elements. The overloaded operator [ ] is used to access the specified element by the variable j.





Test Your Skills Now!
Take a Quiz now
Reviewer Name