Skip to main content

E.A.1 C++ Programming Basics

C++ runtime and memory model diagram

You do not need to become a C++ expert before reading deployment code. First learn the small subset that appears again and again: types, functions, std::vector, references, compilation, and clear output.

Run the smallest inference-style program

Create demo.cpp:

#include <iostream>
#include <vector>

int main() {
std::vector<float> logits = {1.2f, 0.3f, 2.1f};
int best_index = 0;

for (int i = 1; i < static_cast<int>(logits.size()); ++i) {
if (logits[i] > logits[best_index]) {
best_index = i;
}
}

std::cout << "best_class=" << best_index << "\n";
std::cout << "score=" << logits[best_index] << "\n";
return 0;
}

Run it:

c++ -std=c++17 demo.cpp -o demo
./demo

Expected output:

best_class=2
score=2.1

What to notice

C++ ideaDeployment meaning
std::vector<float>A simple tensor-like container
explicit type float / intThe compiler must know data shapes and value types
static_cast<int>(...)Convert types deliberately instead of hoping it works
compile then runDeployment usually produces a binary, not just a script
printed resultEvery deployment test needs reproducible evidence

Practice change

Change the logits to:

std::vector<float> logits = {3.4f, 0.3f, 2.1f};

Run again. The expected best_class should become 0.

Pass check

You pass this lesson when you can compile the file, change the input values, explain why the selected class changed, and say what std::vector<float> represents in an inference program.