Simple binary search Here the task (in Indonesian).

I think my binary search algorithm is correct.

Ideone

 #include&lt;bits/stdc++.h&gt;
using namespace std;
vector&lt;pair&lt;string, string&gt;&gt; v;
int awal1 = 1;
string check(string s, int awal, int akhir){
    int tengah = (awal+akhir)/2;
    bool temukan = false;
    if(v[tengah].first == s){
        return v[tengah].second;
        temukan = true;
    }
    else if(v[tengah].first &gt; s){
        return check(s, awal, tengah-1);
    }
    else if(v[tengah].first &lt; s){
        return check(s, tengah+1, akhir);
    }
    if(temukan == false) return "NIHIL";
} 

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
     string s, s1, s2; vector&lt;string&gt; h; 
    int n, p;
    cin &gt;&gt; n &gt;&gt; p;
    for(int i=1;i&lt;=n;i++){
        cin &gt;&gt; s &gt;&gt; s1;
        v.push_back(make_pair(s, s1));
    }
    for(int j=1;j&lt;=p;j++){
        cin &gt;&gt; s2;
        h.push_back(s2);
    }
    for(int i=1;i&lt;=h.size();i++){
        cout &lt;&lt; check(h[i], 1, v.size()) &lt;&lt; "\n";   
    }
    /*for(int i=0;i&lt;v.size();i++){
        cout &lt;&lt; v[i].first &lt;&lt; " " &lt;&lt; v[i].second &lt;&lt; "\n";
    } */
}
This is a non standard header.

#include&lt;bits/stdc++.h&gt;


The "bits" directory is an implementation detail of your compiler. You should NEVER use it in your code.

Your headers should be:

#include &lt;vector&gt;
#include &lt;iostream&gt;


You should not do this:

using namespace std;


Read the details about the issues here: Why is “using namespace std” considered bad practice?

You should avoid using global variables.

vector&lt;pair&lt;string, string&gt;&gt; v;


Here you are locking your check() function to searching only one list. It should search any list for the key.

This global variable does not seem to be used anywhere:

int awal1 = 1;


Bounds checking:

string check(string s, int awal, int akhir){


If awal and akhir are equal there is nothing to check. You have gone beyond the range. So You need to do a test here. If these two are equal then you failed to find the data.

Note: That akhir is one beyond the end of the range you are checking. For the very right hand end of the vector this means beyond the end of the v which means that accessing it is undefined behavior and likely to cause a crash.

This value is always false.

    bool temukan = false;


Because this statement is never executed.

    if(v[tengah].first == s){
        return v[tengah].second;

        // The line above is a return. 
        // So the next line is never reached.
        // So we never execute this.
        temukan = true;
    }


    // So in this line  `temukan` is always false.
    // So this `if` test is always true
    // So this would always return 'NIHIL'
    if(temukan == false) return "NIHIL";


BUT. In the same function you also have another set of if else statements. That always result in a return. So the code never falls through to the last 'NIHL' statement.

if(v[tengah].first == s){              // Equal
    return v[tengah].second;
}
else if(v[tengah].first &gt; s){          // Greater
    return check(s, awal, tengah-1);
}
else if(v[tengah].first &lt; s){          // Less
    return check(s, tengah+1, akhir);
}


One of those branches must be true (unless you were comparing floats). So the code will never go past these set of tests.

So there are some massive bugs in the function check(). You definitely need to fix these. I am not going to fix them because I think it would do you good to try and work out what the correct solution is. But as it stands this would fail any unit tests you should have written.

This is C code.

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);


What you probably want is the C++ std::fstream class.

One variable per line please:

     string s, s1, s2; vector&lt;string&gt; h; 
    int n, p;


As containers like arrays/vectors are 0 based. We usually loop from 0 to 1 less than the number.

    for(int i=1;i&lt;=n;i++){

    // Most C/C++/Java/C# etc programmer would write that loop as:
    for(int i = 0; i &lt; n; ++i) {


Yes this works.

        v.push_back(make_pair(s, s1));


But you can use the emplace_back() method which makes it easier to read and removes an unneeded copy.

        v.emplace_back(s, s1);


As I mentioned above containers are indexed from 0.

        cout &lt;&lt; check(h[i], 1, v.size()) &lt;&lt; "\n";   


So passing a 1 here is wrong. As you will never find the first element. You should have noticed there was not check for "agung". You can't find this person if you search from 1 as they are in position 0. Unfortunately your check loop also started at 1 so your code missed it.