1. Basic method of string extraction
1.1 Use the std::istringstream and the >> operators
std::istringstream
is 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 >> token
The 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 stringid
The following numbers can be usedstd::string::substr
method.
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)
fromtoken
The second character of starts the substring and skip"id"
。Regardless
id
Is 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::getline
and 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::getline
You 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 layer
std::getline
Extract content by line.Inner layer
lineStream >> token
Separate 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 expressions
R"(id(\d+))"
Matchid
The 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 destruction
std::istringstream
Object.use
std::string_view
(C++17) Reduce string copying.
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; (10); // Preallocate space 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; }
analyze
Pre-allocated
ids
The space can reduce the overhead of dynamic memory allocation.use
std::string_view
Unnecessary string copying can be avoided.
4. Summary
This article introduces in detail various methods of string extraction and segmentation in C++, including:
use
std::istringstream
and>>
Operators separate strings by spaces.use
std::getline
Process custom separators and multi-line input.Use regular expressions to handle complex string matching tasks.
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!