Summary of C + + string segmentation methods

In some programming exercises, the string is often processed, and the string is often segmented to extract each part of the information before processing. Although C + + does not provide a direct string segmentation function such as split like python, there are some other methods to segment it. The following describes several string segmentation methods commonly used in C + +.

Implemented by string member function

In the C++ string class, two member functions are provided to implement string segmentation. One is the find function and the other is the substr function. Let's take a look at the declarations of these two functions.
find function:
Prototype: size_ t find (const string& str, size_t pos = 0) const;
Function: find the position where the substring first appears.
Parameter Description: str is the substring and pos is the initial search position.
Return value: if found, the first occurrence position is returned; otherwise, string::npos is returned.
substr function:
Prototype: string substr (size_t pos = 0, size_t len = npos) const;
Function: intercept substrings in the original string.
Parameter Description: pos is the starting position and len is the length of the substring to be intercepted.
Return value: substring.
The following code implements the string segmentation function:

vector<string> split(const string &str, const string &pattern)
{
    vector<string> res;
    if(str == "")
        return res;
    //A separator is also added at the end of the string to intercept the last paragraph
    string strs = str + pattern;
    size_t pos = strs.find(pattern);

    while(pos != strs.npos)
    {
        string temp = strs.substr(0, pos);
        res.push_back(temp);
        //Remove the split string and split it in the remaining string
        strs = strs.substr(pos+1, strs.size());
        pos = strs.find(pattern);
    }

    return res;
}

Implemented by strtok function

strtok is a string segmentation function in C language. Its specific explanation is as follows:
Prototype: char * strtok (char * STR, const char * delimiters);
Function: split string str, delimiters is the specified delimiter, and there can be multiple.
Note: strtok can only accept C-style strings. If it is a string type, you can use c_str function. Strtok () is used to split a string into fragments. Parameter s points to the string to be split, and parameter delim is the split string. When strtok() finds the split character of parameter delim in the string of parameter s, it will change the character to \ 0 character. In the first call, strtok () must give the parameter s string, and in subsequent calls, set the parameter s to NULL. Each successful call returns a pointer to the fragment being split.
The implementation code is as follows:

vector<string> split2(const string &str, const string &pattern)
{
    char * strc = new char[strlen(str.c_str())+1];
    strcpy(strc, str.c_str());   //Convert string to C-string
    vector<string> res;
    char* temp = strtok(strc, pattern.c_str());
    while(temp != NULL)
    {
        res.push_back(string(temp));
        temp = strtok(NULL, pattern.c_str());
    }
    delete[] strc;
    return res;
}

Implemented by stringstream

Stringstream is a string input / output stream, which inherits from iostream. Using stringstream flexibly can complete many string processing functions, such as string and other types of conversion, string segmentation, etc. Here, we use its implementation string segmentation function. Note that the use of tingstream needs to include s sstream header files.

vector<string> split3(const string &str, const char pattern)
{
    vector<string> res;
    stringstream input(str);   //Read str into string stream
    string temp;
    //Use the getline function to read from the string stream and stop when a delimiter is encountered, which is similar to reading from cin
    //Note that getline can read spaces by default
    while(getline(input, temp, pattern))
    {
        res.push_back(temp);
    }
    return res;
}

For details on stringstream, see:
http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream



Author: litexy
Link: https://www.jianshu.com/p/5876a9f49413
Source: Jianshu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

Keywords: C++

Added by ailgup on Tue, 14 Dec 2021 23:53:30 +0200