cpp
examples
examples.cpp⚙️cpp
/**
* STL Iterators - Examples
* Compile: g++ -std=c++17 -Wall examples.cpp -o examples
*/
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <map>
#include <iterator>
#include <sstream>
#include <algorithm>
using namespace std;
// ============================================================
// SECTION 1: BASIC ITERATION
// ============================================================
void demoBasic() {
cout << "--- Basic Iteration ---\n" << endl;
vector<int> v = {1, 2, 3, 4, 5};
// Traditional iterator loop
cout << " Forward: ";
for (auto it = v.begin(); it != v.end(); ++it) {
cout << *it << " ";
}
cout << endl;
// Const iterator
cout << " Const: ";
for (auto it = v.cbegin(); it != v.cend(); ++it) {
cout << *it << " ";
}
cout << endl;
// Reverse iterator
cout << " Reverse: ";
for (auto it = v.rbegin(); it != v.rend(); ++it) {
cout << *it << " ";
}
cout << endl;
}
// ============================================================
// SECTION 2: ITERATOR OPERATIONS
// ============================================================
void demoOperations() {
cout << "\n--- Iterator Operations ---\n" << endl;
vector<int> v = {10, 20, 30, 40, 50};
// advance
auto it = v.begin();
advance(it, 2);
cout << " advance(begin, 2): " << *it << endl;
// distance
cout << " distance(begin, end): " << distance(v.begin(), v.end()) << endl;
// next/prev
auto it2 = next(v.begin(), 3);
cout << " next(begin, 3): " << *it2 << endl;
auto it3 = prev(v.end(), 2);
cout << " prev(end, 2): " << *it3 << endl;
// Random access operations
auto it4 = v.begin();
it4 += 4;
cout << " begin + 4: " << *it4 << endl;
cout << " it[2]: " << v.begin()[2] << endl;
}
// ============================================================
// SECTION 3: DIFFERENT CONTAINERS
// ============================================================
void demoDifferentContainers() {
cout << "\n--- Different Containers ---\n" << endl;
// Vector (random access)
vector<int> vec = {1, 2, 3};
cout << " vector: ";
for (auto it = vec.begin(); it != vec.end(); ++it) {
cout << *it << " ";
}
cout << endl;
// List (bidirectional)
list<int> lst = {4, 5, 6};
cout << " list: ";
for (auto it = lst.begin(); it != lst.end(); ++it) {
cout << *it << " ";
}
cout << endl;
// Set (bidirectional, const elements)
set<int> s = {9, 7, 8};
cout << " set: ";
for (auto it = s.begin(); it != s.end(); ++it) {
cout << *it << " ";
}
cout << endl;
// Map
map<string, int> m = {{"a", 1}, {"b", 2}};
cout << " map: ";
for (auto it = m.begin(); it != m.end(); ++it) {
cout << it->first << "=" << it->second << " ";
}
cout << endl;
}
// ============================================================
// SECTION 4: INSERT ITERATORS
// ============================================================
void demoInsertIterators() {
cout << "\n--- Insert Iterators ---\n" << endl;
// back_inserter
vector<int> v1;
auto bi = back_inserter(v1);
*bi++ = 1;
*bi++ = 2;
*bi++ = 3;
cout << " back_inserter: ";
for (int x : v1) cout << x << " ";
cout << endl;
// front_inserter
deque<int> dq;
auto fi = front_inserter(dq);
*fi++ = 1;
*fi++ = 2;
*fi++ = 3;
cout << " front_inserter: ";
for (int x : dq) cout << x << " ";
cout << endl;
// inserter
vector<int> v2 = {1, 5};
auto ii = inserter(v2, next(v2.begin()));
*ii++ = 2;
*ii++ = 3;
*ii++ = 4;
cout << " inserter: ";
for (int x : v2) cout << x << " ";
cout << endl;
}
// ============================================================
// SECTION 5: STREAM ITERATORS
// ============================================================
void demoStreamIterators() {
cout << "\n--- Stream Iterators ---\n" << endl;
// Input stream iterator
istringstream iss("10 20 30 40 50");
istream_iterator<int> iit(iss), end;
vector<int> v(iit, end);
cout << " From stream: ";
for (int x : v) cout << x << " ";
cout << endl;
// Output stream iterator
cout << " To stream: ";
ostream_iterator<int> oit(cout, "-");
for (int x : v) {
*oit++ = x;
}
cout << endl;
// Copy with stream iterator
cout << " Copy: ";
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
// ============================================================
// SECTION 6: ALGORITHMS WITH ITERATORS
// ============================================================
void demoAlgorithms() {
cout << "\n--- Algorithms with Iterators ---\n" << endl;
vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6};
// Find
auto it = find(v.begin(), v.end(), 5);
if (it != v.end()) {
cout << " Found 5 at index " << distance(v.begin(), it) << endl;
}
// Sort
sort(v.begin(), v.end());
cout << " Sorted: ";
for (int x : v) cout << x << " ";
cout << endl;
// Unique
auto last = unique(v.begin(), v.end());
cout << " Unique: ";
for (auto it = v.begin(); it != last; ++it) {
cout << *it << " ";
}
cout << endl;
// Transform
vector<int> doubled;
transform(v.begin(), last, back_inserter(doubled),
[](int x) { return x * 2; });
cout << " Doubled: ";
for (int x : doubled) cout << x << " ";
cout << endl;
}
// ============================================================
// SECTION 7: RANGE-BASED FOR
// ============================================================
void demoRangeBased() {
cout << "\n--- Range-Based For ---\n" << endl;
vector<int> v = {1, 2, 3, 4, 5};
// Read
cout << " Read: ";
for (const auto& x : v) {
cout << x << " ";
}
cout << endl;
// Modify
for (auto& x : v) {
x *= 2;
}
cout << " Modified: ";
for (const auto& x : v) {
cout << x << " ";
}
cout << endl;
// With index (C++20 style workaround)
cout << " With index: ";
for (size_t i = 0; const auto& x : v) {
cout << "[" << i++ << "]=" << x << " ";
}
cout << endl;
}
// ============================================================
// SECTION 8: CUSTOM ITERATOR
// ============================================================
class CountingRange {
int start, stop;
public:
CountingRange(int s, int e) : start(s), stop(e) {}
class Iterator {
int current;
public:
Iterator(int val) : current(val) {}
int operator*() const { return current; }
Iterator& operator++() { ++current; return *this; }
bool operator!=(const Iterator& other) const {
return current != other.current;
}
};
Iterator begin() const { return Iterator(start); }
Iterator end() const { return Iterator(stop); }
};
void demoCustomIterator() {
cout << "\n--- Custom Iterator ---\n" << endl;
cout << " CountingRange(1, 6): ";
for (int x : CountingRange(1, 6)) {
cout << x << " ";
}
cout << endl;
cout << " CountingRange(10, 15): ";
for (int x : CountingRange(10, 15)) {
cout << x << " ";
}
cout << endl;
}
// ============================================================
// MAIN
// ============================================================
int main() {
cout << "╔══════════════════════════════════════╗" << endl;
cout << "║ STL ITERATORS - EXAMPLES ║" << endl;
cout << "╚══════════════════════════════════════╝" << endl;
demoBasic();
demoOperations();
demoDifferentContainers();
demoInsertIterators();
demoStreamIterators();
demoAlgorithms();
demoRangeBased();
demoCustomIterator();
cout << "\n=== Complete ===" << endl;
return 0;
}