SoFunction
Updated on 2025-04-13

Various methods of C++ string extraction and segmentation

1. Basic method of string extraction

1.1 Use the std::istringstream and the >> operators

std::istringstreamis a class in the C++ standard library that processes strings as input streams. pass>>Operator, we can extract space-separated words or numbers from streams.

Sample code

#include <iostream>
#include <sstream>
#include <string>
 
int main() {
    std::string s = "id13 id1 id6 id0 id8 id6 id0";
    std::istringstream iss(s);
    std::string token;
 
    while (iss >> token) {
        std::cout << token << std::endl;
    }
 
    return 0;
}

Output

id13
id1
id6
id0
id8
id6
id0

analyze

  • iss >> tokenThe string will be separated by spaces and words will be extracted one by one.

  • This method is suitable for simple scenarios where words in strings are separated by spaces.

1.2 Extract the number after id

If needed, from similar"id13 id1 id6"Extract from the stringidThe following numbers can be usedstd::string::substrmethod.

Sample code

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
 
int main() {
    std::string s = "id13 id1 id6 id0 id8 id6 id0";
    std::istringstream iss(s);
    std::string token;
    std::vector<int> ids;
 
    while (iss >> token) {
        if ((0, 2) == "id") {
            int id = std::stoi((2));
            ids.push_back(id);
        }
    }
 
    for (int id : ids) {
        std::cout << id << std::endl;
    }
 
    return 0;
}

Output

13
1
6
0
8
6
0

analyze

  • (2)fromtokenThe second character of  starts the substring and skip"id"

  • RegardlessidIs the following number one, two or three digits?substr(2)All can be extracted correctly.

  • This method is simple and efficient, and is suitable for extracting numbers after fixed prefixes.

2. Handle complex separators

2.1 Use std::getline to customize the separator

If the delimiter of a string is not a space (for example, a comma,or semicolon;), can be usedstd::getlineand specify a separator.

Sample code

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
 
int main() {
    std::string s = "id13,id1,id6,id0,id8,id6,id0";
    std::istringstream iss(s);
    std::string token;
    std::vector<int> ids;
 
    while (std::getline(iss, token, ',')) {
        if ((0, 2) == "id") {
            int id = std::stoi((2));
            ids.push_back(id);
        }
    }
 
    for (int id : ids) {
        std::cout << id << std::endl;
    }
 
    return 0;
}

Output

13
1
6
0
8
6
0

analyze

  • std::getline(iss, token, ',')The strings will be separated by commas and words will be extracted one by one.

  • This method is suitable for scenarios where custom delimiters are handled.

2.2 Processing multiple lines of input

If the input is multiple lines,std::getlineYou can also extract content by line.

Sample code

#include <iostream>
#include <sstream>
#include <string>
 
int main() {
    std::string s = "id13 id1 id6\nid0 id8 id6\nid0";
    std::istringstream iss(s);
    std::string line;
 
    while (std::getline(iss, line)) {
        std::istringstream lineStream(line);
        std::string token;
        while (lineStream >> token) {
            std::cout << token << std::endl;
        }
    }
 
    return 0;
}

Output

id13
id1
id6
id0
id8
id6
id0

analyze

  • Outer layerstd::getlineExtract content by line.

  • Inner layerlineStream >> tokenSeparate the words on each line by space.

  • This method is suitable for scenarios where multiple lines of input are processed.

3. Advanced string processing skills

3.1 Using regular expressions

C++11 has been introduced<regex>Library, supports regular expression matching, and can handle strings more flexibly.

Sample code

#include <iostream>
#include <regex>
#include <string>
#include <vector>
 
int main() {
    std::string s = "id13 id1 id6 id0 id8 id6 id0";
    std::regex pattern(R"(id(\d+))");
    std::smatch matches;
    std::vector<int> ids;
 
    auto words_begin = std::sregex_iterator((), (), pattern);
    auto words_end = std::sregex_iterator();
 
    for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
        std::smatch match = *i;
        int id = std::stoi((1));
        ids.push_back(id);
    }
 
    for (int id : ids) {
        std::cout << id << std::endl;
    }
 
    return 0;
}

Output

13
1
6
0
8
6
0

analyze

  • Using regular expressionsR"(id(\d+))"MatchidThe number behind.

  • This method is powerful, but has a complex syntax, and is suitable for handling complex string matching tasks.

3.2 Performance optimization

For large-scale data processing, performance can become a bottleneck. It can be optimized by:

  • Avoid frequent creation and destructionstd::istringstreamObject.

  • usestd::string_view(C++17) Reduce string copying.

Sample code

#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
 
int main() {
    std::string s = "id13 id1 id6 id0 id8 id6 id0";
    std::istringstream iss(s);
    std::string token;
    std::vector&lt;int&gt; ids;
    (10); // Preallocate space 
    while (iss &gt;&gt; token) {
        if ((0, 2) == "id") {
            int id = std::stoi((2));
            ids.push_back(id);
        }
    }
 
    for (int id : ids) {
        std::cout &lt;&lt; id &lt;&lt; std::endl;
    }
 
    return 0;
}

analyze

  • Pre-allocatedidsThe space can reduce the overhead of dynamic memory allocation.

  • usestd::string_viewUnnecessary string copying can be avoided.

4. Summary

This article introduces in detail various methods of string extraction and segmentation in C++, including:

  1. usestd::istringstreamand>>Operators separate strings by spaces.

  2. usestd::getlineProcess custom separators and multi-line input.

  3. Use regular expressions to handle complex string matching tasks.

  4. Improve code efficiency through performance optimization techniques.

Each method has its applicable scenarios and advantages and disadvantages, and developers can choose the appropriate method according to specific needs. By mastering these techniques, you will be able to efficiently handle various string tasks, improving the readability and performance of your code.

The above is the detailed content of various methods of C++ string extraction and segmentation. For more information about C++ string extraction and segmentation, please pay attention to my other related articles!