List Container – see textbook page 605 for basic operations: 3a. Write a templat
ID: 3802465 • Letter: L
Question
List Container – see textbook page 605 for basic operations:
3a. Write a template function to read values from an input stream to a list (in header file)
3b. Write a template function to fill in a list with a given vector (in header file)
3c. Write a template function to change a given value in a list to another given value(use find(), insert() and erase()) (in header file)
Then write a driver to test the above three template functions.
Here is what I have so far (I am told that my 'itr' is undeclared identifier in my Header.H. I'm not 100% sure how to fix it. I think I need to add "typedef typename list< class T>::iterator itr;" I just don't know where.)
Header.h
#include <iostream>
#include <list>
#include <vector>
using namespace std;
template<class T>
list<T> read()
//Purpose: reads values to a list
//Precondition: list
//Postcondition: reads value from input to a list
{
list<T> list;
T value;
while (cin >> value)
list.push_back(value);
return list;
}
template<class T>
list<T> fill(vector <T> vector)
//Purpose: fills list with a given vector
//Precondition: list and vector
//Postcondition: vector is put into list
{
list<T> list;
for (int i=0; i < vector.size(); i++)
list.push_back(vector[i]);
return list;
}
template<class T>
void replace(list<T>&list, T a, T b)
//Purpose: replaces value in list with another
//Precondition: filled list and a new value
//Postcondition: new value replaces a value in the list
{
list<int>::iterator itr = find(list.begin(), list.end(), a);
list.insert(itr, b);
list.erase(itr);
}
template<class T>
void print(list<T> list)
//Purpose: prints the new list
//Precondition: list
//Postcondition: prints the new list out with new value
{
for (itr = list.begin(); itr != list.end(); itr++)
cout << *itr << " ";
cout << endl;
}
Main.cpp
#include "Header.h"
using namespace std;
int main()
{
list<int> list = read<int>();
print(list);
cout << "Replacing numbers." << endl;
replace(list, 1, 10);
print(list);
return 0;
}
Explanation / Answer
/*Dear, There are multiple changes in you code that are needed all of those are mentioned below*/
#include <iostream>
#include <list>
#include <vector>
/*Change1:::::when you are using iterator, you need to call iterator header that contains all the properties of a iterator in STL*/
#include <iterator>
/*Change2:::::in order to use the find function that you have used in replace function, you need to include algorithm as all algorithm related functions like find, gcd , binary search etc are there in algorithm*/
#include <algorithm>
using namespace std;
template<class T>
list<T> read()
//Purpose: reads values to a list
//Precondition: list
//Postcondition: reads value from input to a list
{
list<T> list;
T value;
while (cin >> value)
list.push_back(value);
return list;
}
template<class T>
list<T> fill(vector <T> vector)
//Purpose: fills list with a given vector
//Precondition: list and vector
//Postcondition: vector is put into list
{
list<T> list;
for (int i=0; i < vector.size(); i++)
list.push_back(vector[i]);
return list;
}
template<class T>
void replace(list<T>&list, T a, T b)
//Purpose: replaces value in list with another
//Precondition: filled list and a new value
//Postcondition: new value replaces a value in the list
{
typename list<int>::iterator itr = find(list.begin(), list.end(), a);
list.insert(itr, b);
list.erase(itr);
}
template<class T>
void print(list<T> list)
//Purpose: prints the new list
//Precondition: list
//Postcondition: prints the new list out with new value
{
/*Change3::::: in the function , you forget to declare the iterator itr, you need to add the line just below this comment. */
typename list<int>::iterator itr;
for (itr = list.begin(); itr != list.end(); itr++)
cout << *itr << " ";
cout << endl;
}
/*remember , you didnt added any condition in the enter function of list, hence the input will be taken again and again till it reaches a null value.... once you read the inputs, it should have 1 in the values as it is nowhere checked that whether 1 is there in the input or not. In case it is not necessary that 1 will be there, you can write the condition as
if(itr!=list.end())
then replace;
This will save from the failure in case the value to be replaced is not present in the array
You can copy the code after removing my comments, its same except the mentioned changes
*/
//list.cpp is same as previous
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.