Dividing user input into strings

I'm trying to save the words of a sentence that is entered by the user in different strings. The problem is that I have no idea how to write that a space marks the beginning of a new word. Would appreciate any help or suggestions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>

int main()
{
   std::cout << "Enter a sentence: ";

   std::string s;
   std::getline( std::cin, s );

   std::istringstream is( s );

   std::vector<std::string> v;

   std::copy( std::istream_iterator<std::string>( is ),
                   std::istream_iterator<std::string>(),
                   std::back_inserter( v ) );

   for ( std::string word : v ) std::cout << word << std::endl;
}


EDIT: I updated the code.
Last edited on
Please don't tell people to use strtok(). The function has serious limitations and almost no one (even among experienced programmers) knows how to use it properly.
Whatever the limitations might be, I'm sure the extent of OP's problem does not uncover those limitation. And how do you know that almost no one uses this function?
@ vlad: you can construct the vector with the istream_iterators instead of using copy.
Last edited on
I love it when some snot starts making wild statements.

If you understood strtok(), you would know that the OP (and every other beginner who has ever touched it) will almost certainly "uncover those limitation".

Forums all over the internet for three generations are filled with posts about how to make strtok() behave.

Also, be careful how you represent me. I didn't say "almost no one uses it". I said "almost no one knows how to use it properly."

The fact is that it is a dangerous function. So much so that I've written an entire FAQ on it:
http://www.cplusplus.com/faq/sequences/strings/strtok/

I know you won't bother to click the link, so, in a nutshell:

  - it is not reentrant!
  - it modifies its argument

Heck, its very manpage warns you to "be cautious when using [it]".


So, please don't tell people to use strtok().

There are plenty of safer alternatives.
http://www.cplusplus.com/faq/sequences/strings/split/
@LowestOne

@ vlad: you can construct the vector with the istream_iterators instead of using copy.


You are right. It would be simpler to write

1
2
std::vector<std::string> v( std::istream_iterator<std::string>( is ),
                            std::istream_iterator<std::string>() );


The same task can be done in several ways in C++.:)
Last edited on
That's a function declaration, make it at least
1
2
std::vector<std::string> v{ std::istream_iterator<std::string>( is ),
                            std::istream_iterator<std::string>() };
@Cubbi

That's a function declaration, make it at least


1
2
 std::vector<std::string> v{ std::istream_iterator<std::string>( is ),
                             std::istream_iterator<std::string>() };



You meant to make it for example as

1
2
 std::vector<std::string> v{ ( std::istream_iterator<std::string>( is ) ),
                             std::istream_iterator<std::string>() };

Last edited on
No, that's just silly (combining two different ways to avoid MVP)
Ah, I have not seen that you are using braces instead of parentheses.:)
Ok @Douas you're right
Sorry I was grouchy with you.
Topic archived. No new replies allowed.