Tuesday, 13 September 2016

Hacker Rank Solution Program In C++ For " StringStream "

Problem :- Hacker Rank Solution Program In C++ For " StringStream "

String stream :-string stream is a stream class to operate on strings. It basically implements input/output operations on memory (string) based streams. string stream can be helpful in different type of parsing. The following operators/functions are commonly used here

Operator >> Extracts formatted data.

Operator << Inserts formatted data.
Method str() Gets the contents of underlying string device object.
Method str(string) Sets the contents of underlying string device object.

Its header file is sstream.


Copy Color Code And Paste Into Hackerrank Editor


Solution :-


#include <sstream>

#include <vector>
#include <iostream>
using namespace std;

vector<int> parseInts(string str) 

{
stringstream ss(str);   
vector<int> out;
char ch;
int temp;
while(ss >> etmp) 
{     
    out.push_back(temp);
    ss >> ch;          
}
return out;
}

int main() 

{
    string str;
    cin >> str;
    vector<int> integers = parseInts(str);
    for(int i = 0; i < integers.size(); i++) 
    {
        cout << integers[i] << "\n";
    }
    return 0;

}

Output :-



Hacker Rank Solution Program In C++ For " StringStream "

Hacker Rank Solution Program In C++ For " StringStream "

No comments:
Write comments

Recommended Posts × +