how to split std::string into std::cvector<int>

  • Thread starter Sergey Lukoshkin
  • Start date
S

Sergey Lukoshkin

Hello!

Is there a way to split the string "10 20 30 40" into a vector of
integers? I mean that such tools as std::copy, std::istringstream and
std::istream_iterator sjould be used.

Thanks.
 
D

dlyoung

Hello!

Is there a way to split the string "10 20 30 40" into a vector of
integers? I mean that such tools as std::copy, std::istringstream and
std::istream_iterator sjould be used.

Thanks.

Yes there is. I'm fairly new myself but I believe it's similar to:

string s = "10 20 30 40";
istringstream iss(stream);
vector<int> numbers;
int temp;
while(iss>>temp)
numbers.push_back(temp);
 
D

dlyoung

Yes there is.  I'm fairly new myself but I believe it's similar to:

string s = "10 20 30 40";
istringstream iss(stream);
vector<int> numbers;
int temp;
while(iss>>temp)
numbers.push_back(temp);

(sorry the istringstream(stream) should be istringstream(s);)
 
S

Sergey Lukoshkin

Yes there is.  I'm fairly new myself but I believe it's similar to:

string s = "10 20 30 40";
istringstream iss(stream);
vector<int> numbers;
int temp;
while(iss>>temp)
numbers.push_back(temp);

Ok! And how can I avoid using the operator >> for istringstream? I
mean is it possible to use the std::copy algorithm in order to
directly insert converted integers into the vector<int> in iterative
manner? for example:
std::copy( std::istream_iterator<int>(iss(s)),
std::istream_iterator<int>(), std::back_inserter(numbers) ) -- is this
correct?
 
G

Gert-Jan de Vos

Ok! And how can I avoid using the operator >> for istringstream? I
mean is it possible to use the std::copy algorithm in order to
directly insert converted integers into the vector<int> in iterative
manner? for example:

Yes, it is.
std::copy( std::istream_iterator<int>(iss(s)),
std::istream_iterator<int>(), std::back_inserter(numbers) ) -- is this
correct?

You are close, did you try?
 
S

Sergey Lukoshkin

Yes, it is.


You are close, did you try?

std::string s("10 20 30 40");
std::istringstream istr(s);
std::vector<int> vec;
std::copy( std::istream_iterator<int>( istr ),
std::istream_iterator<int>(), std::back_inserter(vec) );

Yes, great! Thank you for answers! It works.
 
B

Bart van Ingen Schenau

std::string s("10 20 30 40");
std::istringstream istr(s);
std::vector<int> vec;
std::copy( std::istream_iterator<int>( istr ),
std::istream_iterator<int>(), std::back_inserter(vec) );

Yes, great! Thank you for answers! It works.

You can also directly initialise the vector from the stream:
std::string s("10 20 30 40");
std::istringstream istr(s);
std::vector<int> vec((std::istream_iterator<int>( istr )),
(std::istream_iterator<int>()));

(Note: The extra parentheses around the arguments are needed to defeat
C++'s most vexing parse: If it can be interpreted as a function
declaration, then do so.)

Bart v Ingen Schenau
 
D

Daniel Pitts

You can also directly initialise the vector from the stream:
std::string s("10 20 30 40");
std::istringstream istr(s);
std::vector<int> vec((std::istream_iterator<int>( istr )),
(std::istream_iterator<int>()));

You can even do it all in one go:
std::vector<int> vec((
std::istream_iterator<int>(
std::istringstream(
std::string("10 20 30 40")
)
),
 

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

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top