数学建模社区-数学中国
标题:
vector调用push_back为什么会失败?初始化了也不行
[打印本页]
作者:
结局我一人
时间:
2015-4-14 10:06
标题:
vector调用push_back为什么会失败?初始化了也不行
头文件handle.h
* _# ~# ]" M A9 d7 @6 d4 _
#ifndef _HANDLE_H
6 s+ k1 @! Z# i. U8 S5 ]8 c, P0 [
#define _HANDLE_H
9 _* Q1 D+ E" g t4 c- ^( @
7 v) J& L) l6 z" P
class Handle{
0 Y' @% A2 D+ q* M P
struct StackOfInt;
) v8 s" a8 _; l9 W
StackOfInt* smile;
8 S0 o1 d' V5 [ t; x: }: \
public:
* E2 |6 A% q! ?5 J3 ~5 n" `! S
void initialize();
0 ?0 [$ q O% N
void cleanup();
6 z ?! f0 E, o8 P- n
void push(int elem);
5 h! d; U: W% m: ^' g6 b
int fetch();
! f$ J* a! H" P3 H" b
int pop();
N; _ b, |5 m5 U$ l- u3 W8 N
int count();
5 ^6 d2 h2 o( r) r) J% ~' L
};
6 I+ a4 f, X& t: U4 F
5 K/ g9 Y$ e; Q4 B
#endif//_HANDLE_H
$ F* i/ I+ q: J
s" Q- Q& a$ k0 J) y
类class的函数定义
/ L+ m6 [9 E1 _( A7 d
#include<iostream>
5 v, U. r1 a _3 r& W
#include<vector>
: G: a% J7 q, p3 t+ |. @0 A* U) _
#include "handle.h"
7 O, x$ J" n( |4 @; q
using namespace std;
! X( D; \2 I1 M2 L% T5 K* c% \5 g) f
* g1 O. H3 u1 ]; j, U; p
struct Handle::StackOfInt{
f. S2 `0 q1 @
vector<int> top;
$ ?, E4 {, s* A. P, d. |1 H% ~
};
: x* Q" C$ e& X/ C
( A5 I# |$ i6 ]" ]$ k) O
void Handle::initialize()
9 y) H( m4 }$ e4 ]/ P g! k% [- P
{
% P7 d7 Z- n7 I7 }) n8 N
StackOfInt* smile = new StackOfInt;
/ H* i8 V' V! Q9 r R5 g
smile->top.reserve(100);
; v' ?( E+ a1 o; g0 K+ n# H+ ]
cout << smile->top.capacity() << endl;
- t" ^6 Y* U1 w: ^4 T
}
0 P4 O, H) g% r7 E, U- _5 G
" N; P) r3 g: c
void Handle::cleanup()
: E) N7 T# o1 i3 n2 w5 P+ _% ?3 Q
{
6 ]% u) H" y9 \0 Y+ A) [5 i) O8 u; U
delete smile;
! m' \, o. D4 x* R" k4 w
}
# Z/ t; o6 T5 X, i
- A/ Q! m K8 |7 x* m0 F
void Handle::push(int elem)
6 K% m, ]! R0 A: Z6 u
{
* `: k7 T9 p/ X3 J6 R% n
cout << "before push\n";
& d1 |( y) O& T* e
smile->top.push_back(elem);
1 s, w& w/ h. s3 D' W. B
cout << "after push\n";
6 d6 [7 ?& w+ a& y
}
+ [" g2 p# Q4 U( B( j5 T
int Handle::fetch()
4 W; L1 G0 S4 [! Q* y& U o
{
) [* v) S8 l; ^ t* _6 [% _
return smile->top.back();
4 d+ w5 k$ U+ K! k+ o! d6 ]
}
( Y1 |0 t, t9 B, j. O! y+ C
int Handle::pop()
2 t i( m4 I: e$ E% \2 {) y# }
{
* F) U, a) S* v
int n = smile->top.back();
# K! p( N- p% w, T+ ~- F3 Q: }
smile->top.pop_back();
6 v# f4 W7 t, Y r, W
return n;
9 i9 K; \7 j! ]' S0 E) G
}
6 c K. X3 ~8 m: x
int Handle::count()
) C* b4 S$ A: l, Z6 u. f% i7 I3 a9 b
{
0 p) w7 q# t! W$ p* e: P& O3 R
return smile->top.size();
" B3 W9 r& j; B' C) C/ s V4 e
}
) d# w0 p y |# W. [
# y* s' F' k# O5 B6 M, m
主函数 main.cpp
% P( z% M. s% |/ y: x1 |; Q
#include<iostream>
& u4 a6 \% Q% X$ m3 j# y4 L
#include "handle.h"
4 K" J) b% i+ \8 }3 ^! T
using namespace std;
# }) W$ C/ X8 Y/ k
: Y3 B9 o& @% u* o. C: s+ I5 ?
int main()
: P/ ]- Y. S( w5 Q2 @) M( |' [0 [+ j
{
$ V3 N! O5 ]% y& H9 D' t
Handle h;
" x7 F; j8 V( e: q7 J
h.initialize();
+ p8 b1 n6 K+ P/ v# N
for (int i = 0; i < 10; i++)
7 O+ V" ?" X& Q+ d1 i Z! i
{
# a, w6 t0 j {6 S% V
h.push(i);
: z R4 `5 l+ Q
cout << "last element is " << h.fetch() << endl;
7 m* G) o, W2 Y% z- c
}
8 O* V, g2 ~" ^0 |1 m0 w
for (int i = 0; i < 10; i++)
( W* S& P( ]. x7 a* w: M9 L1 _
{
/ o# r" o- j; c! F6 h: j7 r
h.pop();
) B' b3 G' W3 J9 @
}
# t9 t3 J- J$ a$ ~- \5 ]0 d
% U2 f8 u$ L, `- W$ d- _. B( G
return 0;
% |) P7 W5 r) Y# D
}
6 l" x7 n1 J# l" u) F% ~2 D O
5 I3 o( d" Z" r% d
: ] L7 Q# g, f2 k7 ]
0 c7 j6 a8 J) |& v) p- Q; [6 c
作者:
超级管理员
时间:
2015-4-14 21:29
是代码里缺东西了吗?
7 E R2 D9 C$ N% G
" `, f8 P. R; f9 z
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5