Using iterators with find() in C++

Using iterators with find() in C++

You know that iteration over the words can be done using different methods in C++. Also, you know that the std::stringstream is the most common method for iterating over the words of a string. Here is another method using the iterators and std::string::find(), which will give you more flexibility for this task. 

Using the iterators and find() 

The iterators and find() in C++ is a manual technique to find whitespace in the string and iterate over the words of that string. You can use this method when some specific delimiters are to be implemented. Also, it provides you with precise control over parsing and extracting words efficiently in C++ programming. It helps to handle the multiple delimiters and large strings. Unlike the std::stringstream, which totally depends on the whitespace as the delimiter, the std::string::find() helps you specify the custom delimiters. This method is more efficient for iterating over the words of the large strings as it avoids the overhead of stream-based operations and potential errors.

Example 1:

Cpp
#include <iostream>
#include <string>
int main() {
std::string text = "Hello world, this is C++";
std::string::iterator it = text.begin(); // Iterator to traverse the string
std::size_t pos = 0, lastPos = 0;
while ((pos = text.find(' ', lastPos)) != std::string::npos) {
// Create a word using iterators from the last position to find space
std::string word(it + lastPos, it + pos);
std::cout << word << std::endl;
lastPos = pos + 1; // Move to the next word
}
// Print the last word (or the only word if no spaces were found)
std::cout << std::string(it + lastPos, text.end()) << std::endl;
return 0;
}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Using the iterators and find()

In this code, std::string::find() checks for whitespace, separates substrings into different words and iterates over them.

Example 2:

Cpp
#include <iostream>
#include <string>
int main() {
std::string text = "C++ provides multiple ways to process text";
std::size_t start = 0, end;
std::cout << "Words in the string:\n";
while ((end = text.find(' ', start)) != std::string::npos) {
std::cout << text.substr(start, end - start) << std::endl;
start = end + 1;
}
std::cout << text.substr(start) << std::endl; // Print the last word
return 0;
}
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Using the iterators and find()

In this code, str::string::find() is used to check for whitespace, separate substrings into different words, and iterate over them.

Conclusion

You know that every method used for iterating over the words of a string in C++ has its advantages, and the std::stringstream method is very simple and easy to use. But we have discussed that you can also use the iterators and std::string::find() to iterate over the words of a string because it provides us high flexibility and efficiency, as it handles the customized delimiters very well. This method also provides you with clear, readable, and concise code, which is very important for C++ programming. So, by learning this method, you can easily iterate over the words of a string in C++.

Some Other Methods to Iterate over Strings in C++

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner