-6
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    string s,e;
    cin>>n;
    vector <pair<string,string>> m(n);
    for(int i=0;i<n;i++)
    {
        cin >> s >> e;
        if (e.find("gmail")!=string::npos)
            m[i]=make_pair(s,e);
    }

    sort(m.begin(),m.end());
    auto it=m.begin();
    while(it!=m.end())
    {
        cout<<it->first<<"\n";
        it++;
    }
}

Output:

julia
julia
riya
samantha
tanya

My output is correct, but one extra line is getting printed at the beginning and hence none of my test cases pass in Hackerrank.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
  • 2
    Also please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) As well as about [how to ask good questions](http://stackoverflow.com/help/how-to-ask) and [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). For example, what is the input? What is the *expected* output? – Some programmer dude Jul 22 '19 at 10:40
  • Also, please make use of the preview function and format your post (and the code within) properly. However, welcome to Stack Overflow :) – Sebastian Mach Jul 22 '19 at 10:41
  • 2
    What is your *input*? and are you aware that you're incrementing `i` in your input loop even when you're rejecting the input? – user207421 Jul 22 '19 at 10:42
  • Thanks to all,I got the problem. – Nikhil Sarda Jul 23 '19 at 13:02

1 Answers1

5

You create a vector of size n. Then you check for each input line if the second string contains the text gmail and you store the pair in the i-th element. You increment i even if you skip the input. That means that some elements of the vector can remain empty. At the end you print all elements.

E.g. input is:

7
julia gmil
julia gmail
julia gmail
riya gmail
samantha gmail
samantha gmil
tanya gmail

You store:

m[0]:
m[1]: julia
m[2]: julia
m[3]: riya
m[4]: samantha
m[5]:
m[6]: tanya

The output after sort is

(empty)
(empty)
julia
julia
riya
samantha
tanya
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62