cpp

examples

examples.cpp⚙️
/**
 * STL Algorithms - Examples
 * Compile: g++ -std=c++17 -Wall examples.cpp -o examples
 */

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <string>
#include <random>
using namespace std;

// ============================================================
// SECTION 1: NON-MODIFYING ALGORITHMS
// ============================================================

void demoNonModifying() {
    cout << "--- Non-Modifying Algorithms ---\n" << endl;
    
    vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    // for_each
    cout << "  Elements: ";
    for_each(v.begin(), v.end(), [](int x) { cout << x << " "; });
    cout << endl;
    
    // find
    auto it = find(v.begin(), v.end(), 5);
    cout << "  find(5): " << (it != v.end() ? "found" : "not found") << endl;
    
    // find_if
    auto it2 = find_if(v.begin(), v.end(), [](int x) { return x > 7; });
    cout << "  find_if(>7): " << *it2 << endl;
    
    // count / count_if
    cout << "  count(3): " << count(v.begin(), v.end(), 3) << endl;
    cout << "  count_if(even): " << count_if(v.begin(), v.end(), 
                                              [](int x) { return x % 2 == 0; }) << endl;
    
    // all_of, any_of, none_of
    cout << "  all_of(>0): " << boolalpha 
         << all_of(v.begin(), v.end(), [](int x) { return x > 0; }) << endl;
    cout << "  any_of(>8): " 
         << any_of(v.begin(), v.end(), [](int x) { return x > 8; }) << endl;
    
    // min/max
    cout << "  min: " << *min_element(v.begin(), v.end()) << endl;
    cout << "  max: " << *max_element(v.begin(), v.end()) << endl;
}

// ============================================================
// SECTION 2: MODIFYING ALGORITHMS
// ============================================================

void demoModifying() {
    cout << "\n--- Modifying Algorithms ---\n" << endl;
    
    // copy
    vector<int> src = {1, 2, 3, 4, 5};
    vector<int> dest;
    copy(src.begin(), src.end(), back_inserter(dest));
    cout << "  copy: ";
    for (int x : dest) cout << x << " ";
    cout << endl;
    
    // copy_if
    vector<int> evens;
    copy_if(src.begin(), src.end(), back_inserter(evens),
            [](int x) { return x % 2 == 0; });
    cout << "  copy_if(even): ";
    for (int x : evens) cout << x << " ";
    cout << endl;
    
    // transform
    vector<int> doubled;
    transform(src.begin(), src.end(), back_inserter(doubled),
              [](int x) { return x * 2; });
    cout << "  transform(*2): ";
    for (int x : doubled) cout << x << " ";
    cout << endl;
    
    // fill
    vector<int> filled(5);
    fill(filled.begin(), filled.end(), 42);
    cout << "  fill(42): ";
    for (int x : filled) cout << x << " ";
    cout << endl;
    
    // replace
    vector<int> v = {1, 2, 3, 2, 4, 2};
    replace(v.begin(), v.end(), 2, 99);
    cout << "  replace(2->99): ";
    for (int x : v) cout << x << " ";
    cout << endl;
    
    // reverse
    vector<int> rev = {1, 2, 3, 4, 5};
    reverse(rev.begin(), rev.end());
    cout << "  reverse: ";
    for (int x : rev) cout << x << " ";
    cout << endl;
}

// ============================================================
// SECTION 3: REMOVE-ERASE IDIOM
// ============================================================

void demoRemove() {
    cout << "\n--- Remove-Erase Idiom ---\n" << endl;
    
    vector<int> v = {1, 2, 3, 2, 4, 2, 5};
    cout << "  Before: ";
    for (int x : v) cout << x << " ";
    cout << endl;
    
    // Erase-remove idiom
    v.erase(remove(v.begin(), v.end(), 2), v.end());
    cout << "  After remove(2): ";
    for (int x : v) cout << x << " ";
    cout << endl;
    
    // Remove_if
    vector<int> v2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    v2.erase(remove_if(v2.begin(), v2.end(), 
                       [](int x) { return x % 2 == 0; }), v2.end());
    cout << "  After remove_if(even): ";
    for (int x : v2) cout << x << " ";
    cout << endl;
}

// ============================================================
// SECTION 4: SORTING
// ============================================================

