1)STL中的函数random_shuffle()用来对一个元素序列进行重新排序(随机的),包含在头文件 algorithm.h中

函数原型如下:

template<class RandomAccessIterator>

void random_shuffle(

RandomAccessIterator _First, //指向序列首元素的迭代器

RandomAccessIterator _Last  //指向序列最后一个元素的下一个位置的迭代器

);

template<class RandomAccessIterator, class RandomNumberGenerator>

void random_shuffle(

RandomAccessIterator _First,

RandomAccessIterator _Last,

RandomNumberGenerator& _Rand //调用随机数产生器的函数

);

random_shuffle()是一个完全通用的算法,适用于内置数据类型和用户自定义类型。同时,由于STL算法不仅适用于容器,也适用于序列,因此,random_shuffle()算法可用于内置数组。

实例代码如下:

#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

int main()

{

//用于内置数据类型

std::vector<int> vi;

for(int i=0; i<100; i++)

{

vi.push_back(i);

}

std::random_shuffle(vi.begin(), vi.end());

std::vector<int>::iterator it;

for(it=vi.begin(); it!=vi.end(); it++)

{

std::cout<<*it<<std::endl;

}

//用于用户自定义类型

std::vector<std::string> vs;

vs.push_back(std::string("Sunday"));

vs.push_back(std::string("Monday"));

vs.push_back(std::string("Tuesday"));

vs.push_back(std::string("Wednesday"));

vs.push_back(std::string("Thursday"));

vs.push_back(std::string("Friday"));

vs.push_back(std::string("Saturday"));

std::random_shuffle(vs.begin(), vs.end());

for(int i=0; i<7; i++)

{

std::cout<<vs[i]<<std::endl;

}

//用于数组

char arr[6] = {'a', 'b', 'c', 'd', 'e', 'f'};

std::random_shuffle(arr, arr+6);

for(int i=0; i<6; i++)

{

std::cout<<arr[i]<<" ";

}

std::cout<<std::endl;

system("pause");

return 0;

}

2)STL中的函数transform()用来遍历一个容器里面指定范围的元素,并对这些元素执行指定的操作,函数原型如下:

template<class InputIterator, class OutputIterator, class UnaryFunction>

OutputIterator transform(

InputIterator _First1, //元素起始位置的输入迭代器

InputIterator _Last1, //元素结束位置的输入迭代器

OutputIterator _Result, //执行指定操作的元素的起始位置的输出迭代器

UnaryFunction _Func //执行的操作(函数)

);

template<class InputIterator1, class InputIterator2, class OutputIterator,

class BinaryFunction>

OutputIterator transform(

InputIterator1 _First1, //第一个操作范围的元素起始位置的输入迭代器

InputIterator1 _Last1, //第一个操作范围的元素结束位置的输入迭代器

InputIterator2 _First2, //第二个操作范围的元素起始位置的输入迭代器

OutputIterator _Result, //最终范围的元素的起始位置的输出迭代器

BinaryFunction _Func //执行的操作(函数)

);

上面第一个版本的算法对区间[_First1, _Last1]中的每个元素应用函数_Func,并将每次_Func返回的结果存储到_Result中;

第二个版本的算法以类似的方式运行,但它期望获得两个序列并逐次调用一个处理成对元素的二元函数。

实例代码如下:

#include <vector>

#include <algorithm>

#include <functional>

#include <iostream>

// The function object multiplies an element by a Factor

template <typename T>

class MultiValue

{

private:

T Factor;   //The value to multiply by

public:

//Constructor initializes the value to multiply by

MultiValue(const T& _val) : Factor(_val)

{

}

//The function call for the element to be multiplied

T operator()(T& elem) const

{

return elem*Factor;

}

};

int main()

{

using namespace std;

vector<int> v1, v2(7), v3(7);

vector<int>::iterator it1, it2, it3;

//Constructing vector v1;

for(int i=-4; i<=2; i++)

{

v1.push_back(i);

}

cout<<"Original vector v1=(";

for(it1=v1.begin(); it1!= v1.end(); it1++)

{

cout<<*it1<<" ";

}

cout<<")."<<endl;

//Modifying the vector v1 in place

transform(v1.begin(), v1.end(), v1.begin(), MultiValue<int>(2));

cout<<"The elements of the vector v1 multiplied by 2 in place gives:"

<<"/n v1mod=(";

for(it1=v1.begin(); it1!=v1.end(); it1++)

{

cout<<*it1<<" ";

}

cout<<")."<<endl;

//using transform to multiply each element by a factor of 5

transform(v1.begin(), v1.end(), v2.begin(), MultiValue<int>(5));

cout<<"Multiplying the elements of the vector v1mod/n"

<<"by the factor 5 & copying to v2 gives:/n v2=(";

for(it2=v2.begin(); it2!=v2.end(); it2++)

{

cout<<*it2<<" ";

}

cout<<")."<<endl;

//The second version of transform used to multiply the

//elements of the vectors v1mod & v2 pairwise

transform(v1.begin(), v1.end(), v2.begin(), v3.begin(),

multiplies<int>());

cout<<"Multiplying elements of the vectors v1mod and v2 pairwise "

<<"gives:/n v3=( ";

for(it3=v3.begin(); it3!=v3.end(); it3++)

{

cout<<*it3<<" ";

}

cout<<")."<<endl;

system("pause");

return 0;

}

