0% found this document useful (0 votes)
5 views21 pages

Vectors

The C++ Standard Template Library (STL) provides a set of template classes for implementing common data structures and algorithms, including containers, algorithms, and iterators. Vectors, a key component of STL, are dynamic arrays that can grow in size and offer various functionalities for element access, iteration, and manipulation. The document also covers basic vector operations, sorting, and functions for finding minimum and maximum elements, as well as counting and finding specific values.
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)
5 views21 pages

Vectors

The C++ Standard Template Library (STL) provides a set of template classes for implementing common data structures and algorithms, including containers, algorithms, and iterators. Vectors, a key component of STL, are dynamic arrays that can grow in size and offer various functionalities for element access, iteration, and manipulation. The document also covers basic vector operations, sorting, and functions for finding minimum and maximum elements, as well as counting and finding specific values.
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/ 21

STL

STANDARD TEMPLATE LIBRARY


 The C++ STL (Standard Template Library) is a powerful set of C++ template
classes to provide general-purpose classes and functions with templates
that implement many popular and commonly used algorithms and data
structures like vectors, queues, and stacks.

 These are three mostly used basic components of STL:-


1
Containers
Containers are used to manage collections of objects of a certain kind. There
are several different types of containers.
2 Algorithms
Algorithms act on containers. They provide the means by which you will
perform initialization, sorting, searching, and transforming of the contents of
containers.
3 Iterators
Iterators are used to step through the elements of collections of objects. They
are used to point to the memory address.
Why To Learn STL ?

STL provides programmers to store the data effectively, and do


manipulation in stored data. These are the general-purpose
templates of classes and functions that help in implementing the
basic algorithms and data structures like vector, lists, queue,
stack, etc.
VECTORS
 Vectors are like dynamic arrays .

 Vectors are used to store elements of similar data types. However, unlike
arrays, the size of a vector can grow dynamically.

 That is, we can change the size of the vector during the execution of a
program as per our requirements.
Declaration And Initialization Of Vectors:
Before using vectors, make sure that you have included it’s header file.
#include <vector>

To declare an array we use the following syntax:


vector<data-type> v_name;

To initialize it we use the following syntax:


vector<data-type> v_name (size,initial-val);
Or, vector<data-type> v_name = {1,2,3,4.5};
How to Iterate in Vectors
#include <iostream> #include <iostream>
#include <vector> #include <vector>
using namespace std; using namespace std;

int main(){ int main(){


vector<int> v= {1,2,3,4,5}; vector<int> v(5,10);
for (int i=0; i<5; i++){ for (int i=0; i<5; i++){
cout<<v[i]<<" "; cout<<v[i]<<" ";
} }
cout<<endl; cout<<endl;

return 0; return 0;
} }
Element Access Method
To get the element at begin:
v.front();

To get the element at end:


v.back();

To get the element at ith index:


v.at(i);
Or, simply v[i];
The Iterators
1. begin() – Returns an iterator pointing to the first element in the vector

2. end() – Returns an iterator pointing to the theoretical element that follows


the last element in the vector

3. rbegin() – Returns a reverse iterator pointing to the last element in the


vector (reverse beginning). It moves from last to first element

4. rend() – Returns a reverse iterator pointing to the theoretical element


preceding the first element in the vector (considered as reverse end)
Illu
str
Ite atio
#include <iostream>

r n
#include <vector>
using namespace std;
In a to Of
Ve rs
int main(){
cto
rs
vector<int> v;
for (int i = 1; i <= 5; i++)
v.push_back(i);

cout << "Output of begin and end: ";


for (auto i = v.begin(); i != v.end(); ++i)
cout << *i << " ";

cout << "\nOutput of rbegin and rend: ";


for (auto ir = v.rbegin(); ir != v.rend(); ++ir)
cout << *ir << " ";

return 0;
}
Some Basic Functions
To know the size of the vector :
v.size();

To add value at the end:


v.push_back(val);

To remove the value from end:


v.pop_back();

To check the capacity :


v.capacity();
To check whether vector is empty or not :
v.empty();

To clear the vector :


v.clear();

To insert at particular index i:


v.insert(v.begin()+i,30);

To delete a element at index i:


v.erase(v.begin()+i);
Finding Min And Maximum Elements And Their Index

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

int main(){
vector<int> v= {2,5,10,1,50,26,30};
int minimumElement= *min_element(v.begin(),v.end());
int minInd= min_element(v.begin(),v.end())-v.begin();
int maximumElement= *max_element(v.begin(),v.end());
int maxInd= max_element(v.begin(),v.end())-v.begin();
cout<<"Minimum element is :"<<minimumElement<<" at index "<<minInd<<endl;
cout<<"Maximum element is :"<<maximumElement<<" at index "<<maxInd<<endl;
return 0;
}
Sorting
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
vector<int> v= {2,5,10,1,50,26,30};
sort(v.begin(),v.end());
for (int i=0; i<7; i++){
cout<<v[i]<<" ";
}
cout<<endl;
return 0;
}
upper_bound And lower_bound
#include <bits/stdc++.h>
using namespace std;

int main(){
vector<int> v= {1,2,3,4,6,7,9};
int lower1= *lower_bound(v.begin(),v.end(),4);
int lower2= *lower_bound(v.begin(),v.end(),5);
int upper1= *upper_bound(v.begin(),v.end(),4);
int upper2= *upper_bound(v.begin(),v.end(),5);
cout<<"Lower bound of 4 is "<<lower1<<endl;
cout<<"Lower bound of 5 is "<<lower2<<endl;
cout<<"Upper bound of 4 is "<<upper1<<endl;
cout<<"Upper bound of 5 is "<<upper2<<endl;
return 0;
}
Reversing
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
vector<int> v= {2,5,10,1,50,26,30};
reverse(v.begin(),v.end());
for (int i=0; i<7; i++){
cout<<v[i]<<" ";
}
cout<<endl;
return 0;
}
Sum : Accumulate function

#include <bits/stdc++.h>
using namespace std;

int main(){
vector<int> v= {2,5,10,1,50,26,30};
int sum= accumulate(v.begin(),v.end(),0);
cout<<"THE sum is: "<<sum<<endl;
cout<<endl;
return 0;
}
Find Function

#include <bits/stdc++.h>
using namespace std;

int main(){
vector<int> v= {2,5,10,1,50,26,30};
int val= find(v.begin(),v.end(),1)-
v.begin();
cout<<val;
cout<<endl;
return 0;
}
Count Function

#include <bits/stdc++.h>
using namespace std;

int main(){
vector<int> v= {2,5,1,10,1,50,26,1,1,30};
int val= count(v.begin(),v.end(),1);
cout<<val;
cout<<endl;
return 0;
}
Finding the factors:
O(N) O(sqrt(N
))

#include <bits/stdc++.h> #include <bits/stdc++.h>


using namespace std; using namespace std;

int main(){ int main(){


int n= 100; int n= 100;
vector<int> v; vector<int> v;
for (int i=1; i<=n; i++){ for (int i=1; i*i<=n; i++){
if (n%i==0){ if (n%i==0){
v.push_back(i); v.push_back(i);
} if (i!=(n/i))v.push_back(n/i);
} }
for (int i=0; i<v.size(); i++){ }
cout<<v[i]<<" "; for (int i=0; i<v.size(); i++){
} cout<<v[i]<<" ";
return 0; }
} return 0;
}
Thank You

You might also like