STL রিলেটড কিছু প্রোগ্রাম


STL ম্যাপের মাধ্যমে সর্ট করা
code:

include <bits/stdc++.h>
using namespace std;
map<int, char>mp;
int main ()
{
    for(int i=1; i<5; i++)
    {
        char x;
        int y;
        cin >> x >> y;
        mp.insert(pair<int, char>(y, x));
    }
    if(!mp.empty())
    {
        auto it = mp.end();
        --it;


        cout << it->second<<" "<<it->first<<endl;
    }
   return 0;
}

বাইনারী সার্চ:
#include <bits/stdc++.h>
using namespace std;

bool compare(string a, string b)
{
    return (a.size()==b.size());
}
int main ()
{
    int input[100], n;

    cin >> n;
    vector <int> v;
    for(int i=0; i<n; i++)
    {
        int data;
        cin >> data;
        v.push_back(data);
    }
    cout << "Enter you want search : ";
    int x;
    cin >> x;

    bool ans = binary_search(v.begin(), v.end(), x);

    if(ans==true){
        cout << "found" << endl;
    }
    else{
        cout << "Not found" << endl;
    }
    return 0;
}

Cumulative sum:

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

int arraysum(int a[], int n)
{
    int initial_sum = 0;
    return accumulate(a, a+n, initial_sum);
}
int main ()
{
    int n;
    cin >> n;
    int arr[n];

    for(int i=0; i<n; i++)
    {
        cin >> arr[i];
    }
    cout << arraysum(arr, n);
    return 0;
}





Cumulative sum using STL:

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

int arraysum(vector<int>&v)
{
    int initial_sum = 0;
    return accumulate(v.begin(), v.end(), initial_sum);
}
int main ()
{   vector <int> v;
    int siz;
    cin >> siz;
    for(int i=0; i<siz; i++)
    {
        int x;
        cin >> x;
        v.push_back(x);
    }
    cout << arraysum(v);
    return 0;
}


FIND A SUBSTRING IN C++

#include <bits/stdc++.h>
using namespace std;
int main ()
{
    string s1;
    getline(cin, s1);
    string s2;
    getline(cin, s2);

    bool found = s1.find(s2)!=s1.npos;

    if(found){
        cout << "substring found" << endl;
    }
    else cout << "substring not found" << endl;

    return 0;
}

print all substring of a given string:
#include <bits/stdc++.h>
using namespace std;
int main ()
{
    string s;
    getline(cin, s);

    for(int i=0; i<s.size(); i++){
        for(int len=1; len<=s.size()-i; len++){
            cout << s.substr(i, len) << endl;
        }
    }

    return 0;
}

Check if a string is substring of another

#include <bits/stdc++.h>
using namespace std;
int main ()
{
    string s1;
    getline(cin, s1);
    string s2;
    getline(cin, s2);
    int res=0;

    for(int i=0; i<=s1.size()-s2.size(); i++)
    {
        int j;
        for(j=0; j<s2.size(); j++)
        {
            if(s1[i+j]!=s2[j])
            {
                break;
            }
        }
        if(j==s2.size())
        {
            res++;
            j=0;
        }
    }
    cout << res << endl;
    return 0;
}

Delete number using stl

#include <bits/stdc++.h>
using namespace std;
int main ()
{
 vector <int> v;

 int n;
 cin >> n;
 for(int i=0; i<n; i++)
 {
  int x;
  cin >> x;
  v.emplace_back(x);
 }
 cout << "Enter the possition  you del : ";
 int pos;
 cin >> pos;

 v.erase(v.begin()+pos);

 for(auto it = v.begin(); it!=v.end(); it++)
 {
  cout << *it << endl;
 }
 return 0;
}

 

Comments

Popular posts from this blog

কন্টেস্ট রিলেটড কিছু বিষয়

Efficiently getting all divisors of a given number

Print sum of prime divisor