Reading — step 1 of 5
Learn
std::vector
std::vector is C++'s growable array and the single most used container in the language — the C array's rough edges (fixed size, no length, silent overflows) each answered by design. If the course so far taught C++-the-syntax, vector begins C++-the-standard-library, where most real programs actually live.
Creating and growing
#include <vector>
std::vector<int> v; // empty
v.push_back(10); // grow at the back
v.push_back(20);
std::vector<int> nums = {1, 2, 3}; // literal init
std::vector<int> zeros(5, 0); // five zeros — (count, value)
v.size() // element count — it knows!
v.empty() // cleaner than size() == 0
v.back() // last element
v.pop_back() // remove last
The <int> is a template argument — vector<double>, vector<std::string>, vector<vector<int>> (a 2-D grid) all come from one implementation, type-checked at compile time. Chapter 6 shows you how that trick works; for now, enjoy that a vector of strings can't accidentally receive an int.
Memory honesty (this is still C++): a vector owns a heap buffer, grows it geometrically when full (occasionally copying everything — why push_back is "amortized" constant time), and frees it automatically when the vector dies. That last clause is RAII — the C++ idea that ownership lives in objects — quietly doing what C's malloc/free lesson made you do by hand.
Access: the two doors again
v[2] // fast, UNCHECKED — out of bounds = undefined behavior
v.at(2) // checked — out of bounds throws an exception, loudly
Same pair as std::string, same advice: [] where the index is provably fine (a loop bounded by size()), .at() when debugging something weird or handling untrusted indices. C's silent buffer overflow is still available through [] — C++ gives you the safe door, it doesn't force you through it.
Iteration
for (const auto& x : v) { std::cout << x << ' '; } // read — the default
for (auto& x : v) { x *= 2; } // modify in place
for (size_t i = 0; i < v.size(); i++) { … } // when the index matters
All of last lesson's loop guidance applies verbatim — const auto& as reflex. (size_t — unsigned — for index loops avoids the signed/unsigned warning; or int i with a cast, but pick a lane.)
Passing vectors: the reference payoff
long long sum(const std::vector<int>& v) { // const& — no million-element copy
long long total = 0;
for (int x : v) total += x;
return total;
}
A vector by value copies every element — legal, occasionally right, usually an accident. const std::vector<int>& is the professional signature, and it's last lesson's const& rule paying its first big dividend.
Your exercise: Largest Number
Read numbers (count given, or until input ends — check the spec), track the max. The two idioms:
// manual tracker — write this one
int best = v[0]; // seed from data, not from 0!
for (size_t i = 1; i < v.size(); i++)
if (v[i] > best) best = v[i];
// the library one-liner — know it exists
#include <algorithm>
int best = *std::max_element(v.begin(), v.end());
Seeding from v[0] survives all-negative input — the designated gotcha of every largest-number exercise on this platform. Reading until EOF, if the spec wants it: while (std::cin >> x) v.push_back(x); — the stream itself is the loop condition, false when input runs dry. That idiom alone is worth the lesson.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…