Skip to content
Learn Netverks

Lesson

Step 20/36 56% through track

vectors-cpp

std::vector

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches std::vector: the syntax, patterns, and safety habits you need before advancing in C++.

STL containers and algorithms replace hand-rolled loops—know complexity and iterator invalidation.

You will apply std::vector in contexts like: Game engines, quant libraries, and high-throughput services using standard containers.

Write C++ in main.cpp with int main(), click Run on server—the dev runner compiles with c++/g++ -std=c++17 -Wall and runs the binary; read template errors in stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

std::vector is a dynamic array—the default sequential container in modern C++. It manages memory, grows as needed, and works with algorithms and range-for.

Common operations

std::vector<int> v = {1, 2, 3};
v.push_back(4);
v.size();
v[0];  // unchecked
v.at(0);  // bounds-checked

Important interview questions and answers

  1. Q: vector reallocation?
    A: push_back may reallocate and invalidate iterators—use indices or refresh iterators after growth.
  2. Q: vector vs C array?
    A: Vector knows its size, manages lifetime, and integrates with STL algorithms safely.

Self-check

  1. How do you append an element?
  2. What happens to iterators on reallocation?

Tip: Call reserve(n) when you know final size—avoids repeated reallocations during push_back.

Interview prep

vector reallocation?

push_back may reallocate and invalidate iterators—refresh iterators after growth.

vector vs C array?

Vector tracks size, manages memory, and integrates with STL algorithms.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • push_back cost?
  • reserve when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump