Program compiles fine on my machine but failed to compile by LeetCode


  • 0
    N
    #include <string>
    #include <vector>
    #include <iostream>
    #include <stack>
    #include <algorithm>
    #include <sstream>
    #include <ostream>
    #include <iterator>
    
    using namespace std;
    
    stack<string> split(const string& str_to_split, const char delim)
    {
      stack<string> elems;
      stringstream s(str_to_split);
      string item;
      while (getline(s, item, delim))
        {
          elems.push(item);
        } 
      return elems;
    }
    
    class Solution {
    public:
      void reverseWords(string &s) {
        if (s.empty()) {
          return;
        }
        stack<string> st = split(s, ' ');
        vector<string> st_contents;
        while (!st.empty()) {
          if (st.top().empty())
    	continue;
          st_contents.push_back( st.top() );
          st.pop();
        }
        ostringstream os;
        copy (st_contents.begin(), st_contents.end(), std::ostream_iterator<string>(os, " "));
    
        // remove the trailing space
        s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
    
        s = os.str();
      }
    };
    

    I got compiler error: 'ostream_iterator' was not declared in this scope, but it works on my local machine (gcc 4.7 Ubuntu 64). Anyone knows why?


  • 0

    You do not need to do #include <...> as it would be automatically included for you. In fact, all your #includes will be stripped away. In this case, ostream and iterator is not supported, and hence the compile error. I don't think you need fancy library in order to solve this problem.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.