← Back to archive

Stdle #174 · classes · 2026-11-27

What does this C++ snippet output?

A past puzzle — fully playable. 4 attempts, hints on wrong guesses.

#include <iostream>
#include <vector>
struct P { int x; };
int main() {
std::vector<P> v = {{1}, {2}, {3}};
for (auto& p : v) p.x *= 2;
std::cout << v[0].x << v[1].x << v[2].x << "\n";
return 0;
}
Classes
4 attempts left

Answer & explanation