C++ Data StructuresLesson 48Free preview
C++ Vectors
This lesson is part of C++ · All levels
Summary
Vectors are dynamic containers that store sequential elements in memory, such as arrays but can increase in size as the program runs. We can add with push_back(), delete with pop_back(), and access through the operator [] or at() that checks the boundary. Because it is flexible and remains fast for random access, vectors become a natural choice for variable-length data lists.
Key points
- Push_back() adds the element at the end, pop_back() removes the last element.
- At() checks the index and throws it out_of_range, whereas [] does not.
- Returns the number of elements, empties the entire contents.
- Range-based for simplifies iteration: for (auto x : v) { ... }.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.