Learning pathOriginal reference
Lesson 1 of 12

Meet your standard-library toolkit

Choose an interface by the next operation you need, and construct vectors with the intended contents.

The same arrivals, different next items

For arrivals 4, 9, 2, a queue removes 4 next, a stack removes 2, and a default integer priority queue removes 9. Choose the tool from the required processing rule: arrival order, newest first, or greatest priority.

vector and deque are sequence containers. queue, priority_queue, and stack are adaptors that expose restricted interfaces over underlying containers. pair holds two related fields; it is a utility type rather than a variable-length container. The library supplies operations, while you still design the algorithm and its tie rules.

Include the tool you actually use

Use <vector>, <utility> for pair, <deque>, <queue> for both queue types, and <stack>. Algorithms such as sort and find need <algorithm>; accumulate needs <numeric>; greater needs <functional>. Use std:: explicitly in these examples.

The type in angle brackets selects the element type: std::vector<int> stores integers, while std::vector<std::string> stores strings and also needs <string>. size() asks for the current count, and empty() asks whether that count is zero. empty() never removes elements.

Parentheses create counts; braces list values

vector<int> a starts empty. vector<int> b(3) creates three zero-valued integers. vector<int> c(3, 7) creates three copies of seven. vector<int> d{3, 7} creates exactly the two listed values. These valid declarations do different jobs, so a construction mistake may still compile.

For a known input length, create n elements and fill them. To filter an unknown number of accepted values, start empty and push_back each accepted value. Constructing vector(n) and then pushing n inputs appends after the initial zeros and produces 2n elements.

C++23
// Complete C++23 program. No input; output: 0 3 3 2
#include <iostream>
#include <vector>
int main() {
    std::vector<int> empty;
    std::vector<int> zeros(3);
    std::vector<int> copies(3, 7);
    std::vector<int> listed{3, 7};
    std::cout << empty.size() << ' ' << zeros.size() << ' '
              << copies.size() << ' ' << listed.size() << '\n';
}

Container workbench

Insert values, then remove the next accessible item. Priority queue values are shown sorted for explanation, not as an internal heap.

80
31
52

Next removal: 5. Size: 3.

Guided simulation · Change the inputs to test your understanding.

Pause & predict

Which declaration creates four elements, each equal to two?

Choose an answer
Try it yourself

Turn the idea into code

Read 0 ≤ n ≤ 200000 integers and keep only strictly positive values, preserving order and duplicates. Print the count, then the kept values. Print a blank second line when none survive.

Give me a hint

Start with an empty vector. Append only when x > 0; zero is not positive.

Show the explained solution

After every input, the vector contains exactly the positive values seen so far in arrival order. The n input steps and amortized appends give O(n + 1) time and O(k + 1) stored elements, where k is the kept count.

C++23
// Complete C++23 program. Input: n, then n integers.
#include <iostream>
#include <vector>
int main() {
    int n = 0;
    std::cin >> n;
    std::vector<int> kept;
    for (int i = 0; i < n; ++i) {
        int x = 0;
        std::cin >> x;
        if (x > 0) kept.push_back(x);
    }
    std::cout << kept.size() << '\n';
    for (int i = 0; i < static_cast<int>(kept.size()); ++i) {
        if (i != 0) std::cout << ' ';
        std::cout << kept[i];
    }
    std::cout << '\n';
}

Your notes

Ready for the next idea?

Mark complete when you can explain it yourself.

From a problem to a programIdentify the input, output, and constraints, then translate a small plan into C++.Variables, types, and remembered valuesChoose a representation, initialize it, and understand what assignment really changes.Arithmetic without surprisesPredict division and remainder, widen before multiplication, and format decimal answers.Conditions and logical decisionsBuild exclusive categories, combine comparisons, and protect operations with short-circuit logic.Optional: other ways to chooseRecognize switch for exact choices and the conditional operator for choosing a value.For loops: make repetition preciseTrace initialization, condition, body, and update—and avoid an extra iteration.While, do-while, and stopping rulesChoose a loop from its stopping condition and read the exact input format.Break, continue, and early answersDistinguish leaving a loop from skipping one iteration, without losing required input.Five patterns worth understandingUse sums, counts, best-so-far candidates, products, and flags to process a stream.Nested loops: rows and columnsTranslate a shape into two loop roles and count the total work.Take a number apart, one digit at a timeUse remainder and division to process decimal digits, including zero.Multiple cases and a debugging mindsetCombine the patterns, isolate each case, and test the boundaries that can break your idea.Arrays: give every value a placeUnderstand indices, initialization, capacity, active length, and the first safe array loop.Scan, count, search, and compareDistinguish values from positions and preserve the exact tie rule a problem asks for.Change a sequence without losing itUpdate, copy, compare, reverse, and inspect neighbours using the correct bounds.Matrices: think in two coordinatesTraverse tables, total rows and columns, inspect diagonals, and protect grid neighbours.Characters become stringsSeparate characters from numbers, then safely inspect and edit managed text.Read the text the problem actually givesChoose words or full lines, preserve empty cases, and reset only the state that needs it.Scan text: count, filter, transformBuild a result from matching characters without losing leading zeros or skipping shifted text.Join, compare, and find textLearn content comparison, lexicographical order, substring counts, and the not-found sentinel.