cpp

exercises

exercises.cpp⚙️
/**
 * STL Iterators - Exercises
 * Compile: g++ -std=c++17 -Wall exercises.cpp -o exercises
 */

#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <iterator>
#include <algorithm>
#include <sstream>
using namespace std;

// ============================================================
// Exercise 1: Manual Iteration ⭐
// ============================================================

// TODO: Using iterators (not range-for), print vector elements
// in format: [1, 2, 3, 4, 5]

void exercise1() {
    // vector<int> v = {1, 2, 3, 4, 5};
    // printVector(v);  // [1, 2, 3, 4, 5]
    cout << "(Implement printVector with iterators)" << endl;
}

// ============================================================
// Exercise 2: Find and Replace ⭐⭐
// ============================================================

// TODO: Using iterators, find all occurrences of 'old'
// and replace with 'new' value. Return count of replacements.

void exercise2() {
    // vector<int> v = {1, 2, 3, 2, 4, 2};
    // int count = findAndReplace(v, 2, 99);
    // v is now {1, 99, 3, 99, 4, 99}, count = 3
    cout << "(Implement findAndReplace)" << endl;
}

// ============================================================
// Exercise 3: Reverse Copy ⭐⭐
// ============================================================

// TODO: Copy elements from source to dest in reverse order
// using reverse iterators

void exercise3() {
    // vector<int> src = {1, 2, 3, 4, 5};
    // vector<int> dest;
    // reverseCopy(src, dest);
    // dest is {5, 4, 3, 2, 1}
    cout << "(Implement reverseCopy)" << endl;
}

// ============================================================
// Exercise 4: Merge Sorted Lists ⭐⭐
// ============================================================

// TODO: Merge two sorted containers into one sorted container
// using iterators

void exercise4() {
    // list<int> a = {1, 3, 5};
    // list<int> b = {2, 4, 6};
    // list<int> result;
    // mergeSorted(a, b, result);
    // result is {1, 2, 3, 4, 5, 6}
    cout << "(Implement mergeSorted)" << endl;
}

// ============================================================
// Exercise 5: Stream to Container ⭐⭐
// ============================================================

// TODO: Read all integers from a string using stream iterators
// and return as vector

void exercise5() {
    // string input = "10 20 30 40 50";
    // auto v = parseInts(input);
    // v is {10, 20, 30, 40, 50}
    cout << "(Implement parseInts with istream_iterator)" << endl;
}

// ============================================================
// Exercise 6: Insert at Intervals ⭐⭐⭐
// ============================================================

// TODO: Insert a separator value every N elements using inserter

void exercise6() {
    // vector<int> v = {1, 2, 3, 4, 5, 6};
    // insertSeparator(v, 0, 2);
    // v is {1, 2, 0, 3, 4, 0, 5, 6}
    cout << "(Implement insertSeparator)" << endl;
}

// ============================================================
// Exercise 7: Custom Range Iterator ⭐⭐⭐
// ============================================================

// TODO: Create StepRange(start, end, step) class
// that iterates from start to end by step

void exercise7() {
    // for (int x : StepRange(0, 10, 2)) {
    //     cout << x << " ";  // 0 2 4 6 8
    // }
    cout << "(Implement StepRange iterator)" << endl;
}

// ============================================================
// Exercise 8: Bidirectional Traversal ⭐⭐⭐
// ============================================================

// TODO: Given a list, print elements alternating from
// front and back: first, last, second, second-last, ...

void exercise8() {
    // list<int> lst = {1, 2, 3, 4, 5};
    // zigzagPrint(lst);  // 1 5 2 4 3
    cout << "(Implement zigzag print with bidirectional iterators)" << endl;
}

// ============================================================
// MAIN
// ============================================================

int main() {
    cout << "=== STL Iterators Exercises ===" << endl;
    
    cout << "\nEx1: Print with Iterators" << endl;
    exercise1();
    
    cout << "\nEx2: Find and Replace" << endl;
    exercise2();
    
    cout << "\nEx3: Reverse Copy" << endl;
    exercise3();
    
    cout << "\nEx4: Merge Sorted" << endl;
    exercise4();
    
    cout << "\nEx5: Stream Parsing" << endl;
    exercise5();
    
    cout << "\nEx6: Insert Separator" << endl;
    exercise6();
    
    cout << "\nEx7: Step Range" << endl;
    exercise7();
    
    cout << "\nEx8: Zigzag Print" << endl;
    exercise8();
    
    return 0;
}

// ============================================================
// ANSWERS
// ============================================================
/*
Ex1:
void printVector(const vector<int>& v) {
    cout << "[";
    for (auto it = v.begin(); it != v.end(); ++it) {
        if (it != v.begin()) cout << ", ";
        cout << *it;
    }
    cout << "]" << endl;
}

Ex2:
int findAndReplace(vector<int>& v, int oldVal, int newVal) {
    int count = 0;
    for (auto it = v.begin(); it != v.end(); ++it) {
        if (*it == oldVal) {
            *it = newVal;
            ++count;
        }
    }
    return count;
}

Ex3:
void reverseCopy(const vector<int>& src, vector<int>& dest) {
    copy(src.rbegin(), src.rend(), back_inserter(dest));
}

Ex4:
void mergeSorted(const list<int>& a, const list<int>& b, list<int>& result) {
    merge(a.begin(), a.end(), b.begin(), b.end(), back_inserter(result));
}

Ex5:
vector<int> parseInts(const string& s) {
    istringstream iss(s);
    return vector<int>(istream_iterator<int>(iss), istream_iterator<int>());
}

Ex7:
class StepRange {
    int start, stop, step;
public:
    StepRange(int s, int e, int st) : start(s), stop(e), step(st) {}
    class Iterator {
        int cur, step;
    public:
        Iterator(int c, int s) : cur(c), step(s) {}
        int operator*() const { return cur; }
        Iterator& operator++() { cur += step; return *this; }
        bool operator!=(const Iterator& o) const { return cur < o.cur; }
    };
    Iterator begin() { return {start, step}; }
    Iterator end() { return {stop, step}; }
};

Ex8:
void zigzagPrint(list<int>& lst) {
    auto front = lst.begin();
    auto back = prev(lst.end());
    while (front != back && front != next(back)) {
        cout << *front++ << " ";
        if (front != back) cout << *back-- << " ";
    }
    if (front == back) cout << *front;
    cout << endl;
}
*/
Exercises - C++ Tutorial | DeepML