Anda di halaman 1dari 4

MAP DATA STRUCTURE:

map.find() iterates through your map structure. if it finds the


element then it returns an iterator to that element else it
returns end. End being an element past the last element of that
container, essentially telling you it wasn't found. Remember
containers in c++: arrays, vectors, maps, queues, stacks, etc.
start at 0. So if you have a map container with 10 elements and
an index of 10 is returned this is outside the bounds of the
container so in this function end is returned. check out the
following;
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print
output to STDOUT */
int n;
string name;
long num;
cin >> n;
map <string, long> pBook;
for (int i = 0; i < n; i++)
{
cin >> name;
cin >> num;
pBook[name] = num;
}
while(cin >> name)
{
if(pBook[name])
{
cout << name << "=" << pBook[name];
}
else cout << "Not found";
cout << endl;
} return 0;
Always declare the arry as the vector in cpp
SYNTAX:
vector<int> ar(n)
MUST REMEMBER POINT.

vector< vector<int> > a(n,vector<int>(n));

declaration of double array

for printing the float precision


cout<<fixed<<setprecision(1)<<e<<endl;
to print the decimals after the . in the program

so we should not go for 2 loops in hacker rank


prefer only one loop so that the time out error will
not occur.
function insertionSort (array) {
for(var i = 1 ; i < array.length ;){
if(array[i] < array[i-1]){
var temp = array[i]
array[i] = array[i -1]
array[i -1] = temp
i--
} else{i++}
}
return array
}
DECLARING THE DUAL SIZE ARAY:
vector< vector<int> > arr(6,vector<int>(6));
for(int arr_i = 0;arr_i < 6;arr_i++){
for(int arr_j = 0;arr_j < 6;arr_j++){
cin >> arr[arr_i][arr_j];
}
}

COUNTING COSECUTIVE NUMBERS:


using namespace std;
int binary1(int n)
{
int e=0,a,f=0;
long b=0;

while(n>0)
{
a= n%2;
if(a==1)
{
e++;
if(e>f)
{
f=e;
}
}
else
{

e=0;
}
n=n/2;
}

return(f);

int main(){
int n,a;

cin >> n;
a= binary1(n);
cout<<a;
return 0;
}

Anda mungkin juga menyukai