One simple non-copying/non-allocating tokenizer is the abominable std::strtok
The following beats your python program in my tests
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cstring>
int main()
{
std::cout.sync_with_stdio(false); // we don't need C in the picture
std::string line;
getline(std::cin, line);
int num_cases = stoi(line);
std::vector<char*> words;
for(int n = 0; getline(std::cin, line) && n < num_cases; ++n)
{
words.clear();
char* p = std::strtok(&line[0], " ");
while (p) {
words.push_back(p);
p = std::strtok(nullptr, " ");
}
std::cout << "Case #" << n + 1 << ": ";
reverse_copy(words.begin(), words.end(),
std::ostream_iterator<char*>(std::cout, " "));
std::cout << '\n'; // never std::endl!
}
}
PS: your C++ and python outputs don't match exactly; this program matches your C++ output