1 /*《C++ Primer》3rd Edition, Section 4.15
2 * Question:
3 * 1)What the type of the constructor of class iStack?
4 * 2)In function iStack::full(),if we replace int(_stack.size() -1) with _stack.size() -1, then there yields a "warning C4018: “<”: 有符号/无符号不匹配" under Visual Studio 2008, why?
5 *
6 */
7 #include<vector>
8 #include<iostream>
9 using namespace std;
10
11 class iStack
12 {
13 public:
14 iStack( int capacity )
15 : _stack( capacity ), _top( 0 ) {}
16
17 bool pop( int &value );
18 bool push( int value );
19
20 bool full();
21 bool empty();
22 void display();
23
24 int size();
25 private:
26 int _top;
27 vector< int > _stack;
28 };
29
30 inline int iStack::size()
31 {
32 return _top;
33 }
34
35 inline bool iStack::empty()
36 {
37 return _top ? false : true;
38 }
39
40 inline bool iStack::full()
41 {
42 return _top < int(_stack.size() -1) ? false : true;
43 }
44
45 bool iStack::pop( int &top_value )
46 {
47 if ( empty() )
48 return false;
49 top_value = _stack[ --_top ];
50 cout << "iStack::pop(): " << top_value << endl;
51
52 return true;
53 }
54
55 bool iStack::push( int value )
56 {
57 cout << "iStack::push( " << value << " )\n";
58
59 if ( full() )
60 return false;
61
62 _stack[ _top ++ ] = value;
63 return true;
64 }
65
66 void iStack::display()
67 {
68 if ( !size() )
69 {
70 cout << "( 0 )\n";
71 return;
72 }
73 cout << "(" << size() << ")(bot: ";
74
75 for ( int ix = 0; ix < _top; ix ++ )
76 cout << _stack[ ix ] << " ";
77 cout << " :top )\n";
78 }
79
80 int main()
81 {
82 iStack stack( 32 );
83
84 stack.display();
85 for ( int ix = 1; ix < 51; ix ++)
86 {
87 if ( ix % 2 == 0 )
88 stack.push( ix );
89 if ( ix % 5 == 0 )
90 stack.display();
91 if ( ix % 10 == 0 )
92 {
93 int dummy;
94 stack.pop(dummy);
95 stack.pop(dummy);
96 stack.display();
97 }
98 }
99 return 0;
100 }
2 * Question:
3 * 1)What the type of the constructor of class iStack?
4 * 2)In function iStack::full(),if we replace int(_stack.size() -1) with _stack.size() -1, then there yields a "warning C4018: “<”: 有符号/无符号不匹配" under Visual Studio 2008, why?
5 *
6 */
7 #include<vector>
8 #include<iostream>
9 using namespace std;
10
11 class iStack
12 {
13 public:
14 iStack( int capacity )
15 : _stack( capacity ), _top( 0 ) {}
16
17 bool pop( int &value );
18 bool push( int value );
19
20 bool full();
21 bool empty();
22 void display();
23
24 int size();
25 private:
26 int _top;
27 vector< int > _stack;
28 };
29
30 inline int iStack::size()
31 {
32 return _top;
33 }
34
35 inline bool iStack::empty()
36 {
37 return _top ? false : true;
38 }
39
40 inline bool iStack::full()
41 {
42 return _top < int(_stack.size() -1) ? false : true;
43 }
44
45 bool iStack::pop( int &top_value )
46 {
47 if ( empty() )
48 return false;
49 top_value = _stack[ --_top ];
50 cout << "iStack::pop(): " << top_value << endl;
51
52 return true;
53 }
54
55 bool iStack::push( int value )
56 {
57 cout << "iStack::push( " << value << " )\n";
58
59 if ( full() )
60 return false;
61
62 _stack[ _top ++ ] = value;
63 return true;
64 }
65
66 void iStack::display()
67 {
68 if ( !size() )
69 {
70 cout << "( 0 )\n";
71 return;
72 }
73 cout << "(" << size() << ")(bot: ";
74
75 for ( int ix = 0; ix < _top; ix ++ )
76 cout << _stack[ ix ] << " ";
77 cout << " :top )\n";
78 }
79
80 int main()
81 {
82 iStack stack( 32 );
83
84 stack.display();
85 for ( int ix = 1; ix < 51; ix ++)
86 {
87 if ( ix % 2 == 0 )
88 stack.push( ix );
89 if ( ix % 5 == 0 )
90 stack.display();
91 if ( ix % 10 == 0 )
92 {
93 int dummy;
94 stack.pop(dummy);
95 stack.pop(dummy);
96 stack.display();
97 }
98 }
99 return 0;
100 }