Efficient traversal: Detailed explanations and examples of 3 methods to separate string words in C++ 

Overview: There are several ways to iterate over the words of a space-delimited string in C++, including using `std::istringstream`, manually iterating over characters, and regular expressions. Among them, `std::istringstream` is a simple and efficient choice to extract words through the stream. Iterating through characters manually is more tedious, the regular expression approach is more flexible but may have a performance overhead. Choose a method based on actual needs. This article provides clear example source code.

In C++, there are many ways to traverse a string consisting of space-separated words, including using std::istringstream in the C++ standard library , manually traversing characters, and using regular expressions. These methods will be introduced separately below, and detailed example source code will be provided.

Method 1: Use std::istringstream

#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string inputString = "Hello C++ World";
    std::istringstream iss(inputString);

    std::string word;
    while (iss >> word) {
        // 处理每个单词,例如输出
        std::cout << "Word: " << word << std::endl;
    }

    return 0;
}

This method uses std::istringstream , which converts the input string into an input stream-like object and then extracts each word via the >> operator.

Method 2: Manually traverse characters

#include <iostream>
#include <string>

int main() {
    std::string inputString = "Hello C++ World";

    std::string word;
    for (char c : inputString) {
        if (c != ' ') {
            word += c;
        } else {
            // 处理每个单词,例如输出
            std::cout << "Word: " << word << std::endl;
            word.clear();
        }
    }

    // 处理最后一个单词
    if (!word.empty()) {
        std::cout << "Word: " << word << std::endl;
    }

    return 0;
}

This method builds words one by one by manually traversing the characters in the input string, and processes the current word when spaces are encountered.

Method 3: Use regular expressions

#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string inputString = "Hello C++ World";

    std::regex wordRegex("\\S+");  // 匹配非空白字符的正则表达式

    std::sregex_iterator it(inputString.begin(), inputString.end(), wordRegex);
    std::sregex_iterator end;

    while (it != end) {
        // 处理每个匹配到的单词,例如输出
        std::cout << "Word: " << it->str() << std::endl;
        ++it;
    }

    return 0;
}

This method uses a regular expression to match non-whitespace characters to extract each word.

Method comparison

Of these three methods, using std::istringstream is usually the simplest and more efficient method because it takes full advantage of the capabilities of the C++ standard library. The manual method of traversing characters is relatively tedious, while the regular expression method may have some performance overhead, but is more flexible in some cases.

The selection method should be determined based on actual needs and performance requirements. If you are simply splitting space-separated words, std::istringstream is a good choice.