title: STL sort doesn't sort my vector<pair<int,int> > well
body: <p>I am trying to solve a more complex algorithmic problem and a partial requirement involves sorting some pairs of integers.</p>
  
  <p>Here is my code ( I've commented the irellevant part for my question) </p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;queue&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  ifstream fin("ai.in");
  ofstream fout("ai.out");
  
  #define MaxN 1001
  #define MaxK 150001
  //
  //int n;
  //int t1, t2, s1, s2, s3, s4, r1, r2, r3, r4;
  int k;
  int x,y;
  
  vector&lt;pair&lt;int,int&gt; &gt;v(MaxK);
  
  int main()
  {
  //    fin &gt;&gt; n;
  //
  //    fin &gt;&gt; t1 &gt;&gt; t2 &gt;&gt; s1 &gt;&gt; s2 &gt;&gt; s3 &gt;&gt; s4 &gt;&gt; r1 &gt;&gt; r2 &gt;&gt; r3 &gt;&gt; r4;
  
      fin &gt;&gt; k;
  
      for ( int i = 1; i &lt;= k; ++i )
      {
          fin &gt;&gt; x &gt;&gt; y;
          v[i].first = x;
          v[i].second = y;
      }
  
      sort(v.begin() + 1, v.begin() + n + 1);
  
      for ( int i = 1; i &lt;= k; ++i, fout &lt;&lt; '\n' )
          fout &lt;&lt; v[i].first &lt;&lt; ' ' &lt;&lt; v[i].second;
  
      fin.close();
      fout.close();
      return 0;
  }
  </code></pre>
  
  <p>For this input </p>
  
  <pre><code>8
  1 2
  2 3
  2 5
  4 2
  6 2
  2 2
  2 4
  5 2
  </code></pre>
  
  <p>I get this output</p>
  
  <pre><code>1 2
  2 2
  2 3
  2 5
  4 2
  6 2
  2 4
  5 2
  </code></pre>
  
  <p>Which of course is wrong as you can see the 2 4 pair near the end of output.
  So how can I solve this?</p>