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.
// 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.
Next removal: 5. Size: 3.
Guided simulation · Change the inputs to test your understanding.
Which declaration creates four elements, each equal to two?
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.
// 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
Mark complete when you can explain it yourself.