473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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.  

1 1198
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

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

Similar topics

13
by: Victor Hannak | last post by:
I am taking a program written in Borland C++ Builder 4 and converting the non-GUI related code to be generic c++ that can run anywhere. My main issue at this point is dealing with the string...
3
by: Chris | last post by:
Hi, I am filling a server side listbox in an asp.net app Each text value for each item in the list is created by concatenating to string values together. I want to give the appearance of...
8
by: Grant Wagner | last post by:
I'm a bit confused by String() (typeof 'string') vs new String() (typeof 'object'). When you need to access a method or property of a -String-, what type is JavaScript expecting (or rather, what...
6
by: Mark | last post by:
I've got a really messy text file that I need to work on and the only things separating each record is either "\r\n\r\n" or "Total:". I figure I won't be able to use strtok because it will split...
4
by: Emilio | last post by:
Question about Shared Sub Connect(server As , message As ) Why is in square brackets? Is it like Shared Sub Connect(server() As String, message() As String)
2
by: Brian Mitchell | last post by:
Hello, I am trying to create a property that can be set to a string or give the user a list of methods inside that property. For instance, I have the property -Location-. I would like the...
9
by: Fei Liu | last post by:
In Accellerated C++, the author recommends that in a header file one should not declare using std::string, using std::vector etc instead one should directly specify the namespace specifier in...
17
by: ramadu | last post by:
I know its a sin to use strings, lets skip that part... Which of these is faster and uses less memory? String.Format("SomeValue='{0}'", m_Value); or String.Concat("SomeValue='", m_Value,...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
7
by: HansWernerMarschke | last post by:
Is there a special function to insert a string into another string ? For example I want to exchange the german "Umlauts" ä,ö,ü with ae,oe and ue in a string. I also wonder if there is a function...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.