Dividing user input into strings

Jun 29, 2013 at 11:45pm
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.
Jun 29, 2013 at 11:58pm
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 Jun 30, 2013 at 11:28am
Jun 30, 2013 at 2:44am
Jun 30, 2013 at 3:22am
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.
Jun 30, 2013 at 4:44am
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?
Jun 30, 2013 at 2:11pm
@ vlad: you can construct the vector with the istream_iterators instead of using copy.
Last edited on Jun 30, 2013 at 2:12pm
Jun 30, 2013 at 3:12pm
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/
Jun 30, 2013 at 3:23pm
@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 Jun 30, 2013 at 3:24pm
Jun 30, 2013 at 4:30pm
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>() };
Jun 30, 2013 at 4:39pm
@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 Jun 30, 2013 at 4:39pm
Jun 30, 2013 at 4:56pm
No, that's just silly (combining two different ways to avoid MVP)
Jun 30, 2013 at 5:01pm
Ah, I have not seen that you are using braces instead of parentheses.:)
Jun 30, 2013 at 8:26pm
Ok @Douas you're right
Jun 30, 2013 at 11:19pm
Sorry I was grouchy with you.
Topic archived. No new replies allowed.