Find the longest word in a sentence This is my second attempt after I started to learn about STL. I am oblivious and need suggestions/advices on if there can be improvements made on this code.

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;algorithm&gt;
#include &lt;iterator&gt;
#include &lt;vector&gt;
using namespace std;

string LongestWord(string sen) {

  vector&lt;string&gt; coll; //initialize vector

  istringstream iss(sen); //read the string "sen"

  copy(istream_iterator&lt;string&gt;(iss), //copy from beginning of iss
    istream_iterator&lt;string&gt;(), //to the end of iss
    back_inserter(coll)); //and insert string to vector
  //istream_iterator by default separates word by whitespace

  string longestWord = coll.at(0);
  int longestCount = longestWord.length();

  for(auto element : coll)
  {
      if(element.length() &gt; longestCount)
      {
          longestWord = element;
      }
  }
    return longestWord;

}
As @user1118321 points out: just using your implementation, I believe you're missing a crucial line (inside your for loop and if-statement):

longestCount = element.length();


Which becomes:

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;algorithm&gt;
#include &lt;iterator&gt;
#include &lt;vector&gt;
using namespace std;

string LongestWord(string sen) {
    vector&lt;string&gt; coll; //initialize vector

    istringstream iss(sen); //read the string "sen"

    copy(istream_iterator&lt;string&gt;(iss), //copy from beginning of iss
        istream_iterator&lt;string&gt;(), //to the end of iss
        back_inserter(coll)); //and insert string to vector
    //istream_iterator by default separates word by whitespace

    string longestWord = coll.at(0);
    int longestCount = longestWord.length();

    for(auto element : coll)
    {
        if(element.length() &gt; longestCount)
        {
            longestWord = element;
            longestCount = element.length();
        }
    }

    return longestWord;
}




Don't use using namespace std, it's better to use std:: as needed. While it's insignificant here, if you work on larger projects it will cause headaches. Especially if you're learning STL, it's a good idea to learn what goes in the STL and the C++ Standard Library (ie within namespace std).

Instead of iterating over the string (when tokenizing into words) and again over the vector, you could just iterate once:

Below is a common way to find the maximum of a container of elements, in this case std::vector&lt;std::string&gt;, while taking advantage of std::istringstream:

#include &lt;sstream&gt;
#include &lt;string&gt;

std::string LongestWord(std::string str){
    std::string::size_type max_len = 0;
    std::string longest_word;
    std::string word;

    std::istringstream stream(str);

    while(stream &gt;&gt; word) {
        if(max_len &lt; word.length()) {
            max_len = word.length();
            longest_word = word;
        }
    }

    return longest_word;
}


This function could be improved depending on how you wish to use it. You may wish to not count punctuation (recommended), you may wish to return a vector of strings if there are words of the same length, or you may wish to throw an error when given an empty string. These are all fairly easy to implement as needed.