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 6154131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:12:42+00:00 2026-05-23T20:12:42+00:00

Here’s the code. I have no idea why it doesn’t recognize that it needs

  • 0

Here’s the code. I have no idea why it doesn’t recognize that it needs to copy the memory, and I can’t force it to.

 string message="The quick brown fox jumped over the lazy dog.";

  vector<char*> words;

  while(message.length()>0){
    char wtf[message.substr(0,message.find(" ")).c_str().length()]=message.substr(0,message.find(" ")).c_str();
    words.push_back(wtf);
    message=message.substr(message.find(" ")+1);
  }

I see that there are similar threads, but none on this. Also, it seems like a shortcomming that C++ can’t deal with this that easily.

  • 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. Editorial Team
    Editorial Team
    2026-05-23T20:12:43+00:00Added an answer on May 23, 2026 at 8:12 pm

    How to break text into words (the easy way)

    #include <string>
    #include <sstream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    
    int main()
    {
        std::string message="The quick brown fox jumped over the lazy dog.";
    
        std::vector<std::string>    words;
    
        std::stringstream   stream(message);                  // 1: Create a stream
        std::copy(std::istream_iterator<std::string>(stream), // 2: Copy words from the stream
                  std::istream_iterator<std::string>(),
                  std::back_inserter(words));                 //    into the back of the vector.
    }
    

    A break down on how it works (for 12 year old’s learning to program)

    • The operator >> when applied to a stream (and a string) reads a single (white) space separated word.

      std::string message="The quick brown fox jumped over the lazy dog.";
      std::stringstream   stream(message);
      
      std::string  word;
      stream >> word; // Reads "The"
      stream >> word; // Reads "quick"
      stream >> word; // Reads "brown" etc...
      
    • The istream_iterator is an adapter for streams that make them look like containers.
      It reads items from the stream of type ‘T’ using the operator >>

      std::stringstream   stream("The quick brown fox jumped over the lazy dog.");
      std::istream_iterator<std::string> i(stream);
      
      std::string  word;
      
      word = *i;      // de-reference the iterator to get the object.   Reads "The"
      ++i;
      word = *i; ++i; // Reads "quick"
      word = *i; ++i; // Reads "brown" etc
      
      // Works for any type that uses >> to read from the stream 
      std::stringstream   intstream("99 40 32 64 20 10 9 8 102");
      std::istream_iterator<int> i(stream);  // Notice the type is int here
      
      int   number;
      
      number = *i;      // de-reference the iterator to get the object.   Reads "99"
      ++i;
      number = *i; ++i; // Reads "44"
      number = *i; ++i; // Reads "32" etc
      
    • The standard algorithms all work on iterators.
      std::copy iterates over the source and places each item in the destination:

      int    src[] = { 1, 2, 3, 4, 5, 6 };
      int    dst[] = { 0, 0, 0, 0, 0, 0 };
      
      std::copy(src, src+6, dst); // copies src into dst
                                  // It assumes there is enough space in dst
      
    • The back_inserter is an adapter that uses push_back to add items to the container.
      We could have made sure that the destination vector was the correct size. But it is easier to use the back_inserter to make sure the vector is dynamically sized.

      int    src[] = { 1, 2, 3, 4, 5, 6 };
      std::vector<int> dst; // Currently has zero size
      
      std::copy(src, src+6, std::back_inserter(dst)); // copies src into dst
                                                      // back_inserter expands dst to fit
                                                      // by using push_back
      
    • Putting it all back together:

      // Create a stream from the string
      std::stringstream   stream(message);
      
      // Use std::copy to copy from the string.
      //     The stream is iterated over a word at a time.
      //     because the istream iterator is using std::string type.
      //
      //     The istream_iterator with no parameters is equivelent to end.
      //
      //     The destination appends the word to the vector words.
      std::copy(std::istream_iterator<std::string>(stream), // 2: Copy words from the stream
                std::istream_iterator<std::string>(),
                std::back_inserter(words));                 //    into the back of the vector.
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a strange question for you guys, I have a nice sorted list that
here a a piece of code that is supposed to loop over and over
Here's my code $string = preg_replace(/rad\:([0-9]+)px\;\s+\/\*\sALT\[(.+)\*\/|rad\:([0-9]+)px\;/,($2?$2:$1),$string); Basically, in the regex I've got a pipe
Here is what I am trying to achieve in PHP: I have this string:
here's the code: #include <string> class Config { public: static const std::string asdf =
Here is a snippet of my JavaScript Code that I want to DRY up:
Here's the abstract situation: I have CoreData objects 'car' and 'review'.'review' can be 'good','bad'
Here's a basic regex technique that I've never managed to remember. Let's say I'm

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.