数学建模社区-数学中国

标题: vector调用push_back为什么会失败?初始化了也不行 [打印本页]

作者: 结局我一人    时间: 2015-4-14 10:06
标题: vector调用push_back为什么会失败?初始化了也不行
头文件handle.h5 D: N/ D, ^6 |5 O
#ifndef _HANDLE_H
5 t# Q! c" Q. J  {2 N8 P5 n9 `$ \#define _HANDLE_H! |: M" G4 a/ M0 R1 g
# @3 w! j9 |3 Y! |+ {
class Handle{
1 Q3 W* j; [, P7 u    struct StackOfInt;8 s6 t8 U  U3 Q( r& @) x: f% g& T0 y
    StackOfInt* smile;* |) e  T: V4 Z+ o; a
public:
) N* U$ Q' @. P/ d9 K) ]  H# r" j    void initialize();
* L7 f7 h  ]9 J# j. U    void cleanup();- }' \; P, X/ s, A, n
    void push(int elem);+ l3 ~0 K6 g! j4 X; y- ?. `$ H9 `2 y
    int fetch();* y5 _# c3 D8 Q1 S: X$ _
    int pop();0 z+ g/ f7 r0 n6 x; K8 ]# v7 I' k2 ?
    int count();" s0 c0 c0 W8 H; y$ ^
};) U' F7 u  h' Y6 ~/ P( q4 z
" D2 P0 ?% a$ T
#endif//_HANDLE_H
. F: }& n$ N# Q: J
, w7 z+ h5 L8 N  f* h- B类class的函数定义  W$ _1 r/ i. Y$ e2 k* |
#include<iostream># m& F" a' \( S% o; p, w
#include<vector>7 \& Y  E; E& s5 [& j
#include "handle.h": V* O  p  Q) a0 B
using namespace std;8 V; J6 ?: [+ U6 q& w# ~
9 c# g1 Y( v0 T" M; @/ d' y
struct Handle::StackOfInt{
3 @) J$ q1 P, ]+ d' V* x: p' J1 g    vector<int> top;
- {! U7 v- x1 q1 Y7 t, `};' q: o* ~9 r7 f/ A* M
! m% m, V$ m7 H  D
void Handle::initialize()* }& Z/ e' o) K( j- E! i
{% P7 K9 g! V$ A% d; ~
    StackOfInt* smile = new StackOfInt;4 `3 I* Q" f' k+ j
    smile->top.reserve(100);& g6 g% L! l9 ?8 m( F
    cout << smile->top.capacity() << endl;
0 e& X  J% E) ~9 G! T}
/ Z& N3 j' ?; d1 @- b( y) |( g- N% N5 e* y8 m3 S: _7 U
void Handle::cleanup()5 ~$ y: Z! C2 D; {3 A9 Q/ C- R
{! N" W8 C" A, [& j; s7 W
    delete smile;
  v# Z7 p: y/ y, O) q' n/ F}
0 e& j2 j* S/ F9 V( M) O- E0 A' \: X6 N( h) Q) Q
void Handle::push(int elem)' d1 ^7 g* {$ h- V( G# s2 {6 R
{
3 ~4 p4 g- Z0 S1 o0 h    cout << "before push\n";
5 `. c  E8 c4 M, A$ _. |' S    smile->top.push_back(elem);4 L% {! J+ B& @! U) X3 [( @* Y
    cout << "after push\n";1 ], B! b( [/ |2 ~- ~" ]  l+ Q4 `
}, x/ ?& {7 u9 T. O  C% ?
int Handle::fetch()) _7 O' [" q! [
{0 s# ^; Y( l  t6 ?
    return smile->top.back();
& z1 _$ |& D7 W! t( i' j; V* W}
+ k% k# A4 `3 c& aint Handle::pop()
4 I# p. v- R8 e{2 h* g1 K, [* o- F
    int n = smile->top.back();
9 v+ p  \/ K+ [    smile->top.pop_back();' m% u& ]: h; v1 B
    return n;
' K% }2 a# u  G7 k; a! M/ [9 w}
3 h* }; {; w# P$ P3 e! |6 [int Handle::count()
- {+ T8 e3 F. M5 x$ i! x$ D5 z{
, k! P9 p! l6 V9 L- h    return smile->top.size();5 B& G9 b' R" z' p, F0 s
}! W( q; `! O1 n1 [; s* p
& ]2 \5 C  t4 e. O1 @, W, b
主函数 main.cpp* g7 I8 y* b% T% l
#include<iostream>/ B9 ~$ `" x# M8 u8 f
#include "handle.h"
/ V: m9 F9 a: ]  N+ U4 x% \using namespace std;
5 J, v7 d8 J1 Z4 s6 a( b4 t! G/ u5 t9 N- c# N, N! _
int main()
2 t& l' ~) Z4 A1 z3 I5 k{
9 ^8 d' o$ t8 N3 _2 @$ Q( a    Handle h;
0 o% c: p7 n* u  R4 ^. O    h.initialize();% r8 ]6 q' R3 R; F  v( T
    for (int i = 0; i < 10; i++)
1 h9 ^. q$ L7 W4 |    {, j2 `4 T( f. g
        h.push(i);' e0 \2 t2 b; G. p( z  N( {& h
        cout << "last element is " << h.fetch() << endl;  T' K+ e6 y, t1 M% ~+ w. d
    }. @3 a" q, v3 Z8 s% G) u: K, ]
    for (int i = 0; i < 10; i++)
) B2 W- [; _6 \3 [4 n! P5 k    {$ B' t0 u1 U8 j/ v3 y$ P' ~& S4 A
        h.pop();8 j1 q; L3 r6 B: M0 I
    }
# I& h: }3 }7 g3 t8 F& E
# F: z! L4 ~5 t9 |$ E+ t) T1 ~# g    return 0;
$ i  K6 n9 c5 d9 W3 u, L" T$ ?}$ }- u' c. H% m6 r+ @  [; X

+ Z6 \+ b. J  h8 F9 d. {2 ~
# N/ T: k% m3 n9 S
6 `4 F0 t+ q$ U& m! t# E% V
作者: 超级管理员    时间: 2015-4-14 21:29
是代码里缺东西了吗?' G0 S: f' R# B9 S9 e# s

: @9 g) P2 e; A) P" ~




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5