title: smallest element in a array of strings c++
body: <p>I am trying to find the smallest element in a array of strings but I can't figure out how to do this. I came up with this code idea which works perfectly for <em>integers</em> but does not work for strings. This will compile, although it only checks the first character in string for its ASCII value. In other words, in an array of strings: <code>lists[5] = { "aaa", "z", "cccc", "tt", "jjj"};</code> <em>lists[1]</em> or <em>"z"</em> is the smallest element of the strings. However, because 'a' is a lower ASCII value the code will print out <code>Smallest aaa</code> instead of <code>Smallest z</code>. Now I know I could do some kind of deep compassion of each character in the string using .length but I want to use something that is simple that can solve this because I want to add it to a function that will be overloaded to a integer so I can go back and forth between string and integer comparisons. But if this is not possible I will just have two separate functions to handle each. </p>
  
  <p>What if any suggestions do you have on how to find the smallest element in a array of strings?     </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;algorithm&gt;
  
  using namespace std;
  
  int main() {
  
  
  string  lists[5] = { "aaa", "z", "cccc", "tt", "jjj"};
      string smallests;
      smallests = lists[0];
      for (int i = 0; i &lt; 5; i++){
          cout &lt;&lt; smallests &lt;&lt; endl;
          if (lists[i] &lt; smallests){   // Flip &lt; to &gt; to find largest
              smallests = lists[i];
          }
      }
      cout &lt;&lt; "Smallest " &lt;&lt; smallests &lt;&lt; endl;
      cout &lt;&lt; *min_element(lists, lists + 5) &lt;&lt; endl;
  
  return 0;
  }
  </code></pre>