How to shrink a stringstream

M

Medi Montaseri

Given a stringstream object (tokens) containing
tokens << "token1 token2 token3 token4"

I'd like to extract the first token, which I do by saying

tokens >> token ; // where token is a string

And then pass the remaining portion down to a function.
If I just pass the tokens (even after removing one item from it)
to my function, I see that the entire payload is passed down.

How do I pass the remaining portion down to a function ?
ie "token2 token3 token4"

Thanks
 
M

Mike Wahler

Medi Montaseri said:
Given a stringstream object (tokens) containing
tokens << "token1 token2 token3 token4"

I'd like to extract the first token, which I do by saying

tokens >> token ; // where token is a string

And then pass the remaining portion down to a function.
If I just pass the tokens (even after removing one item from it)
to my function, I see that the entire payload is passed down.

How do I pass the remaining portion down to a function ?
ie "token2 token3 token4"

It would be better if you posted *compilable* code,
so we know for sure what's wrong.

Example:

#include <iostream>
#include <sstream>

void foo(std::stringstream& ss)
{
std::string tok;

while(ss >> tok)
std::cout << tok << '\n';
}

int main()
{
std::stringstream tokens("token1 token2 token3 token4");
std::string token;
tokens >> token;
foo(tokens);
return 0;

}

Output:

token2
token3
token4

HTH,
-Mike
 
M

Medi Montaseri

Mike Wahler said:
It would be better if you posted *compilable* code,
so we know for sure what's wrong.

Example:

#include <iostream>
#include <sstream>

void foo(std::stringstream& ss)
{
std::string tok;

while(ss >> tok)
std::cout << tok << '\n';
}

int main()
{
std::stringstream tokens("token1 token2 token3 token4");
std::string token;
tokens >> token;
foo(tokens);
return 0;

}

Output:

token2
token3
token4

HTH,
-Mike


I must have been sleepy (or even sleeping) last night....you are 100% correct...
it works as expected....sorry for the false alarm....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top