Vectors in functions and grids
Choose copies or references deliberately and traverse nested vectors without assuming every row matches.
The vector knows its own size
A vector parameter carries a size() operation. A helper usually does not need a separate n unless it intentionally uses only a prefix. const std::vector<int>& reads the original, std::vector<int>& can modify it, and std::vector<int> receives an independent value.
Passing an existing n-element vector by value usually copies its elements, costing O(n) work and storage. A helper called q times to ask only for size can accidentally incur O(qn) copy work. A const reference avoids that copy but not any scan in the helper body.
Copies can be part of a correct plan
To print sorted values while preserving their original order, copy the vector, then sort the copy. Both sequences stay alive, so the extra copy needs linear storage. A returned local vector can safely be returned by value; C++ can elide or move that result.
Do not return a reference to a local vector after its lifetime ends. Vector equality compares matching contents, not object identity. Lexicographic order compares the first differing element, with the shorter equal prefix first.
Each nested row is a separate vector
vector<vector<int>> grid(rows, vector<int>(cols)) creates independent rows with cols zero-valued cells each. grid[i][j] requires both a valid row and a valid index within that row. The rows need not share one contiguous rectangular integer block.
Use auto& row and int& x when reading into the stored grid. Without the outer reference, input modifies a copied row. Ragged vectors may have different row lengths, including empty rows, so traverse each row’s own size. With r rows and m total cells, logical storage is O(r + m); row objects still exist when rows are empty.
// Complete C++23 program. No input; output: 11 9 0
#include <iostream>
#include <vector>
int main() {
std::vector<std::vector<int>> groups{{4, 7}, {9}, {}};
for (int i = 0; i < static_cast<int>(groups.size()); ++i) {
long long total = 0;
for (int x : groups[i]) total += x;
if (i != 0) std::cout << ' ';
std::cout << total;
}
std::cout << '\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.
What does for (auto row : grid) do differently from for (auto& row : grid)?
Turn the idea into code
Read positive rows and cols, each at most 300, then a matrix with values in [−10⁹, 10⁹]. Store it as a nested vector and print column sums from left to right.
Give me a hint
For each column, reset one long long total, then visit that column in every row.
Show the explained solution
The inner loop visits exactly the requested column. Repeating across all columns visits every cell once, giving O(rows × cols) processing time and stored cells. Wide totals handle sums exceeding int.
// Complete C++23 program. Input: rows cols, then the matrix row by row.
#include <iostream>
#include <vector>
int main() {
int rows = 0, cols = 0;
std::cin >> rows >> cols;
std::vector<std::vector<int>> grid(rows, std::vector<int>(cols));
for (auto& row : grid) {
for (int& x : row) std::cin >> x;
}
for (int col = 0; col < cols; ++col) {
long long total = 0;
for (int row = 0; row < rows; ++row) total += grid[row][col];
if (col != 0) std::cout << ' ';
std::cout << total;
}
std::cout << '\n';
}Your notes
Mark complete when you can explain it yourself.