void demoSorting() {
    cout << "\n--- Sorting Algorithms ---\n" << endl;
    
    vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6};
    
    // sort ascending
    vector<int> asc = v;
    sort(asc.begin(), asc.end());
    cout << "  Ascending: ";
    for (int x : asc) cout << x << " ";
    cout << endl;
    
    // sort descending
    vector<int> desc = v;
    sort(desc.begin(), desc.end(), greater<int>());
    cout << "  Descending: ";
    for (int x : desc) cout << x << " ";
    cout << endl;
    
    // partial_sort
    vector<int> partial = v;
    partial_sort(partial.begin(), partial.begin() + 3, partial.end());
    cout << "  Partial(top 3): ";
    for (int x : partial) cout << x << " ";
    cout << endl;
    
    // is_sorted
    cout << "  is_sorted(asc): " << boolalpha 
         << is_sorted(asc.begin(), asc.end()) << endl;
}

// ============================================================
// SECTION 5: SEARCHING
// ============================================================

void demoSearching() {
    cout << "\n--- Searching Algorithms ---\n" << endl;
    
    vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    // binary_search
    bool found = binary_search(v.begin(), v.end(), 5);
    cout << "  binary_search(5): " << boolalpha << found << endl;
    
    // lower_bound
    auto lb = lower_bound(v.begin(), v.end(), 5);
    cout << "  lower_bound(5): index " << distance(v.begin(), lb) << endl;
    
    // upper_bound
    auto ub = upper_bound(v.begin(), v.end(), 5);
    cout << "  upper_bound(5): index " << distance(v.begin(), ub) << endl;
    
    // equal_range
    vector<int> v2 = {1, 2, 2, 2, 3, 4};
    auto range = equal_range(v2.begin(), v2.end(), 2);
    cout << "  equal_range(2): [" << distance(v2.begin(), range.first)
         << ", " << distance(v2.begin(), range.second) << ")" << endl;
}

// ============================================================
// SECTION 6: NUMERIC ALGORITHMS
// ============================================================

void demoNumeric() {
    cout << "\n--- Numeric Algorithms ---\n" << endl;
    
    vector<int> v = {1, 2, 3, 4, 5};
    
    // accumulate (sum)
    int sum = accumulate(v.begin(), v.end(), 0);
    cout << "  sum: " << sum << endl;
    
    // accumulate (product)
    int product = accumulate(v.begin(), v.end(), 1, multiplies<int>());
    cout << "  product: " << product << endl;
    
    // partial_sum
    vector<int> psum(5);
    partial_sum(v.begin(), v.end(), psum.begin());
    cout << "  partial_sum: ";
    for (int x : psum) cout << x << " ";
    cout << endl;
    
    // iota
    vector<int> seq(5);
    iota(seq.begin(), seq.end(), 10);
    cout << "  iota(10): ";
    for (int x : seq) cout << x << " ";
    cout << endl;
    
    // inner_product
    vector<int> a = {1, 2, 3};
    vector<int> b = {4, 5, 6};
    int dot = inner_product(a.begin(), a.end(), b.begin(), 0);
    cout << "  inner_product: " << dot << endl;
}

// ============================================================
// SECTION 7: SET ALGORITHMS
// ============================================================

void demoSetAlgorithms() {
    cout << "\n--- Set Algorithms ---\n" << endl;
    
    vector<int> a = {1, 2, 3, 4, 5};
    vector<int> b = {3, 4, 5, 6, 7};
    vector<int> result;
    
    // union
    set_union(a.begin(), a.end(), b.begin(), b.end(), 
              back_inserter(result));
    cout << "  union: ";
    for (int x : result) cout << x << " ";
    cout << endl;
    
    // intersection
    result.clear();
    set_intersection(a.begin(), a.end(), b.begin(), b.end(),
                     back_inserter(result));
    cout << "  intersection: ";
    for (int x : result) cout << x << " ";
    cout << endl;
    
    // difference
    result.clear();
    set_difference(a.begin(), a.end(), b.begin(), b.end(),
                   back_inserter(result));
    cout << "  difference (a-b): ";
    for (int x : result) cout << x << " ";
    cout << endl;
    
    // symmetric_difference
    result.clear();
    set_symmetric_difference(a.begin(), a.end(), b.begin(), b.end(),
                             back_inserter(result));
    cout << "  symmetric_diff: ";
    for (int x : result) cout << x << " ";
    cout << endl;
}

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

int main() {
    cout << "╔══════════════════════════════════════╗" << endl;
    cout << "║     STL ALGORITHMS - EXAMPLES        ║" << endl;
    cout << "╚══════════════════════════════════════╝" << endl;
    
    demoNonModifying();
    demoModifying();
    demoRemove();
    demoSorting();
    demoSearching();
    demoNumeric();
    demoSetAlgorithms();
    
    cout << "\n=== Complete ===" << endl;
    return 0;
}
Examples - C++ Tutorial | DeepML