Chapter 8: Arrays & Strings

Arrays & Strings in C++: Storing Collections of Data Like a Pro!

Hello my amazing student! 🌟 Welcome to Lesson 8 β€” Arrays & Strings! This is a huge step forward because almost every real program needs to handle multiple pieces of data at once: list of students, scores, names, characters in a sentence, pixels in an image, etc.

Today we’ll cover everything you need to know as a beginner-to-intermediate C++ programmer:

  • C-style fixed-size arrays (the old-school way β€” still very important!)
  • std::array (modern, safe C++11+ replacement)
  • std::string (the powerful, easy-to-use string class)
  • std::string_view (C++17+ lightweight view β€” super useful and efficient!)

We’ll go very slowly, with tons of examples, real-life analogies, common mistakes, best practices, and complete programs you can run right away.

Let’s start!

1. C-style Fixed-Size Arrays (The Classic Way)

These are the original arrays from C β€” still used everywhere in C++.

Syntax:

C++

Key points:

  • Size must be known at compile time (or constexpr)
  • Size is fixed β€” you cannot change it later
  • Indices start at 0
  • No built-in safety β€” going out of bounds causes undefined behavior (very dangerous!)

Simple example:

C++

Dangerous mistake – out of bounds:

C++

Pro tip: Always keep track of the size yourself β€” common pattern:

C++

Modern alternative: Use std::array (next section) β€” it remembers its size!

2. std::array – The Modern, Safe C++ Array (C++11+)

std::array is a fixed-size array with all the safety and convenience of the STL.

Advantages over C-style:

  • Knows its own size (size() member function)
  • Has .at() which checks bounds (throws exception if out of range)
  • Can be passed to functions easily
  • Works with range-based for loops perfectly

Include: #include <array>

Example – Everything you need:

C++

Best practice (2025+): Whenever you need a fixed-size collection β†’ prefer std::array over C-style arrays!

3. std::string – The Easy & Powerful String Class

std::string is the go-to way to handle text in modern C++.

Include: #include <string>

Key features:

  • Can grow or shrink automatically
  • Very easy to use (+, +=, size(), empty(), find(), substr(), etc.)
  • Safe β€” no buffer overflows like in C

Basic examples:

C++

Very useful string methods:

Method What it does Example
size() / length() Number of characters name.size()
empty() Is string empty? if (s.empty()) …
clear() Empty the string s.clear();
find(“text”) Find position of substring (or npos) if (s.find(“C++”) != std::string::npos)
substr(pos, len) Extract substring s.substr(0, 5) β†’ first 5 chars
+=, + Concatenate s += ” world!”;

4. std::string_view – Lightweight Read-Only View (C++17+)

std::string_view is a non-owning view into a string (or char array).

Why it’s awesome:

  • Zero copy β€” very fast & memory efficient
  • Works with any contiguous sequence of characters
  • Cannot modify the data (read-only)
  • Perfect for function parameters when you just want to read a string

Include: #include <string_view>

Example – Best practice for function parameters:

C++

Modern recommendation (2025+):

  • When a function only reads a string β†’ use std::string_view
  • When it needs to own or modify the string β†’ use std::string

5. Full Practical Example – Student Names & Scores

C++

Your Mini Homework (Try These!)

  1. Create a C-style array of 6 integers, fill it with numbers 1 to 6, and print them in reverse order.
  2. Use std::array<double, 5> to store temperatures, calculate average using range-based for.
  3. Write a function that takes std::string_view and prints whether the string contains “C++”.
  4. Ask the user for their full name using std::getline, then print:
    • Length
    • First name (before space)
    • Last name (after space)

You’re doing incredibly well! You now know how to handle lists of numbers and text β€” the foundation of almost every real application!

Next lesson: Pointers & References β€” the gateway to dynamic memory and advanced C++!

Any questions? Confused about std::array vs C-style? Want more string tricks or string_view examples? Just ask β€” your friendly C++ teacher is always here for you! πŸš€

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *