设为首页 加入收藏

TOP

C++ 一条代码打印vector内容以及random_shuffle函数
2014-11-24 13:12:28 来源: 作者: 【 】 浏览:0
Tags:代码 打印 vector 内容 以及 random_shuffle 函数

运行结果:


8
1
9
2
0
5
7
3
4
6
Press any key to continue . . .


本实例简单演示了vector的使用,实际上该演示代码同时包含了迭代器(iterator)技术,如v.begin()和v.end(),


他们分别指示vector向量的初始化和末尾指针,同时该代码还采用了STL技术中的算法技术,通过STL算法函数


random_shuffle吧容器类的元素顺序捣乱,并通过算法函数copy实现数据的输出。


有关以上程序的运行过程如下图所示,假设总共有100个元素,以上代码首先定义了一个空的vector对象,然后


赋值如第二步所示,第三步显示了赋值完毕后向量的存储实例,通过random_shuffle函数把容器类的元素顺序捣乱


后向量中存储的元素如第四步所示:



关于random_shuffle函数详解如下:


1.其函数原型如下:



Rearranges the elements in the range [first,last) randomly.

The function swaps the value of each element with that of some other randomly chosen element. When provided, the function rand chooses which element.

The behavior of this function template is equivalent to:






others:




Result of pointer subtraction


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇用C语言求两个数的最大公约数和最.. 下一篇C++ 简单的字符串转换

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: