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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:59:22+00:00 2026-05-25T14:59:22+00:00

So we have a simple split : #include <iostream> #include <string> #include <vector> #include

  • 0

So we have a simple split:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

vector<string> split(const string& s, const string& delim, const bool keep_empty = true) {
    vector<string> result;
    if (delim.empty()) {
        result.push_back(s);
        return result;
    }
    string::const_iterator substart = s.begin(), subend;
    while (true) {
        subend = search(substart, s.end(), delim.begin(), delim.end());
        string temp(substart, subend);
        if (keep_empty || !temp.empty()) {
            result.push_back(temp);
        }
        if (subend == s.end()) {
            break;
        }
        substart = subend + delim.size();
    }
    return result;
}

or boost split. And we have simple main like:

int main() {
    const vector<string> words = split("close no \"\n matter\" how \n far", " ");
    copy(words.begin(), words.end(), ostream_iterator<string>(cout, "\n"));
}

how to make it oputput something like

close 
no
"\n matter"
how
end symbol found.

we want to introduce to split structures that shall be held unsplited and charecters that shall end parsing process. how to do such thing?

  • 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-25T14:59:22+00:00Added an answer on May 25, 2026 at 2:59 pm

    The following code:

    vector<string>::const_iterator matchSymbol(const string & s, string::const_iterator i, const vector<string> & symbols)
    {
        vector<string>::const_iterator testSymbol;
        for (testSymbol=symbols.begin();testSymbol!=symbols.end();++testSymbol) {
            if (!testSymbol->empty()) {
                if (0==testSymbol->compare(0,testSymbol->size(),&(*i),testSymbol->size())) {
                    return testSymbol;
                }
            }
        }
    
        assert(testSymbol==symbols.end());
        return testSymbol;
    }
    
    vector<string> split(const string& s, const vector<string> & delims, const vector<string> & terms, const bool keep_empty = true)
    {
        vector<string> result;
        if (delims.empty()) {
            result.push_back(s);
            return result;
        }
    
        bool checkForDelim=true;
    
        string temp;
        string::const_iterator i=s.begin();
        while (i!=s.end()) {
            vector<string>::const_iterator testTerm=terms.end();
            vector<string>::const_iterator testDelim=delims.end();
    
            if (checkForDelim) {
                testTerm=matchSymbol(s,i,terms);
                testDelim=matchSymbol(s,i,delims);
            }
    
            if (testTerm!=terms.end()) {
                i=s.end();
            } else if (testDelim!=delims.end()) {
                if (!temp.empty() || keep_empty) {
                    result.push_back(temp);
                    temp.clear();
                }
                string::const_iterator j=testDelim->begin();
                while (i!=s.end() && j!=testDelim->end()) {
                    ++i;
                    ++j;
                }
            } else if ('"'==*i) {
                if (checkForDelim) {
                    string::const_iterator j=i;
                    do {
                        ++j;
                    } while (j!=s.end() && '"'!=*j);
                    checkForDelim=(j==s.end());
                    if (!checkForDelim && !temp.empty() || keep_empty) {
                        result.push_back(temp);
                        temp.clear();
                    }
                    temp.push_back('"');
                    ++i;
                } else {
                    //matched end quote
                    checkForDelim=true;
                    temp.push_back('"');
                    ++i;
                    result.push_back(temp);
                    temp.clear();
                }
            } else if ('\n'==*i) {
                temp+="\\n";
                ++i;
            } else {
                temp.push_back(*i);
                ++i;
            }
        }
    
        if (!temp.empty() || keep_empty) {
            result.push_back(temp);
        }
        return result;
    }
    
    int runTest()
    {
        vector<string> delims;
        delims.push_back(" ");
        delims.push_back("\t");
        delims.push_back("\n");
        delims.push_back("split_here");
    
        vector<string> terms;
        terms.push_back(">");
        terms.push_back("end_here");
    
        const vector<string> words = split("close no \"\n end_here matter\" how \n far testsplit_heretest\"another split_here test\"with some\"mo>re", delims, terms, false);
    
        copy(words.begin(), words.end(), ostream_iterator<string>(cout, "\n"));
    }
    

    generates:

    close
    no
    "\n end_here matter"
    how
    far
    test
    test
    "another split_here test"
    with
    some"mo
    

    Based on the examples you gave, you seemed to want newlines to count as delimiters when they appear outside of quotes and be represented by the literal \n when inside of quotes, so that’s what this does. It also adds the ability to have multiple delimiters, such as split_here as I used the test.

    I wasn’t sure if you want unmatched quotes to be split the way matched quotes do since the example you gave has the unmatched quote separated by spaces. This code treats unmatched quotes as any other character, but it should be easy to modify if this is not the behavior you want.

    The line:

    if (0==testSymbol->compare(0,testSymbol->size(),&(*i),testSymbol->size())) {
    

    will work on most, if not all, implementations of the STL, but it is not gauranteed to work. It can be replaced with the safer, but slower, version:

    if (*testSymbol==s.substr(i-s.begin(),testSymbol->size())) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using g++ and having linker errors. I have a simple program in split into
Ok, i have simple scenario: have two pages: login and welcome pages. im using
In Java, I'm using the String split method to split a string containing values
So I have a simple split view that functions great except when the view
I have a reasonably simple split view application adapted from iPhone code. The main
I have a simple actionscript function var string:String = TEXT REMOVED; var myArray:Array =
I have simple regex \.*\ for me its says select everything between and ,
In general, is it a best practice to have simple POJO Java classes implement
I develop tools in Autodesk Maya. Many of the tools I build have simple
Should simple JavaBeans that have only simple getters and setters be unit tested?? What

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.