Skip to content

Instantly share code, notes, and snippets.

@dumbbellcode
Created May 22, 2019 09:12
Show Gist options
  • Save dumbbellcode/c968f4444c41c821da00f0dc060c5fe8 to your computer and use it in GitHub Desktop.
Save dumbbellcode/c968f4444c41c821da00f0dc060c5fe8 to your computer and use it in GitHub Desktop.
C++ SET,MAP DEMO
#include<bits/stdc++.h>
#include <iostream>
#include <set>
#include<map>
using namespace std;
void setDemo()
{
// In vectors we need to sort again and again after appending or updating values in order to perform lower and upper bounds (nlogn time)
// Set is better , every operation takes logn time.
set<int> S;
S.insert(1);S.insert(2);S.insert(-1);S.insert(12);
for(int x :S){cout<<x<<" ";} cout<<endl; // -1 1 2 12
//TO check if an element exists in the set
auto it=S.find(-1);
//if element does not exist iterator goes to s.end()
if(it==S.end()) { cout<<"not present\n";}
else { cout << *it <<" is present\n";}
//lowerbound-> >= upperbound-> >
auto it2=S.lower_bound(-1);// In vectors we used .begin(),.end() because we use external fn lower_bound(),in sets it is internally present in the class
auto it3=S.lower_bound(0); // *it3 = 12 because zero is not present
}
void mapDemo()
{ //map means mapping somethting to something
map<int,int> A;//mapping int to int
A[1]=100;
A[2]=-1;
A[3]=300;
A[100001]=1;
map<char,int> cnt;
string x="sudheer tripathi";
for(char c:x)
{
cnt[c]++;
//we get the frequency of any character in the string
}
cout<<cnt['a']<<" "<<cnt['e']<<endl;
}
void PowerOfStl()
{
}
int main()
{
setDemo();
mapDemo();
PowerOfStl();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment