Tuesday, 13 September 2016

Hacker Rank Solution for StringStream In C++

Problem:- Hacker Rank Solution Program In C++ For " StringStream " or Hacker Rank Solution Program In C++ For StringStream or StringStream solution hacker rank or Hacker rank solution for c++ domain or Hacker rank solution for StringStream sub domain or StringStream solution in c++ of hacker rank.


Logic:- For this problem first step is to take a user input and after taking a user input implement the function vector parseInts(string str), user input separated integer. So basically in this problem, we are going to use vector and string stream Below is the explanation of the problem in step by step with an example. For solving this problem we have to know about some basics related to string stream

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.

Explanation:- So as we discuss above first we take a user input in a single line, the input will be separated by the commas. As shown below we declare string str.

string str;
cin >> str;

vector<int> integers = parseInts(str);

for(int i = 0; i < integers.size(); i++) 
{
cout << integers[i] << "\n";
}

Above we also pass the user input separated by commas to vector function. Now the input is taken, next step is to print the integers. if you don't know the how to push back element work, then you have to take a tour with STL (Standard Template Library for C++). Below is the scenario of a vector function.

Recommended:- Hacker rank solution for STL

vector<int> parseInts(string str) 
{
statemants.............1
statemants.............2
statemants.............3
.
.
.
.
.
statemants.............n
}

Now the implementation of the vector function is described below with all needed action to perform and run the code or run the solution correctly.

stringstream ss(str); 
vector<int> out;

char ch;
int temp;

while(ss >> temp) // Extracts formatted data.

out.push_back(temp);
ss >> ch; 
}

That's it our code is working successfully.

Check- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard Domain.


Submit your solution here:- Click here

Tip:- Before copy the solution I recommended please read this full article, this will help you to build your own logic. If you have any query feel free to share with me, and if you like my work please share, comments and subscribe for our new and latest post related to programming this will help you update all about new post this Article.


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 >> temp)
{
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:-


Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment