Advertisement
Guest User

Untitled

a guest
Sep 9th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /************ stack.h ************/
  2. #include<vector>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. class stack
  7. {
  8.     public:
  9.     bool find(const string &elem) const;
  10.     int count(const string &elem) const;
  11. ...
  12. ...
  13.     private:
  14.     vector<string> _stack;
  15.  
  16. };
  17.  
  18. /********** stack.cpp **********/
  19.  
  20. #include "stack.h"
  21. ...
  22. ...
  23. bool stack::find (const string &elem) const
  24. {
  25.     vector<string>::const_iterator it = _stack.begin();
  26.     return (::find(it,_stack.end(),elem))!=_stack.end();
  27. }
  28. int stack::count(const string &elem) const
  29. {
  30.  
  31.     return (::count(_stack.begin(),_stack.end(),elem));
  32. }
  33.  
  34. /*********** main.cpp *************/
  35. #include "stack.cpp"
  36. #include<fstream>
  37. int main()
  38. {
  39.     stack st;
  40.     string str;
  41.     ifstream readfile("sentence.txt");
  42.     while(readfile>>str && !st.full())
  43.     {
  44.         st.push(str);
  45.     }
  46. ...
  47.     cout<<"\nEnter a word to search: ";
  48.     cin>>str;
  49.     if(st.find(str))
  50.         cout<<"The word was found "<<st.count(str)<<" no. of times"<<endl;
  51.     else
  52.         cout<<"The word was not found"<<endl;
  53.     return 0;
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement