(main.cpp)
#include "ListItem.h"
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main () {
// working with a vector of ints
int myints[] = { 5, 10, 15, 20 };
std::vector<int> vectorOfNums (myints,myints+4);
std::vector<int>::iterator vectItr;
// TODO: write call to find() to search for 15 in the vectorOfNums
if (vectItr != vectorOfNums.end())
std::cout << "Element found: " << *vectItr << 'n';
else
std::cout << "Element not foundn";
// TODO: write call to find() to search for 85 in the vectorOfNums
if (vectItr != vectorOfNums.end())
std::cout << "Element found: " << *vectItr << 'n';
else
std::cout << "Element not foundn";
// working with a list of strings
std::list<std::string> listOfWords;
std::list<std::string>::iterator listIter;
listOfWords.push_back("house");
listOfWords.push_back("cup");
listOfWords.push_back("car");
listOfWords.push_back("lamp");
// TODO: write call to find() to search for 'lamp' in the listOfWords and write
// the code to test for it
// TODO: write call to find() to search for 'music' in the listOfWords and write
// the code to test for it
// working with a list of listItems
std::list<ListItem> listOfItems;
std::list<ListItem>::iterator listItemIter;
listOfItems.push_back(ListItem("shoes", 150.95));
listOfItems.push_back(ListItem("laptop", 1245.85));
listOfItems.push_back(ListItem("dress", 85.68));
listOfItems.push_back(ListItem("polo shirt", 25.67));
// TODO: write call to find() to search for the 'dress' object in the listOfItems and write
// the code to test for it
// TODO: write call to find() to search for the 'slacks' object that cost '85.68' in the listOfItems and
write
// the code to test for it
return 0;
}
(ListItem.h)
#ifndef LISTITEMH
#define LISTITEMH
#include <string>
using namespace std;
class ListItem {
public:
ListItem();
ListItem(string itemInit, double priceInit);
// Print this node
void PrintNodeData();
// TODO: write the prototype for the '==' overload
// TODO: write the prototype for '<<' overload (hint: must be a friend)
private:
string item;
double price;
};
#endif
(ListIem.cpp)
#include "ListItem.h"
#include <iostream>
ListItem::ListItem() {
item = "";
price = 0.0;
}
ListItem::ListItem(string itemInit, double priceInit) {
item = itemInit;
price = priceInit;
}
// Print this node
void ListItem::PrintNodeData() {
cout << item << ": $" << price << endl;
}
// TODO: write the definition for the '==' overload
// TODO: write the prototype for '<<' overload
Given a Listitem class, complete main() using algorithms from the STL (STL Algorithms). An
example algorithm the find(): find (Inputiterator first, Inputiterator last, const T& val) The find uses
two iterators for the bounds of the search, first and last. The 3rd parameter is the value being
searched for. if we look inside the find() we might see code like this (ref cplusplus.com): The
description of find () states "The function uses operator== to compare the individual elements to
val". This means that any object we want to search for needs to be have the capability of
performing ==. And this may mean implementing the overload if the data type of the object does
not already have this overload. Note that C++ types like int, float, double, and string already
support this. Your task is to follow the prompts in main() and write the code required. For some of
the tests, you will need to provide support for overloaded operators in the Listltem class.
419632.1188960.q3zqy7

maincpp include ListItemh include ltstringgt in.pdf

  • 1.
    (main.cpp) #include "ListItem.h" #include <string> #include<list> #include <vector> #include <algorithm> #include <iostream> using namespace std; int main () { // working with a vector of ints int myints[] = { 5, 10, 15, 20 }; std::vector<int> vectorOfNums (myints,myints+4); std::vector<int>::iterator vectItr; // TODO: write call to find() to search for 15 in the vectorOfNums if (vectItr != vectorOfNums.end()) std::cout << "Element found: " << *vectItr << 'n'; else std::cout << "Element not foundn"; // TODO: write call to find() to search for 85 in the vectorOfNums if (vectItr != vectorOfNums.end()) std::cout << "Element found: " << *vectItr << 'n'; else std::cout << "Element not foundn"; // working with a list of strings std::list<std::string> listOfWords; std::list<std::string>::iterator listIter; listOfWords.push_back("house"); listOfWords.push_back("cup"); listOfWords.push_back("car"); listOfWords.push_back("lamp"); // TODO: write call to find() to search for 'lamp' in the listOfWords and write // the code to test for it // TODO: write call to find() to search for 'music' in the listOfWords and write // the code to test for it // working with a list of listItems std::list<ListItem> listOfItems; std::list<ListItem>::iterator listItemIter; listOfItems.push_back(ListItem("shoes", 150.95)); listOfItems.push_back(ListItem("laptop", 1245.85)); listOfItems.push_back(ListItem("dress", 85.68));
  • 2.
    listOfItems.push_back(ListItem("polo shirt", 25.67)); //TODO: write call to find() to search for the 'dress' object in the listOfItems and write // the code to test for it // TODO: write call to find() to search for the 'slacks' object that cost '85.68' in the listOfItems and write // the code to test for it return 0; } (ListItem.h) #ifndef LISTITEMH #define LISTITEMH #include <string> using namespace std; class ListItem { public: ListItem(); ListItem(string itemInit, double priceInit); // Print this node void PrintNodeData(); // TODO: write the prototype for the '==' overload // TODO: write the prototype for '<<' overload (hint: must be a friend) private: string item; double price; }; #endif (ListIem.cpp) #include "ListItem.h" #include <iostream> ListItem::ListItem() { item = ""; price = 0.0; } ListItem::ListItem(string itemInit, double priceInit) { item = itemInit; price = priceInit; } // Print this node
  • 3.
    void ListItem::PrintNodeData() { cout<< item << ": $" << price << endl; } // TODO: write the definition for the '==' overload // TODO: write the prototype for '<<' overload Given a Listitem class, complete main() using algorithms from the STL (STL Algorithms). An example algorithm the find(): find (Inputiterator first, Inputiterator last, const T& val) The find uses two iterators for the bounds of the search, first and last. The 3rd parameter is the value being searched for. if we look inside the find() we might see code like this (ref cplusplus.com): The description of find () states "The function uses operator== to compare the individual elements to val". This means that any object we want to search for needs to be have the capability of performing ==. And this may mean implementing the overload if the data type of the object does not already have this overload. Note that C++ types like int, float, double, and string already support this. Your task is to follow the prompts in main() and write the code required. For some of the tests, you will need to provide support for overloaded operators in the Listltem class. 419632.1188960.q3zqy7