std::istringstream can split a line into whitespace-separated tokens.

token A token is one piece of text separated by whitespace or another delimiter.
istringstream An input string stream lets code read from a string with the same `>>` operator used for input streams.

Token Stream Count

line
token_stream_count.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string line = "red blue";

    std::istringstream input(line);
    std::string token;
    int count = 0;
    while (input >> token) {
        count += 1;
    }

    std::cout << "line=" << line << std::endl;
    std::cout << "count=" << count << std::endl;
    return 0;
}
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string line = "red blue green";

    std::istringstream input(line);
    std::string token;
    int count = 0;
    while (input >> token) {
        count += 1;
    }

    std::cout << "line=" << line << std::endl;
    std::cout << "count=" << count << std::endl;
    return 0;
}
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string line = "solo";

    std::istringstream input(line);
    std::string token;
    int count = 0;
    while (input >> token) {
        count += 1;
    }

    std::cout << "line=" << line << std::endl;
    std::cout << "count=" << count << std::endl;
    return 0;
}
  1. line ← red blue, input ← (empty), token ← (empty), count ← 0

    5int main() {6    std::string line→ red blue = "red blue"; //@line="red blue green", "solo"78    std::istringstream input→ (empty)(linered blue);9    std::string token→ (empty);10    int count→ 0 = 0;11    while (input >> token) {
  2. count ← 1

    pass 1 of 2
    10int count = 0;11while (input(empty) >> tokenred) {12    count→ 1 += 1;13}
  3. count ← 2

    pass 2 of 2
    10int count = 0;11while (input(empty) >> tokenblue) {12    count→ 2 += 1;13}
  4. std::cout << "line=" << line << std::endl;

    15    std::cout << "line=" << linered blue << std::endl;16    std::cout << "count=" << count2 << std::endl;17    return 0;18}
    outputline=red blue
    count=2
  1. line ← red blue green, input ← (empty), token ← (empty), count ← 0

    5int main() {6    std::string line→ red blue green = "red blue green";78    std::istringstream input→ (empty)(linered blue green);9    std::string token→ (empty);10    int count→ 0 = 0;11    while (input >> token) {
  2. count ← 1

    pass 1 of 3
    10int count = 0;11while (input(empty) >> tokenred) {12    count→ 1 += 1;13}
    All 3 passes — pass 1 is the card above
    passtokencount
    1red0 1
    2blue1 2
    3green2 3
  3. std::cout << "line=" << line << std::endl;

    15    std::cout << "line=" << linered blue green << std::endl;16    std::cout << "count=" << count3 << std::endl;17    return 0;18}
    outputline=red blue green
    count=3
  1. line ← solo, input ← (empty), token ← (empty), count ← 0

    5int main() {6    std::string line→ solo = "solo";78    std::istringstream input→ (empty)(linesolo);9    std::string token→ (empty);10    int count→ 0 = 0;11    while (input >> token) {
  2. count ← 1

    10int count = 0;11while (input(empty) >> tokensolo) {12    count→ 1 += 1;13}
  3. std::cout << "line=" << line << std::endl;

    15    std::cout << "line=" << linesolo << std::endl;16    std::cout << "count=" << count1 << std::endl;17    return 0;18}
    outputline=solo
    count=1