Iterating through Object Vectors

Medium Templates & Generic Programming PRO
🔒 Login to Unlock

Problem Statement

Using the same `Item` class (id, name), populate a `std::vector` with N items from input. Use a C++11 range-based for loop (`for (const auto& obj : vec)`) to iterate through the vector. For each item, print ‘ID:[id] Name:[name]’ on a new line.

Input Format

Line 1: N. Next N lines or spaces: pairs of integer ID and string name.

Output Format

Print each object's details on a new line.

Constraints

1 <= N <= 20

Sample Input

2
101 Desk
102 Chair

Sample Output

ID:101 Name:Desk
ID:102 Name:Chair

Explanation

Iterating over template containers holding custom objects leverages robust modern C++ auto-typing.

Starter Code

#include <iostream>
#include <vector>
#include <string>

class Item {
public:
    // Define id, name, and constructor
};

int main() {
    // Setup vector, push N items, iterate and print
    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