简体   繁体   中英

How would I store an empty string into a vector

I am new to C++ and I am having trouble splitting a string by a delimiter and putting the substrings into a vector.

My code is as follows:

vector<string> split(const string &s, const string &delim)
{   
    string::size_type pos = s.find_first_of(delim,0);
    int start = 0;
    vector<string> tokens;

    while(start < s.size())
    {
            if(start++ != pos + 1)
                    tokens.push_back(" ");
            pos = s.find_first_of(delim, start);
            tokens.push_back(s.substr(start, pos - start));
    }

    for(vector<string>::size_type i = 0; i != tokens.size(); ++i)
            cout << tokens[i];

    return tokens;
}

a string and a delimiter are passed into the function and and performs the splitting. This function is suppose to put empty strings into the vector but does not do it for me.

for example if I call the function in main as:

int main()
{
   split("<ab><>cd<", "<>");
}

the output is suppose to be

"","ab","","","","cd",""

minus the quotes

but the output for my code currently is

ab b    cd d  

any help would be appreciated.

This does the trick...

#include <iostream>
#include <vector>

using namespace std;

vector<string> split(string record, string token) {
    vector<string> results;
    size_t startPos = 0;
    size_t pos = 0;

    // Step: If either argument is empty then return
    // an empty vector.
    if (record.length() == 0 || token.length() == 0) {
        return results;
    }

    // Step: Go through the record and split up the data.
    while(startPos < record.length()) {
        pos = record.find(token, startPos);
        if (pos == string::npos) {
            break;
        }

        results.push_back(record.substr(startPos, pos - startPos));
        startPos = pos + token.length();
    }

    // Step: Get the last (or only bit).
    results.push_back(record.substr(startPos, record.length() - startPos));

    // Step: Return the results of the split.
    return results;
}

void printData(vector<string> list) {
    for(vector<string>::iterator it = list.begin(); it < list.end(); it++) {
        cout << *it << endl;
    }
}

int main(int argc, char** argv) {
    string record = "";
    string delim = "";

    if (argc == 3) {
        record = argv[1];
        delim = argv[2];
        printData(split(record,delim));
    } else {
        string record = "comma,delimited,data";
        string delim = ",";
        printData(split(record,delim));

        record = "One<--->Two<--->Three<--->Four";
        delim = "<--->";
        printData(split(record,delim));
    }
}

It seems your loop doesn't quite do the right thing: you walk character by character, advancing start by one during each iteration. I would suspect that you actually want to have a current position, find the next delimiter, add the string between the current position and the delimiter to the result, and make the current position the character after the delimiter:

for (std::string::size_type start(0); start != s.npos; )
{
    std::string::size_type end(s.find_first_of(delim, start));
    tokens.push_back(s.substr(start, end != s.npos? end - start: end));
    start = end != s.npos? end + 1: s.npos;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM