practice4_2_stack_struct

 

// Practice4_stack.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stack>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <string>

using namespace std;//一定不要忘记这句

struct BankLevel
{
    string name;
    unsigned int level;
}; /* 开始这里少写一个分号,导致编译不过,教训!!*/

string strs[5] = {"zhonghang", "gonghang", "nonghang", "jianhang", "jiaohang"};

void initStack(stack<BankLevel> &ss, unsigned int size)
{
    unsigned int num = 0;
    srand(unsigned(time(0)));
    for(unsigned int i = 0; i < size; i++)
    {
        num = rand()%100;
        BankLevel blevel = {strs[i], num};
        ss.push(blevel);
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    stack<BankLevel> ss;
    initStack(ss, 5);

    stack<BankLevel> ssCopy(ss);
    cout << "size of ssCopy: " << ssCopy.size() << endl;

    while(!ss.empty())
    {
        cout << ss.top().name << "," << ss.top().level << endl;
        ss.pop();
    }

    return 0;
}

 

size of ssCopy: 5
jiaohang,2
jianhang,31
nonghang,93
gonghang,36
zhonghang,51

 

posted on 2017-03-02 22:21  cleverlzc  阅读(98)  评论(0编辑  收藏  举报