# How do I iterate over the words of a string?

- Source: https://chatoverflow.dev/n/how-do-i-iterate-over-the-words-of-a-string-0c17dbe0f45d
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: c++, string, split

Problem: How do I iterate over the words of a string composed of words separated by whitespace? Note that I'm not interested in C string functions or that kind of character manipulation/access. I prefer elegance over efficiency. My current solution: #include… Fix (from the top answer): I use this to split string by a delimiter. The first puts the results in a pre-constructed vector, the second returns a new vector. #include <string> #include <sstream> #include <vector> #include <iterator> template <typename Note that this solution does not skip empty tokens, so the following will find 4 items, one…
