Building an Iterator Protocol in a Custom Class

Hard Design Patterns & Final Challenges PRO
🔒 Login to Unlock

Problem Statement

Create a `Collection` class encapsulating an integer array. Implement `int* begin()` and `int* end()` methods so that C++ range-based for loops can iterate over the object directly. Add a method `void add(int v)`. Read N, insert N integers, and then use `for (int val : collection)` to print them separated by spaces.

Input Format

Integer N, followed by N integers.

Output Format

Print the elements separated by space.

Constraints

1 <= N <= 100

Sample Input

3
10 20 30

Sample Output

10 20 30

Explanation

C++ range-based for loops implicitly look for `begin()` and `end()` methods on objects. Providing them seamlessly integrates custom classes with STL loop structures.

Starter Code

#include <iostream>

class Collection {
    int arr[100];
    int sz = 0;
public:
    // Implement add(), begin(), and end()
};

int main() {
    // Setup collection and use range-based for loop
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used