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

  • Home
  • SEARCH
  • 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 9226745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:54:12+00:00 2026-06-18T04:54:12+00:00

I’m trying to make a function to split a string, Split At Spaces, into

  • 0

I’m trying to make a function to split a string, “Split At Spaces”, into a vector which would contain “Split” “At” “Spaces”. So far, this is the code I’ve got.

#include <iostream>
#include <utility>
#include <algorithm>

using namespace std;

std::vector<std::string> split(std::string * s, char * tosplit) 
{
    size_t i = 0;
    int count = 0;
    size_t contain;
    std::vector<std::string> split;

    std::cout << "Start" << std::endl;
    std::cout << *s << std::endl;
    std::cout << *tosplit << std::endl;

    while((contain = s->find(*tosplit,i)) != std::string::npos)
    {
        count++;
        i = contain + 1;
    }

    std::cout << "Contains " << count << std::endl;

    if (count == 0)
    {
        std::cout << "Equals 0" << std::endl;
        split = std::vector<std::string>(1);
        split.at(0) = s->c_str();
        return split;
    }

    split = std::vector<std::string>(count + 1);
    split.begin();

    int lasti;
    i = s->find_first_of(*tosplit);
    split.at(0) = s->substr(0, i);
    lasti = i;
    int runs = 1;

    while (runs <= count) 
    {
        i = s->find(*tosplit, lasti + 1);
        std::cout << i << " " << lasti << std::endl;
        split.at(runs) = s->substr(lasti, --i);
        runs++;
        lasti = i;
    }

    split.at(runs) = s->substr(lasti, s->size());

    std::cout << "done, result is" << std::endl;
    i = 0;
    while (i < split.capacity()) 
    {
        std::cout << split.at(i) << std::endl;
        i++;
    }

    return split;
}

It throws an out_of_range exception. Any help you can give would be appreciated. This is like my first part using pointers in a function so I’m kinda guessing here.
Thanks!

Please don’t suggest using x or y method, I’d like to write my own as I’m doing it for the experience.

  • 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-06-18T04:54:13+00:00Added an answer on June 18, 2026 at 4:54 am

    Single Delimiter:

    You wrote much too much code to do this. You can do it in a few lines. You are getting very over complicated. And there’s no reason to really do anything with pointers for this.

    vector<string> Split(string s, char delim)
    {
        vector<string> strings;
        for(istringstream ss(s); getline(ss, s, delim); strings.push_back(move(s)));
        return strings;
    }
    

    Multiple Delimiters:

    A solution for using multiple delimiters is more complicated. You can no longer leverage off of getline, which means you’re basically writing part of getline‘s functionality yourself. But still, it can be quite short.

    vector<string> Split(const string& s, const char* delims)
    {
        vector<string> strings;
    
        for(string::size_type start = 0, end; end != string::npos && start < s.size(); start = end+1)
        {
            end = s.find_first_of(delims, start);
            strings.push_back(s.substr(start, end-start));
        }
    
        return strings;
    }
    

    This will add blank strings when delimiters are next to each-other. If that’s not the desired behavior for adjacent delimiters, this can be easily avoided by guarding the push_back with if(start != end).

    Conclusion:

    When you are starting to write a low level algorithm like this, pseudo code it out in broad terms and then before writing any code check what the C++ standard library can provide to cut out parts or all of your work. You’ll end up with smaller, less error prone, and more understandable code. No one wants to see a hand rolled implementation for find_first_of, for example. It’s much more clear to read the words find_first_of. It’s clear what that function is going to do, and that it’s bug free (hopefully).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.