问题描述及代码:
/*
02.*copyright (c) 2016,烟台大学计算机学院
03.*All rights reserved.
04.*文件名称:hellow.cpp
05.*作者:田甜
06.*完成日期:2016年6月6日
07.*版本号:v1.0
08.*
09.*问题描述:在扑克牌游戏中,每次游戏开始都要求把54张牌重新排列一下,称为洗牌。试编写程序将一副扑克牌(用54个整数1~54表示)随机洗好后,顺序输出54张牌的情况。
10.*输入描述:///
11.*程序输出:///
12.*/
#include <iostream>
#include <ctime>
#include <vector>
#include <list>
#include <iterator>
#include <cstdlib>
using namespace std;
typedef vector<int> IntVector;
typedef unsigned int VIndex;
//void vectorShuffle(IntVector &unshuffled,IntVector &shuffled);这里不用声明的吗?
void vectorShuffle(IntVector &unshuffled,IntVector &shuffled)
{
VIndex p,size=unshuffled.size();
while(size)
{
p=rand()%size--;
shuffled.push_back(unshuffled[p]);
unshuffled.erase(unshuffled.begin()+p);//unshuffled 是一个容器,begin是这种容器的一个函数
}
}
int main()
{
ostream_iterator<int> os(cout," ");
srand(time(NULL));
IntVector c,sc;
for(VIndex i=1;i<=54;i++)
c.push_back(i);
cout<<"Befor Shuffled:"<<endl;
copy(c.begin(),c.end(),os);
cout<<endl;
vectorShuffle(c,sc);
cout << "After shuffled:" << endl;
copy(sc.begin(),sc.end(),os);
cout<<endl<<endl;
return 0;
}
运行结果:
心得体会:
ostream_iterator是流迭代器。
流迭代器是标准模板库中的。因此是类模板。
ostream_iterator<int>
指定了类型,就是迭代器读写的类型。
通过这个流迭代器可以把你要输入的写入到指定的流中。
cout就是指定的流。就是标准输出。
可以改成一个输出流就可以,比如一个文件。
通俗的一点说,你把它看成一个指向输出流的指针。通过这个指针你可以把东西写的输出流中。
copy (v.begin(),v.end(),output);
这个意思就是说,把向量V中的数据放到cout输出流中,通过流迭代器output.
ostream_iterator<int> output(cout ,"*");
这个的意思说,放到输出流的时候,没放一个整数,就末尾添加一个*.
引用自百度知道:
ID: wanfustudio