Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 74553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:22:35+00:00 2026-05-10T20:22:35+00:00

How do I iterate over the words of a string composed of words separated

  • 0

How do I iterate over the words of a string composed of words separated by whitespace?

Note that I’m not interested in C string functions or that kind of character manipulation/access. I prefer elegance over efficiency. My current solution:

#include <iostream> #include <sstream> #include <string>  using namespace std;  int main() {     string s = "Somewhere down the road";     istringstream iss(s);      do {         string subs;         iss >> subs;         cout << "Substring: " << subs << endl;     } while (iss); } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:22:35+00:00Added an answer on May 10, 2026 at 8:22 pm

    For what it’s worth, here’s another way to extract tokens from an input string, relying only on standard library facilities. It’s an example of the power and elegance behind the design of the STL.

    #include <iostream> #include <string> #include <sstream> #include <algorithm> #include <iterator>  int main() {     using namespace std;     string sentence = 'And I feel fine...';     istringstream iss(sentence);     copy(istream_iterator<string>(iss),          istream_iterator<string>(),          ostream_iterator<string>(cout, '\n')); } 

    Instead of copying the extracted tokens to an output stream, one could insert them into a container, using the same generic copy algorithm.

    vector<string> tokens; copy(istream_iterator<string>(iss),      istream_iterator<string>(),      back_inserter(tokens)); 

    … or create the vector directly:

    vector<string> tokens{istream_iterator<string>{iss},                       istream_iterator<string>{}}; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to iterate over the characters in a string to build an XML
I'm looking for a way to iterate over each word within a string gathered
Earlier today I needed to iterate over a string 2 characters at a time
I have a Map[Long, String] which I would like iterate over in descending order
I have a map<string, list<int> > which I want to iterate over the list
I'm wondering if there's some sort of iterator that can iterate over values in
I have some string like 1;2;3;4;5 I want to be able to iterate over
I'm trying to iterate over a value list and convert it into a string.
I am not too familiar with RE but I am trying to iterate over
I iterate over an array of CartItem class I created and populate a view

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.