By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,985 Members | 2,884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,985 IT Pros & Developers. It's quick & easy.

Setting a string using cin then making the string into a list between spaces

P: 1
So I set up a string like this:

string input
cin >> input

then I want to split that string into a list so that all the words in between spaces are entries in a list.

So if the string were "I like cats"
The list would have the first entry be "I" and the second entry would be "like" and so on.

How do I do this??
Dec 9 '15 #1

✓ answered by hpmachining

First off, you won't be able to get a string with spaces using std::cin like that. It will only get the first word. Instead use std::getline. Then one way to to get the individual words is with std::stringstream, which allows a string to be treated as a stream.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5.  
  6. int main(void)
  7. {
  8.   std::string input;
  9.   std::getline(std::cin, input);
  10.  
  11.   std::stringstream inputStream(input);
  12.   std::vector<std::string> words;
  13.   std::string word; // temporary buffer to hold extracted word
  14.   while (inputStream >> word)
  15.     words.push_back(word);
  16.  
  17.   for (size_t i = 0; i < words.size(); ++i)
  18.     std::cout << words[i] << std::endl;
  19.  
  20.   return 0;
  21. }
  22.  
I used a std::vector for the list, but if you actually want to use std::list, replace
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
with
Expand|Select|Wrap|Line Numbers
  1. #include <list>
Replace
Expand|Select|Wrap|Line Numbers
  1. std::vector<std::string> words;
with
Expand|Select|Wrap|Line Numbers
  1. std::list<std::string> words;
And to iterate the list replace
Expand|Select|Wrap|Line Numbers
  1. for (size_t i = 0; i < words.size(); ++i)
  2.     std::cout << words[i] << std::endl;
with
Expand|Select|Wrap|Line Numbers
  1.  for (auto it = words.begin(); it != words.end(); ++it)
  2.     std::cout << *it << std::endl;
  3.  

Share this Question
Share on Google+
1 Reply


P: 15
First off, you won't be able to get a string with spaces using std::cin like that. It will only get the first word. Instead use std::getline. Then one way to to get the individual words is with std::stringstream, which allows a string to be treated as a stream.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5.  
  6. int main(void)
  7. {
  8.   std::string input;
  9.   std::getline(std::cin, input);
  10.  
  11.   std::stringstream inputStream(input);
  12.   std::vector<std::string> words;
  13.   std::string word; // temporary buffer to hold extracted word
  14.   while (inputStream >> word)
  15.     words.push_back(word);
  16.  
  17.   for (size_t i = 0; i < words.size(); ++i)
  18.     std::cout << words[i] << std::endl;
  19.  
  20.   return 0;
  21. }
  22.  
I used a std::vector for the list, but if you actually want to use std::list, replace
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
with
Expand|Select|Wrap|Line Numbers
  1. #include <list>
Replace
Expand|Select|Wrap|Line Numbers
  1. std::vector<std::string> words;
with
Expand|Select|Wrap|Line Numbers
  1. std::list<std::string> words;
And to iterate the list replace
Expand|Select|Wrap|Line Numbers
  1. for (size_t i = 0; i < words.size(); ++i)
  2.     std::cout << words[i] << std::endl;
with
Expand|Select|Wrap|Line Numbers
  1.  for (auto it = words.begin(); it != words.end(); ++it)
  2.     std::cout << *it << std::endl;
  3.  
Dec 10 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.