第15周实践项目-洗牌(2)

来源:互联网 发布:ant.jar 执行sql脚本 编辑:程序博客网 时间:2024/05/20 21:58

问题描述及代码:

/**copyright (c) 2016,烟台大学计算机学院*All rights reserved.*文件名称:hellow.cpp*作者:田甜*完成日期:2016年6月12日*版本号:v1.0**问题描述:洗牌*输入描述:////*程序输出:///*/#include <iostream>#include <vector>#include <iterator>#include <algorithm>#include <ctime>using namespace std;typedef vector<int> Intvector;void Swapshuff(Intvector &data,int time){    unsigned size=data.size(),p1,p2;//无符号可以储存两倍长度,省略了后面的关键词就默认unsigned int    while(time--)    {        p1=rand()%size;        p2=rand()%size;        swap(data[p1],data[p2]);//swap函数是namespace中的函数    }}int main(){    ostream_iterator<int> os(cout," ");    srand(time(NULL));    vector<int> poker;    for(int i=1;i<=54;i++)    {        poker.push_back(i);    }    cout<<"Before Shuffle:"<<endl;    copy(poker.begin(),poker.end(),os);    cout<<endl;    Swapshuff(poker,54);    cout<<"After Shuffle:"<<endl;    copy(poker.begin(),poker.end(),os);    cout<<endl;    return 0;}

运行结果:


心得体会:

这次又遇到让人很无奈的情况,出现一堆貌似很严重的debug,后来发现是因为我在声明迭代器的时候写成了ostream iterator<int>  os(cout," "); 应该是ostream_iterato<int> os(cout," ");

0 0
原创粉丝点击