Practice problems on vector in C++

 Practice Problems On Vector in C++

Hello, everyone in todays blog we are going to discuss the most interesting topic of C++ i.e. VECTOR. VECTOR is same as array just a little difference is that VECTOR can be resize but Arrays cannot be resize once it initialize .



Key Differences in VECTOR & Arrays

  • Arrays are Static but VECTOR can be resize according to our need
  • VECTORS and Arrays both stored in contiguous memory location.
  • VECTORS some time also called as Dynamic Arrays
Basic Operations In VECTORS :

  • Declaration
syntax:-vector<datatype>vector_name(size);

Ex:- vector<int>v;

  • Size
v.size(); //by this function you can the length

  • Resize
v.resize(new size); //by this you can resize your dynamic array

  • Add Elements
v.push_back(element); //this will add element from last position

Ex:- 2 3 4

        v.push_back(5);

        2 3 4 5 //

  • Insert Element
v.insert(position,element);

v.begin() // to find first element

v.end() // to find last element

  • Delete Elements 
v.pop_back() //delete last element

Ex: 2 3 4

v.pop_back();

2 3 //final answer

  • Delete All Elements Of Dynamic Array
v.clear();

These are all the functions used in Dynamic Arrays / VECTOR.

  1.  

/*Write a C++ program to check whether numbers in
a vector can be rearranged so that each number
appears exactly once in a consecutive list of
numbers. Return true otherwise false.
Example:
1 2 5 0 3 6 7
Check consecutive numbers in the said vector! 0 */
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool consecutive(vector<int>& v){
    sort(v.begin(),v.end());
    for(int i=0;i<v.size()-1;i++){
        if(v[i+1]-v[i]>1){
            return false;
        }
    }
    return true;
}
int main(){
    //declaring the vector
    vector<int>v={1,2,9,0,3,6,7};
        cout<<"Check consecutive numbers in the
said vector "<<consecutive(v)<<endl;
        return 0;
   
}
Output :-Check consecutive numbers in the said
vector 1

2.
/*Write a C++ program that returns the elements in
a vector that are strictly smaller than their
adjacent left and right neighbours.
Example:
Original Vector elements:
1 2 5 0 3 1 7
Vector elements that are smaller than its adjacent
neighbours:
0
1 */
#include<iostream>
using namespace std;
#include<bits/stdc++.h>
void smallerLeftRight(vector<int> &v){
    for(int i=1;i<v.size()-1;i++){
        if(v[i]<v[i-1] && v[i]<v[i+1]){
            cout<<v[i]<<endl;
        }
    }
    return ;
}
int main(){
    vector<int>v={1,2,5,0,3,1,7,4,6};
    smallerLeftRight(v);
    return 0;
}
Output :- 0
1
4

3.
/*Write a C++ program to create an n x n matrix by
taking an integer (n) as input from the user.
Example:
Input: 2
Input an integer value: Create an n x n matrix by
said integer:
2 2
2 2 */
#include<iostream>
#include<vector>
using namespace std;
int main(){
    int n;
    cout<<"Enter the size of the matrix"<<endl;
    cin>>n;
    vector<vector<int>>v(n,vector<int>(n));
    //input
   int input;
   cout<<"Enter the input"<<endl;
   cin>>input;
    //output
    for(int i=0;i<v.size();i++){
        for(int j=0;j<v.size();j++){
            cout<<(v[i][j]=input)<<" ";
        }
        cout<<endl;
    }
    return 0;

}
Output:-Enter the size of the matrix
4 Enter the input 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

Comments