/*
* Copyright ©2026 Soham Pardeshi. All rights reserved.
* Permission is hereby granted to students registered for University of
* Washington CSE 333 for use solely during Summer Quarter 2026 for
* purposes of the course. No other use, copying, distribution, or
* modification is permitted without prior written consent. Copyrights
* for third-party components of this work must be honored. Instructors
* interested in reusing these course materials should contact the author.
*/
#include <algorithm>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
// Algorithms operate on iterator RANGES [begin, end), not on containers,
// so the same algorithm works on any container. See lecture 12 slide 58.
static void Print(int x) { cout << x << " "; }
int main(int argc, char** argv) {
std::vector<int> v = {5, 3, 9, 1, 7};
// sort uses the element's operator<.
std::sort(v.begin(), v.end());
cout << "sorted: ";
std::for_each(v.begin(), v.end(), Print); // calls Print(x) on each
cout << endl;
// find returns an iterator, or end() if the value is absent.
auto it = std::find(v.begin(), v.end(), 9);
if (it != v.end()) {
cout << "found 9 at index " << (it - v.begin()) << endl;
}
auto missing = std::find(v.begin(), v.end(), 42);
cout << "find(42) == end()? " << std::boolalpha << (missing == v.end())
<< endl;
return EXIT_SUCCESS;
}