title: C++: Sort list alphabetically, display only first and last items
body: <p><strong>Update</strong>: Final versions, <a href="http://pastie.org/8396380" rel="nofollow">with <code>&lt;set&gt;</code></a> and <a href="http://pastie.org/8396372" rel="nofollow">without <code>&lt;set&gt;</code></a>.</p>
  
  <p>I've been working on a problem:</p>
  
  <blockquote>
    <p>Design a program that asks the user for a series of names (in no
    particular order). After the final person’s name has been entered, the
    program should display the name that is first alphabetically and the
    name that is last alphabetically.</p>
    
    <p>For example, if the user enters the names Kristin, Joel, Adam, Beth,
    Zeb, and Chris, the program would display Adam and Zeb.</p>
  </blockquote>
  
  <p>Here's the code I have so far:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;set&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  void displayOutput(const string&amp; item)
  {
      cout &lt;&lt; item &lt;&lt; endl;
  }
  
  int main()
  {
      set&lt;string&gt; sortNames;
      string name;
  
      do {
          cout &lt;&lt; "Enter a name (\"end\" to finish):\t";
          cin &gt;&gt; name;
  
          sortNames.insert(name);
      } while ( name != "end" );
  
      for_each(sortNames.begin(), sortNames.end(), &amp;displayOutput);
  
      return 0;
  }
  </code></pre>
  
  <p>My code so far works fine, in that it displays all inputted strings alphabetically. (On a side note, I'm not sure how to get around having "end" not display in the output itself.) The problem is, the program is only supposed to display the (alphabetically) <em>first and last names</em> from the list. I've been thinking about it, and I think that I might have to approach the problem from a different angle, but I'm not sure where to start. Any help would be greatly appreciated.</p>