Showing posts with label stl. Show all posts
Showing posts with label stl. Show all posts

Friday, January 1, 2021

Print Pretty | HackerRank | C++ | STL | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/prettyprint/problem

Solution :

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

int main() {
    int T; cin >> T;
    cout << setiosflags(ios::uppercase);
    cout << setw(0xf) << internal;
    while(T--) {
        double A; cin >> A;
        double B; cin >> B;
        double C; cin >> C;

        /* Enter your code here */
        long long int D = (long)A;
        cout<<hex<<showbase<<left<<nouppercase<<D<<endl;
        cout<<fixed<<dec<<setw(15)<<setprecision(2)<<showpos<<right<<setfill('_');
        cout<<B<<endl;
        cout<<setprecision(9)<<dec<<left<<noshowpos<<uppercase<<scientific;
        cout<<C<<endl;
    }

Maps-STL | HackerRank | C++ | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/cpp-maps/problem

Solution : 

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int Q;
    cin>>Q;
    map<string,int>mp;
    while(Q--){
        int type;
        cin>>type;
        if(type == 1){
            string name;
            int marks;
            cin>>name>>marks;
            if(mp.find(name)!=mp.end()){
                mp[name] = mp[name] + marks;
            }else{
                mp[name] = marks;
            }
            
        }else if(type == 2){
            
            string name;
            cin>>name;
            mp[name] = 0;
        }else if(type == 3){
            string name;
            cin>>name;
            cout<<mp[name]<<endl;
        }
    }
    return 0;
  
}




Friday, October 30, 2020

CREATING STACK USING STL

 STACK IN STL

Standard library provides us stack container with number of built in features.


STACK TEMPLATE :

template <class T, class Container = deque<T> > class stack;


MEMBER FUNCTIONS :

All these operations can be performed in 0(1)

empty  :  test whether the container is empty
size    : return the size of the container
top   : return the top element
push  : insert element
emplace  : construct and insert element
pop  : remove top element
swap  : swap contents


CODE ;

#include <iostream>
#include <stack>    // for using stack container
using namespace std;

int main()
{
int sum = 0;
stack<int> mystack; // declaring stack
mystack.push(21);   // inserting element
mystack.push(32);
mystack.push(23);
mystack.push(63);
mystack.push(212);

while (!mystack.empty()) {   // checking the empty condition
sum = sum + mystack.top();   // checking the top element
mystack.pop();               // performing pop operation

cout << sum;
return 0;
}