程序运行后输出如下:

Original vector  v1 = ( -4 -3 -2 -1 0 1 2 ).
The elements of the vector v1 multiplied by 2 in place gives:
 v1mod = ( -8 -6 -4 -2 0 2 4 ).
Multiplying the elements of the vector v1mod
 by the factor 5 & copying to v2 gives:
 v2 = ( -40 -30 -20 -10 0 10 20 ).
Multiplying elements of the vectors v1mod and v2 pairwise gives:
 v3 = ( 320 180 80 20 0 20 80 ).

《认清C++语言》的random_shuffle()和transform()算法相关推荐

  1. 《认清C++语言》のrandom_shuffle()和transform()算法

    1)STL中的函数random_shuffle()用来对一个元素序列进行重新排序(随机的),函数原型如下: template<class RandomAccessIterator> voi ...

  2. random_shuffle 和transform算法

    1)STL中的函数random_shuffle()用来对一个元素序列进行重新排序(随机的),函数原型如下: std::random_shuffle

  3. C语言实现DES加解密算法

    C语言实现DES加解密算法 DES加解密 DES加解密 #include <stdio.h> #include <stdlib.h> #include <string.h ...

  4. C#实现DLT直接线性变换(Direct Linear Transform)算法

    C#实现DLT直接线性变换(Direct Linear Transform)算法 参考资料: 1.武大版<工业测量技术与数据处理>P100-P106 2.转载博文--MATLAB实现DLT ...

  5. 【每日算法】C语言8大经典排序算法(2)

    接上文--->[每日算法]C语言8大经典排序算法(1) 二.插入类排序 插入排序(Insertion Sort)的基本思想是:每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子文件中 ...

  6. 数据结构源码笔记(C语言):置换-选择算法

    //实现置换-选择算法#include<stdio.h> #include<malloc.h> #include<string.h> #include<std ...

  7. Algorithm:C+语言实现之数组相关算法(和为定值的两个数、和为定值的m个数、荷兰国旗、长度为2n的洗牌算法、任意长度数组的洗牌算法)

    Algorithm:C+语言实现之数组相关算法(和为定值的两个数.和为定值的m个数.荷兰国旗.长度为2n的洗牌算法.任意长度数组的洗牌算法) 目录 数组 1.寻找和为定值的两个数 2.和为定值的m个数 ...

  8. Algorithm:C++语言实现之队列相关算法(最短路径条数问题、拓扑排序)

    Algorithm:C++语言实现之队列相关算法(最短路径条数问题.拓扑排序) 目录 队列 1.最短路径条数问题 2.拓扑排序 队列 1.最短路径条数问题

  9. Algorithm:C++语言实现之链表相关算法(单链公共结点问题、一般LCA、括号匹配、最长括号匹配、逆波兰表达式Reverse Polish Notation、直方图矩形面积、收集雨水问题)

    Algorithm:C++语言实现之链表相关算法(单链公共结点问题.一般LCA.括号匹配.最长括号匹配.逆波兰表达式Reverse Polish Notation.直方图矩形面积.收集雨水问题) 目录 ...

最新文章

  1. The Human Touch 将人工智能和机器人用于病人工作的实际和伦理意义
  2. boost库之tcp实例(同步方式)
  3. 大数据为何让传统银行焦虑?
  4. 两个有序数组合成一个有序数组
  5. 一篇对伪共享、缓存行填充和CPU缓存讲的很透彻的文章
  6. Eigen与Matlab语法及语义辞典
  7. PHP-CGI, FastCGI, PHP-FPM的关系和区别
  8. ubuntu安装与配置mysql_ubuntu下mysql的安装与配置
  9. 睡眠声音识别中的准确率问题(三)--采集的音频测试结果及分析
  10. Ruffer Investment共持有略高于3%的比特币敞口
  11. Spring核心容器简介
  12. 关于ArcGIS Mobile回传数据中常遇到的问题整理!
  13. 提取特征点的方式,opencv2.x和opencv3.x的区别
  14. jQuery 插件 autocomplete 的使用
  15. JS打开新窗口的2种方式
  16. 浅谈对二分查找最大次数的理解
  17. C#通过ToLower()方法将字符串转换成小写的代码
  18. 【Python】MySQLdb库的使用以及格式化输出字段中的值
  19. jdbc连接mysql数据库,设置字符集编码
  20. 我,喜提招商银行,当爹啦

热门文章

  1. 数据库-多条件查询-优先级
  2. SocketIO-nio
  3. java的发展_java的发展
  4. 设置二进制或者16/32位的某一位的值
  5. Xilinx下载方式(具体可以参考配置MCS文件时右下角help调出的doc)
  6. 用python写网络爬虫 -从零开始 3 编写ID遍历爬虫
  7. linux 压缩及解压缩 命令
  8. Apache JMeter 3.2版新特性详述
  9. 记crontab脚本未执行问题排查
  10. win7 CapsLooks键 转换成Ctrl键