CodeISM Class 5 (STL Set, Map)
CodeISM Class 5 (STL Set, Map)
Set
set is a special kind of STL container which stores unique
elements in sorted order.
set<int> st;
Example:
st.erase(3);
for(auto it=st.begin();it!=st.end();it++){
cout<<*it<<" ";
}
auto it = st.find(6);
cout<< *it << endl; // 6
Map
Map is a special kind of STL container which stores
elements as key-value pair. No two mapped values can
have same key. All the keys are sorted in ascending
order.
All the keys are unique.
Example:
map<string, string> mp;
mp["20JE0666"]="Sakshi";
mp["20JE0648"]="Saksham";
mp["20JE0654"]="Shivali";
Another example:
map<int,char> mp1;
mp1[1]='A';
mp1[2]='B';
mp1[3]='C';
map<char,int> mp2;
mp2['A']=1;
mp2['B']=2;
mp2['C']=3;
Question1->
Print this pattern using for loop:
1
1,2,
1,2,3,
1,2,3,4
1,2,3,4,5
Approach: (Use a variable i for row)
i=1 1
i=2 1,2,
i=3 1,2,3,
i=4 1,2,3,4
i=5 1,2,3,4,5
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++) cout<<j<<" ";
cout<<endl;
}
Question2
https://www.hackerrank.com/challenges/marcs-cakewalk/p
roblem
Approach:-
1, 2, 4, 8, 16..... [constant value]
A1 A2 A3 A4 A5..... [any particular arrangement]
Note:
When I was using “int currValue” then there was a
problem of overflow so always look at the worst case
value and here when I changed it to “long currValue”
then all the test cases got passed.
'a' => 97
'b' => 98
...
...
'z' => 122
'A' => 65
'B' => 66
...
...
'Z' => 90
#include <bits/stdc++.h>
using namespace std;
int main(){
char c='a';
cout<<c<<endl;
int value = (int)c;
cout<<value<<endl;
return 0;
}