i am trying to read words from a file i have "words.txt" into a set. the code i have written compiles and prints to screen fine but my question is what makes something a "set"? i mean i named my class "set" but beyond that how do i know it is a set?
code for main:
code for class:Code:#include <iostream> #include <fstream> #include <string> #include <algorithm> #include "set.h" using namespace std; int main() { set<string> object; string word, search; ifstream infile; infile.open("words.txt"); while (infile >> word) { object.readWords(word); } object.printWords(); cout << "Enter word to search for: "; cin >> search; object.search(search); return 0; }
Code:#ifndef SET_H #define SET_H #include <list> #include <algorithm> using namespace std; template <class T> class set { public: void readWords(T& word); void printWords(); void search(T& search); private: list<T> setList; }; template <class T> void set<T>::readWords(T &word) { setList.insert(setList.end(), word); } template <class T> void set<T>::printWords() { list<T>::iterator iter; for (iter = setList.begin(); iter != setList.end(); iter++) cout << *iter << endl; } template <class T> void set<T>::search(T &search) { if (binary_search(setList.begin(), setList.end(), search)) cout << search << " was found in the set. " << endl; else cout << "That word was not found. " << endl; } #endif



LinkBack URL
About LinkBacks


