my first value is not compared in phone diary

the starting values are only compared in map eg.a,b,c,d....and end values such as u,v,w,...z are not compared in this program the starting values of map are not been compared plz. fix the problem.

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

       int main()
       {

           map<string, long int> pd;
           vector<string> sb;
           /* Enter your code here. Read input from STDIN. Print output to STDOUT */
           int n;
           cin >> n;
           if (n >= 1 && n <= pow(10, 5))
           {
               for (int i = 0; i < n; i++)
               {

                   string name;
                   long int phone;
                   cin >> name >> phone;
                   pd.insert(pair<string, long int>(name, phone));
               }
               for (int i = 0; i < n; i++)
               {
                   string sr;
                   cin >> sr;
                   sb.push_back(sr);
               }
           }

           if (n >= 1 && n <= pow(10, 5))
           {
               map<string, long int>::iterator p = pd.begin();
               vector<string>::iterator it = sb.begin();
               while (p != pd.end())
               {
                   while (it != sb.end())
                   {
                           if (p->first == *it)
                           {
                               cout << p->first <<"="<< p->second << endl; 

                           }
                           else
                           {
                               cout << "Not found " << endl;

                           }  
                       it++;
                   }
                       p++;
               }
           }

if the input: 3

sam 99912222

tom 11122222

harry 12299933

sam

edward

harry

OUTPUT:

Not found

Not found

harry=12299933

CORRECT OUTPUT:

sam=99912222

Not found

harry=12299933

1 answer

  • answered 2019-07-18 15:39 Maxim Egorushkin

    The search loop isn't structured correctly.

    A fix:

    map<string, long int>::iterator p = pd.begin();
    while(p != pd.end())
    {
        vector<string>::iterator it = sb.begin();
        while(it != sb.end() && p->first != *it)
            ++it;
        if(it != sb.end())
            cout << p->first <<"="<< p->second << endl;
        else
            cout << "Not found " << endl;
        ++p;
    }