// 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 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 = {23, 18, 3, 9, 7, 34, 8, 3, 12, 15, -8, 203, 3}; vector v2 = {8, 19, 24, 7, 34, 8, 3, 12, 308, 14, 2, 6, 7, 9}; cout << "equal test returns " << equal(v1.begin() + 4, v1.begin() + 9, v2.begin() + 3, v2.begin() + 8) << endl; auto itr = find(v1.begin(), v1.end(), 8); cout << "v1 = " << v1 << endl; *itr = 3; cout << "v1 = " << v1 << endl; cout << "count of 3s is " << count(v1.begin(), v1.end(), 3) << endl; replace(v1.begin(), v1.end(), 3, -42); cout << "v1 = " << v1 << endl; cout << endl; reverse(v1.begin(), v1.end()); cout << "v1 = " << v1 << endl; sort(v1.begin(), v1.end()); cout << "v1 = " << v1 << endl; cout << "v1 is sorted returns " << is_sorted(v1.begin(), v1.end()) << endl; random_shuffle(v1.begin(), v1.end()); cout << "v1 = " << v1 << endl; cout << "v1 is sorted returns " << is_sorted(v1.begin(), v1.end()) << endl; cout << endl; vector v3(v1.size() + v2.size()); sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); cout << "v1 = " << v1 << endl; cout << "v2 = " << v2 << endl; 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; 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; }