0% found this document useful (0 votes)
37 views

Vectors and Matrices in STL

The Standard Template Library (STL) provides common data structures and functions like lists, stacks, arrays. It has 4 components - Algorithms, Containers, Functors, Iterators. Algorithms act on containers to perform operations on container elements. Containers like Vector, List, Deque store objects and data. Vectors are dynamically allocated arrays that can be accessed using indices. They support operations like push_back(), pop_back(), size(), capacity(), resize() etc. Matrices can be represented using a vector of vectors.
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)
37 views

Vectors and Matrices in STL

The Standard Template Library (STL) provides common data structures and functions like lists, stacks, arrays. It has 4 components - Algorithms, Containers, Functors, Iterators. Algorithms act on containers to perform operations on container elements. Containers like Vector, List, Deque store objects and data. Vectors are dynamically allocated arrays that can be accessed using indices. They support operations like push_back(), pop_back(), size(), capacity(), resize() etc. Matrices can be represented using a vector of vectors.
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/ 9

STANDARD TEMPLATE LIBRARY :

The Standard Template Library (STL) is a set of C++ template classes to provide common
programming data structures and functions such as lists, stacks, arrays, etc.

STL has 4 components:


Algorithms
Containers
Functors
Iterators

1. Algorithms
The header algorithm defines a collection of functions specially designed to be used on a range
of elements. They act on containers and provide means for various operations for the contents
of the containers.

Algorithm
Sorting
Searching

2. Containers
Containers or container classes store objects and data.
Eg : Vector, list, deque, queue, priority_queue, stack, set, multiset, map, multimap,
unordered_set , unordered_multiset, unordered_map, unordered_multimap

Vector :

Header file :
To use this class, we need to include the following header file in our program
#include<vector>

Initialisation :

vector<int> v0;
This creates an empty vector container of type integer

vector<int> v1{1,2,3,4,5,6};
This creates a vector container of type integer of size 6 with values as 1, 2, 3, 4, 5, 6.

vector<int> v2 (10,3);
This creates a vector container of 10 integers with all set to value 3.

vector<int> v4 (v3);
This copies the vector container v3 to v4.

vector<int> b1 (4,0);
vector<int> b2;
b2 = b1;
The member function operator= can be used to assign new contents to the vector container.
The following statements will initialize the size of vector container b2 as 4(b1 initially declared as
size 4 will all 4 integers initialized to 0) with all 4 integers initialized to 0.

Size :
vector<int> b1(3,4);
cout << b1.size();

Accessing :
The elements of the vector can be accessed using indices within square brackets.
Vector are 0-indexed.
b1[3]

Displaying the contents :


for(int i=0;i<b1.size();i++)
{
cout<<b1[i]<<” “;
}

push_back()
This memeber function accepts a single value(this value depends on the type, i.e., int, float),
and adds a new element at the end to the vector.

vector<int>v5(2,3);
This creates a vector container v5 of 2 integers with all set to value 3.

