# 算法代写|数据结构代写|C++代写|C代写-Iterator usage

### HW 5: Iterator usage

Assigned Wednesday, April 4th Due 11p.m. Friday, April 6th Summary In this assignment, you will write an iterator-based function to determine whether or not a collection is sorted Learning Goals Use of iterators to track positions within a collection Collaboration This is an individual (and very short!) assignment.

#### Activity

In the provided code, you will find a simple C++ file using some standard library data structures. The example of a sum function, as we wrote in class, is already provided.

#include #include #include

using namespace std;

template int sum(const C& collection) { int result = 0; for (typename C::const_iterator it = collection.begin(); it != collection.end(); ++it ) { result = result + *it; } return result; }

template bool isSorted(const C& collection) { //FIXME: some collections are actually sorted! return false; }

int main() { list myList; myList.push_front(5); myList.push_front(3); myList.push_front(42); std::cout << "Sum1: " << sum(myList) << std::endl;

std::cout << "IsSorted: " << isSorted(myList) << std::endl;

vector v; v.push_back(3); v.push_back(5); v.push_back(42); std::cout << "Sum2: " << sum(v) << std::endl; std::cout << "IsSorted: " << isSorted(v) << std::endl;

//Writing other test cases is recommended!

}

Your task is to implement the "isSorted" function, such that it can be invoked on iterable collections just as the sum function. Your function should return a boolean indicating whether the collection is sorted in increasing order or not. Some simple tests are given, but you are encouraged to write more test cases. Remember that if you use the "auto" keyword (optional), you will have to add the "-std=c++11" argument to your compilation command.