SHOW:
|
|
- or go back to the newest paste.
1 | /************ stack.h ************/ | |
2 | #include<vector> | |
3 | #include<iostream> | |
4 | #include<algorithm> | |
5 | using namespace std; | |
6 | class stack | |
7 | { | |
8 | public: | |
9 | - | bool push(const string&); |
9 | + | |
10 | - | bool pop(string &elem); |
10 | + | |
11 | - | bool peek(string &elem); |
11 | + | ... |
12 | - | bool empty() const {return _stack.empty();} |
12 | + | ... |
13 | - | bool full() const {return _stack.size()==_stack.max_size();} |
13 | + | |
14 | - | int size() const {return _stack.size();} |
14 | + | |
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 | - | bool stack::push(const string& str) |
26 | + | |
27 | } | |
28 | - | if(full()) return false; |
28 | + | |
29 | - | _stack.push_back(str); |
29 | + | |
30 | - | return true; |
30 | + | |
31 | return (::count(_stack.begin(),_stack.end(),elem)); | |
32 | - | bool stack::pop(string& elem) |
32 | + | |
33 | ||
34 | - | if(empty()) return false; |
34 | + | |
35 | - | elem = _stack.back(); |
35 | + | |
36 | - | _stack.pop_back(); |
36 | + | |
37 | - | return true; |
37 | + | |
38 | { | |
39 | - | bool stack::peek(string& elem) |
39 | + | |
40 | string str; | |
41 | - | if(empty()) return false; |
41 | + | |
42 | - | elem = _stack.back(); |
42 | + | |
43 | - | return true; |
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 | } |