Can I use stringstream?


  • 0
    U
    void reverseWords(string &s) {
        stack<string> words;
        stringstream ss;
        ss << s;
        while(ss >> s){
             words.push(s);       
        }
    
        s.clear();
        while(!words.empty()){
            s += words.top() + " ";
            words.pop();
        }
        if(s != "")
            s.resize(s.size() - 1);
    }
    

    This is my code of using stringstream. Quit simple. I saw some comments said better not use built-in func. Is stringstream tricky?


Log in to reply
 

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