// Examples of available algorithms: // // find(start, end, value) first occurrence in [start, end) or end if not // found // count(start, end, value) number of occurrences of value in [start, end) // equal(start1, end1, whether values in [start1, end1) are all equal // start2, end2) to the values in [start2, end2) // replace(start, stop, replaces all occurrences of old with new in // old, new) [start, end) // reverse(start, stop) reverses the order of the elements [start, end) // random_shuffle(start, stop) randomly shuffles values in [start, end) // sort(start, end) sorts elements from [start, end) // is_sorted(start, stop) returns whether values in range are sorted // merge(start1, stop1, merges sorted [start1, stop1) with sorted // start2, stop2, start3) [start2, stop2), storing in start3 // accumulate(start, stop, s) sum of values in [start, stop), init sum s #include #include #include #include #include #include #include using namespace std; ostream & operator<<(ostream & out, const vector & v) { out << "["; if (v.size() > 0) { auto itr = v.begin(); out << *itr; itr++; while (itr != v.end()) { out << ", " << *itr; itr++; } } out << "]"; return out; } int main() { vector v1 = {28, 7, 3, 3, 19, 2, 5, 7, 12, 53, 42, 19, 3, 8, 3}; vector v2 = {843, -7, 14, 3, 2, 5, 7, 12, 53, 92}; set s = {7, 2, 53, 12, 5}; cout << "v1 = " << v1 << endl; cout << "v2 = " << v2 << endl; cout << endl; auto itr = find(v1.begin(), v1.end(), 3); if (itr == v1.end()) { cout << "value not found" << endl; } else { *itr = 42; cout << "v1 = " << v1 << endl; } auto itr2 = find(s.begin(), s.end(), 7); cout << (itr2 == s.end()) << endl; // the compiler did not allow us to attempt to change a value in the set // *itr2 = 42; cout << endl; cout << "count returns " << count(v1.begin(), v1.end(), 3) << endl; cout << endl; cout << "equal returns " << equal(v1.begin() + 5, v1.begin() + 10, v2.begin() + 4, v2.begin() + 9) << endl; cout << "equal returns " << equal(v1.begin() + 5, v1.begin() + 10, s.begin(), s.end()) << endl; cout << endl; replace(v1.begin(), v1.end(), 3, -8); cout << "v1 = " << v1 << endl; // we weren't able to call replace on a set // replace(s.begin(), s.end(), 2, 15); cout << endl; reverse(v1.begin(), v1.end()); cout << "v1 = " << v1 << endl; // we weren't able to call reverse on a set // reverse(s.begin(), s.end()); cout << endl; sort(v1.begin(), v1.end()); cout << "v1 = " << v1 << endl; cout << "is sorted returns " << is_sorted(v1.begin(), v1.end()) << endl; cout << endl; random_shuffle(v1.begin(), v1.end()); cout << "v1 = " << v1 << endl; cout << "is sorted returns " << is_sorted(v1.begin(), v1.end()) << endl; cout << endl; vector v3 (v1.size() + v2.size() + 3); sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); cout << "v3 = " << v3 << endl; cout << endl; cout << "accumulate v1 = " << accumulate(v1.begin(), v1.end(), 0) << endl; cout << endl; string phrase; cout << "phrase to use? "; cin >> phrase; // change the letters to lowercase in the user's phrase for (char & ch : phrase) { ch = tolower(ch); } sort(phrase.begin(), phrase.end()); ifstream input("dictionary.txt"); string word; while (input >> word) { string copy = word; sort(copy.begin(), copy.end()); if (phrase == copy) { cout << word << endl; } } return 0; }