C++ · Beginner level

Beginner level · Module 3 of 5 · 10 guided hours

Vectors, Arrays and Strings

Store collections of values with std::vector and std::array, work with text using std::string, and use standard algorithms such as std::sort.

Learning objectives

  • Use std::vector and std::array to store and process data
  • Manipulate text with std::string
  • Apply basic standard algorithms to collections

Topics

  • std::vector and push_back
  • std::array and C arrays
  • range-based for
  • at() vs []
  • std::string operations
  • std::sort, std::accumulate, std::find

Lessons

  1. Lesson 1 std::vector and std::array

    Store many values in vectors that grow and fixed-size arrays, and loop over them safely.

    std::vectorpush_back and size()std::arrayundefined behaviour

  2. Lesson 2 Working with std::string

    Read, join, search and split text with std::string and handle whole lines of input.

    std::stringgetlinefind and substrstd::istringstream

  3. Lesson 3 Standard Algorithms on Collections

    Sort, sum, count and search collections with the <algorithm> and <numeric> headers instead of writing every loop by hand.

    iterators begin() and end()std::sortstd::accumulatemedian

Practical activity

Analyse a week of minibus fare records: store them in a vector, sort them and report the cheapest, dearest, mean and median fare.

Expected outcomes

  • Correct processing of collections with loops and algorithms
  • Safe text handling

Assessment

Practical: implement statistics functions over a vector; marked by test inputs including an empty vector.

Free taster lesson Arrays & Strings Read a free self-study lesson on this topic in the Free Course Library.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.