for(int k = 0 ; k < v5.size(); k++{


v5.push_back(k);
}

The push_back function adds each value of counter k to the end of v5 i.e., v5[0]=3, v5[1]=3,
v5[2]=0, v5[3]=1 and note that v5.size() will now be 4.

pop_back()
This deletes the end element of the vector, thus reducing the size by 1. In the above example, if
v5.pop_back(); is executed, the size of vector v5 will be reduced by 1 i.e., 3 and the elements of
the vector v5 will be v5[0]=3, v5[1]=3, v5[2]=0
front( )
This member function returns a direct reference to the first element in the vector
cout<<v5.front();

back()
This member function returns a direct reference to the end element in the vector
cout<<v5.back();

clear()
This member function empties the vector object and the size of the vector will be 0.
v5.clear();

resize()
This member function resizes the vector container so that it contains the number of elements(n)
specified in the argument. The argument can also contain the value that is to be written for each
of the n elements. If new n is greater than the current size of the container, then the container is
expanded by adding those many elements(value if specified in the argument) required to make
the new size of the container equal to n. If n is less than the previous size of the container, then
the container is resized to n with the remaining elements destroyed

vector<int>b1(10,3);
create a vector container of 10 integers with all set to value 3.

b1.resize(5);
resizes the size of container to 5 and destroys all the elements

b1.resize(7,20);
resizes the size of container to 7 by adding two elements(both set to value 20)

Memory allocation in vectors :


When vectors are initialized, they typically consist of a pointer to a dynamically allocated
memory. The allocated size(capacity) may be larger than the actual size used in the program.
When new elements are inserted, the actual size of the vector is automatically set. If the size
becomes larger than the capacity, reallocation occurs.

capacity()
This returns the size of the allocated space for the vector during initialization. It can be equal or
greater than the actual size. For example, if the statements are

vector<int> b1(10,4);
cout<< b1.capacity(); // 10
cout<<b1.size(); // 10
b1.push_back(5);
cout<< b1.capacity(); // 20
cout<<b1.size(); // 11
b1.reserve(100);
cout<< b1.capacity(); // 100
cout<<b1.size(); // 11

Lab Exercise for Vectors :


#include <iostream>
#include <vector>

using namespace std;

class V{
public :
vector<int>v;
int size;
V()
{
size=0;
}

V(int size)
{
(*this).size = size; // this->size = size;
v.resize(size);
}

V(int size,int value)


{
(*this).size = size;
v.resize(size,value);
}

V(vector<int>v1)
{
v = v1;
size = v1.size();
}

V(const V &V_obj)
{
v = V_obj.v;
size = V_obj.v.size();
}
void print()
{
cout <<"\nV : ";
for(int i=0;i<size;i++)
{
cout<<v[i]<<" ";
}
}

~V()
{
v.clear();
}

};

int main()
{
// create object and do necessary operations
return 0;
}

Matrix : (Vector of Vector)

Initalization :

vector<vector<int>>v;
Creation of empty vector of vector

vector<vector<int>>v(3,vector<int>(4,5));
Creation of vector of vector of 3 rows, 4 columns and values as 5.

vector<vector<int>>v{{1,2,3},{4,5,6}};
Creation of vector of vector of 2 rows and 3 columns with custom values.

Row and Column Size:

int row = v.size();


int col = v[0].size();

Example 1 : (Reading row, col, and input values from users)


#include <iostream>
#include <vector>
using namespace std;

int main()
{
int row,col,x;
cout<<"\nEnter no. of rows : ";
cin>>row;
cout<<"\nEnter no. of cols : ";
cin>>col;
vector<vector<int>>v(row,vector<int>(col));

cout<<"Enter the matrix values : ";

for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>x;
v[i][j] = x;
}
}

cout<<"\nEntered values are : \n";


for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<v[i][j]<<" ";
}
cout<<endl;
}

return 0;
}

Example 2 : (Matrix of different row size, and push_back of a new row)

#include <iostream>
#include<vector>
using namespace std;

int main()
{
vector<vector<int>>v{{1},{2,3},{4,5,6}};

cout<<"\nMatrix values are : \n";


for(int i=0;i<v.size();i++)
{
for(int j=0;j<v[i].size();j++)
{
cout<<v[i][j]<<" ";
}
cout<<endl;
}

// adding a new vector as last row

v.push_back(vector<int>(4,5)); // v.push_back({5,5,5,5});

cout<<"\nMatrix values after adding a new row are : \n";


for(int i=0;i<v.size();i++)
{
for(int j=0;j<v[i].size();j++)
{
cout<<v[i][j]<<" ";
}
cout<<endl;
}

return 0;
}

Lab Exercise for Matrix :


#include <iostream>
#include <vector>

using namespace std;


class M{
public :
vector<vector<int>>m;
int row,col;
M()
{
row=0;
col=0;
}

M(int r)
{
row = r;
col = 0;
m.resize(r);
}

M(int r,vector<int>v)
{
row = r;
col = v.size();
m.resize(r,v);
}

M(vector<vector<int>>m1)
{
m = m1;
row = m1.size();
col = m1[0].size();
}

M(const M &M_obj)
{
m = M_obj.m;
row = M_obj.m.size();
col = M_obj.m[0].size();
}

void print()
{
cout <<"\nM : \n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<m[i][j]<<" ";
cout<<endl;
}
}

~M()
{
m.clear();
}

};

int main()
{

// create object and do necessary operations


return 0;
}

You might also like