数学建模社区-数学中国

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

作者: 结局我一人    时间: 2015-4-14 10:06
标题: vector调用push_back为什么会失败?初始化了也不行
头文件handle.h
) S- N$ s& Y" V! U#ifndef _HANDLE_H$ f1 R# r+ ?5 P* w7 i# B! t
#define _HANDLE_H
# K% l! E/ m3 n! e3 b' q6 o
# E! M: G8 g% I  z- qclass Handle{* y8 p0 ^( U9 @# w- B4 A( P
    struct StackOfInt;3 p# C- M: ~2 c; R, G% v# P
    StackOfInt* smile;" @, N* V* M, d. H& Y) x
public:
7 A1 N3 e0 B; [4 Y+ W  j# \' c  x$ |. `    void initialize();
+ [7 f) C& a& V* K. i0 f- p9 z    void cleanup();
( A8 c4 U6 X, w  O8 a9 n* s$ h    void push(int elem);
; e, g* |0 A- w+ O- K8 F    int fetch();" N% V- M1 ]( p6 I
    int pop();: j5 a5 w$ U2 g) f/ }! [
    int count();+ {) b/ f" k, s2 P0 x8 X+ q
};( K" E+ [! s2 e  q2 L2 _& S, {

: ~' F. M, N! Y7 S6 ?& C$ m0 o. |#endif//_HANDLE_H7 k2 n5 U/ s! L  ]. t
# s' q3 ?' J3 I$ d
类class的函数定义' N' u2 q" b4 M' y0 q& t" q
#include<iostream>
3 n/ i2 X* _% W/ G#include<vector>) z- \. Y/ R9 i& V% q5 r
#include "handle.h"
5 x) b& R  f! u# Y4 t3 Musing namespace std;2 f3 L1 \, M0 \8 P: B
5 ^, \% M( v- J. S, w1 B
struct Handle::StackOfInt{5 g" c7 R2 J/ f  f7 Q
    vector<int> top;. E5 M! a7 }( n, r/ l& V1 \
};& Y: b6 ]8 v' ]5 X! I

3 w) O3 z/ I/ {# u8 Avoid Handle::initialize(): b2 d% \8 t6 Q" W. n
{
  @4 p9 B  ?- I, p% W8 z    StackOfInt* smile = new StackOfInt;7 v; }, v5 `8 ^
    smile->top.reserve(100);
- G! o" s4 R( c/ E0 b0 H: I5 y" n    cout << smile->top.capacity() << endl;
7 p* L  o: [4 `/ c( D& ~- e0 {$ b. @/ D}/ m3 k: M! o! y3 Z  _& @, X3 [
4 R: X/ R8 h* ^' x7 a
void Handle::cleanup(). E; K6 i1 m: K6 A) o/ A
{- U6 A3 J; E5 G
    delete smile;' \% W( a# j8 V& T* r
}4 u; m3 A* r" U4 }. k, ]$ `
& W: O1 }/ _7 E# t7 ?9 h
void Handle::push(int elem)
0 M6 M% v- i, @) }$ V  ?$ j{( O' \5 p" [) O
    cout << "before push\n";! r' R4 Q# D; O6 X) Y
    smile->top.push_back(elem);
* S# q6 x7 M7 X$ \5 `- q6 q$ D    cout << "after push\n";' z* l- G1 W' P* C# p' N
}
3 g0 S8 ^: m1 B0 Y; c# Oint Handle::fetch()
1 n/ ?+ v6 ^. c$ @{
0 P' ^. Y3 y) \  ^9 u7 c    return smile->top.back();7 x) s' h: d4 Y3 W" C+ P" r1 O
}
7 {/ ^5 m) {2 g, N/ hint Handle::pop()
9 ^' u" ]2 F2 i4 b# z# y{. f- j+ n5 R% U+ x, x
    int n = smile->top.back();0 X" R4 O  n# J  |4 v1 y+ Q
    smile->top.pop_back();
0 N& X) R5 D7 ^2 s& z' V2 q    return n;
9 f$ b% X- X4 `2 E5 x$ c+ [( o}
+ d" [9 o, I( f# T+ p& ~0 Oint Handle::count()
& W1 x, T; ]* c8 D. @$ X- j4 L{
/ x( u8 m) X, B, |5 C; A    return smile->top.size();
% Q' g' z  D9 r% _}
. q3 i' E  v& ?+ e! l" N& `
/ ?- |: J7 m& C  p主函数 main.cpp
1 _+ f4 ]) i* e#include<iostream>+ q7 r) n- S6 y" Y
#include "handle.h"% C! Z+ N0 ?- ~4 X2 \$ p" e# f
using namespace std;
  \0 E0 P! i- L8 L/ L. |/ Q0 O$ r. x3 [/ K  T. P! G, k/ O5 y
int main()
3 ]! u+ O) A7 c5 x' I+ |/ S{
1 i) L/ k6 a1 L6 S    Handle h;
6 x5 S; Q4 C5 \/ h( m: [    h.initialize();- N4 |( |1 y0 h, I4 B
    for (int i = 0; i < 10; i++)9 A9 x' m' w1 |3 j
    {  g( h6 J- B* D8 s/ s& t
        h.push(i);
0 f6 x8 o( C" }        cout << "last element is " << h.fetch() << endl;
1 [' C0 G* B  e7 m. v" I+ P- |    }
) U3 ?( U. _7 O  c    for (int i = 0; i < 10; i++)
( \  u- L( r! q# N* W# j- \6 g    {
8 _) \8 G6 E% F9 h& G        h.pop();. c% k1 b4 a0 N; s
    }9 Z) X' G9 k# J2 I6 n

2 j8 M$ N( W) A4 ~" `- U    return 0;
( |: n+ f! d) N}
4 W! P1 u3 H2 A3 q5 i# j+ N$ A& I/ S& m/ z1 l2 K
! @9 ^' V( e  f
7 a0 B! |( |- g5 \+ U! i" b& K* ]

作者: 超级管理员    时间: 2015-4-14 21:29
是代码里缺东西了吗?6 r3 e* Q% i+ W5 d8 j2 I! M

' {" m, c9 M' x- {. v5 t# ]) T




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