#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
int main (int argc, char *argv[])
{
int n = 10;
vector<int> v;
//append integers 0 to n-1 to v
for (int i = 0; i < n; ++i) {
v.push_back(i);
}
//random shuffle the values of v
random_shuffle (v.begin(), v.end());
//print all values of v to screen
copy (v.begin(), v.end(), ostream_iterator<int> (cout, "\n"));
return 0;
}
运行结果: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实现数据的输出。
有关以上程

本文介绍了如何使用C++的random_shuffle函数打乱vector元素顺序,并展示了一条代码实现遍历并打印vector的内容。通过示例代码,详细解释了迭代器和指针在过程中的应用。
最低0.47元/天 解锁文章


被折叠的 条评论
为什么被折叠?



