This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 9th, 2012  |  syntax: C++  |  size: 1.09 KB  |  views: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }
clone this paste RAW Paste Data