Implement the function maxLoc(), which returns an iterator at the largest element in a list. template typename list::iterator maxLoc(list& aList); Write a program that tests maxLoc(), using the following declarations: int intArr[] = {23, 49, -3, 29, 17, 200, 38, 93, 40}; int intSize = sizeof(intArr)/sizeof(int); list intList(intArr,intArr+intSize); char chrArr[] = "Hello World!"; int chrSize = sizeof(chrArr); list chrList(chrArr,chrArr+chrSize); The program should repeatedly call maxLoc(), output the largest value, and then delete the value, until the list is empty.

Respuesta :

Answer:

See explaination

Explanation:

#include <iostream>

#include <iterator>

#include <list>

using namespace std;

list <int> :: iterator maxLoc(list<int>& alist){

list<int> :: iterator max;

list<int> :: iterator it;

int maxs = -199999999;

for (it = alist.begin(); it != alist.end();it++){

int c = *it;

if(maxs < c){

max = it;

maxs = *it;

}

}

alist.erase(max);

return max;

}

list <char> :: iterator maxLoc(list<char>& alist){

list<char> :: iterator max;

list<char> :: iterator it;

int maxs = -199999999;

for (it = alist.begin(); it != alist.end();it++){

char c = *it;

if(maxs < c){

max = it;

maxs = *it;

}

}

alist.erase(max);

return max;

}

int main() {

int intArr[] = {23, 49, -3, 29, 17, 200, 38, 93, 40};

int intSize = sizeof(intArr) / sizeof(int);

list <int> intlist(intArr, intArr + intSize);

list<int> :: iterator it;

for(int i = 0;i<intSize;i++){

list<int> :: iterator m = maxLoc(intlist);

cout << *m << endl;

}

char chrArr[] = "Hello World!";

int chrSize = sizeof(chrArr);

list<char> chrlist(chrArr, chrArr + chrSize);

for(int j = 0;j<chrSize;j++){

list<char> :: iterator m = maxLoc(chrlist);

cout << *m << endl;

}

}