◆ 常用的遍历算法:

1.1、用指定函数依次对指定范围内所有元素进行迭代访问。该函数不得修改序列中的元素

functor for_each(iteratorBegin, iteratorEnd, functor对每个元素进行操作);

1.2、与for_each类似,遍历所有元素,但可对容器的元素进行修改( transform 是变换的意思)

OutputIterator transform(InputIterator _First1,   InputIterator _Last1,  OutputIterator _Result, 一元functor对每个元素进行操作);
OutputIterator transform(InputIterator1 _First1, InputIterator1 _Last1, InputIterator2 _First2,  OutputIterator _Result, 二元functor对每个元素进行操作);

1、

1.1、第6讲 PPT.42

◆ for_each() :  用指定函数依次对指定范围内所有元素进行迭代访问。该函数不得修改序列中的元素。

ZC: 只有一种参数格式,返回值为 传入的functor 。

ZC: vs2010 测试代码:

#ifdef WIN32
#pragma warning (disable: 4786)
#endif #include <string>
#include <vector>
#include <set> #include <algorithm> // 算法
#include <numeric> // 算法
#include <functional> // 算法 using namespace std; typedef void (*FUNC1)(const int &_iItem);
void (*FUNC2)(const int &_iItem); void Show(const int &iItem)
{
//cout << iItem;
printf("iItem : %d\n", iItem);
} void main()
{
int iArray[] = {,,,,};
vector<int> vecInt(iArray, iArray+sizeof(iArray)/sizeof(iArray[]));
//FUNC1 = for_each(vecInt.begin(), vecInt.end(), Show); // ZC: 编译通不过
FUNC2 = for_each(vecInt.begin(), vecInt.end(), Show); if (FUNC2 == Show)
printf("FUNC2 == Show\n");
else
printf("FUNC2 != Show\n"); system("pause");
}

ZC:控制台输出:

iItem : 0
iItem : 1
iItem : 2
iItem : 3
iItem : 4
FUNC2 == Show
请按任意键继续. . .

1.2、第6讲 PPT.42

◆ transform() :   与for_each类似,遍历所有元素,但可对容器的元素进行修改

ZC: 2种参数格式,返回值为 输出容器中 经过转换后的最后一个元素的iterator 。

ZC: VC6 测试代码:

 #ifdef WIN32
#pragma warning (disable: 4786)
#endif #include <string>
#include <vector>
#include <set> #include <algorithm> // 算法
#include <numeric> // 算法
#include <functional> // 算法 using namespace std; int Increase (int i)
{
return i+;
} void main()
{
vector<int> vecIntA;
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back(); transform(vecIntA.begin(), vecIntA.end(), vecIntA.begin(), Increase); //vecIntA : {2,4,6,8,10} int iIdx = ;
vector<int>::iterator it = vecIntA.begin();
while (it != vecIntA.end())
{
printf("[%02d] ==> %d\n", iIdx, *it);
it ++;
}
}

ZC:控制台输出 - 1:

 [00] ==> 2
[00] ==> 4
[00] ==> 6
[00] ==> 8
[00] ==> 10
Press any key to continue

ZC: vs10 测试代码:

 // alg_transform.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream> // The function object multiplies an element by a Factor
template <class Type>
class MultValue
{
private:
Type Factor; // The value to multiply by
public:
// Constructor initializes the value to multiply by
MultValue ( const Type& _Val ) : Factor ( _Val )
{} // The function call for the element to be multiplied
int operator ( ) ( Type& elem ) const
{
return elem * Factor;
}
}; template <class Type>
class MultValue2
{
private:
Type Factor; // The value to multiply by
public:
// Constructor initializes the value to multiply by
MultValue2 ( const Type& _Val ) : Factor ( _Val )
{} // The function call for the element to be multiplied
int operator ( ) ( Type& elem1, Type& elem2 ) const
{
return elem1*elem2 * Factor;
}
}; int main( )
{
using namespace std;
vector <int> v1, v2(), v3();
vector <int>::iterator Iter1, Iter2 , Iter3; // Constructing vector v1
int i;
for ( i = - ; i <= ; i++ )
{
v1.push_back( i );
} cout << "Original vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl << endl; // Modifying the vector v1 in place
transform (v1.begin ( ) , v1.end ( ) , v1.begin ( ) , MultValue<int> ( ) );
cout << "The elements of the vector v1 multiplied by 2 in place gives:"
<< "\n v1mod = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl << endl; // Using transform to multiply each element by a factor of 5
transform ( v1.begin ( ) , v1.end ( ) , v2.begin ( ) , MultValue<int> ( ) ); cout << "Multiplying the elements of the vector v1mod\n "
<< "by the factor 5 & copying to v2 gives:\n v2 = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")." << endl << endl; // The second version of transform used to multiply the
// elements of the vectors v1mod & v2 pairwise
// ZC: 预定义的 二元functor
//transform ( v1.begin ( ) , v1.end ( ) , v2.begin ( ) , v3.begin ( ) ,
// multiplies <int> ( ) );
// ZC: 自定义的 二元functor
transform ( v1.begin ( ) , v1.end ( ) , v2.begin ( ) , v3.begin ( ) , MultValue2<int> ( ) ); cout << "Multiplying elements of the vectors v1mod and v2 pairwise "
<< "gives:\n v3 = ( " ;
for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
cout << *Iter3 << " ";
cout << ")." << endl; system("pause");
}

ZC:控制台输出 - 2:

 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 = ( 640 360 160 40 0 40 160 ).
请按任意键继续. . .

?.?、第6讲 PPT.?

ZC: VC6 测试代码:

ZC:控制台输出:

X

STL_算法_06_遍历算法的更多相关文章

  1. 链表创建和链表遍历算法的演示_C语言

    今天搞了一个多小时,头是疼的,应该是没休息好吧,学习了数据结构这一节,感觉收益良多,下面贴上代码和心得: /*24_链表创建和链表遍历算法的演示*/ # include <stdio.h> ...

  2. Kasaraju算法--强连通图遍历及其python实现

    在理解有向图和强连通分量前必须理解与其对应的两个概念,连通图(无向图)和连通分量. 连通图的定义是:如果一个图中的任何一个节点可以到达其他节点,那么它就是连通的. 例如以下图形: 这是最简单的一个连通 ...

  3. 图的遍历算法:DFS、BFS

    在图的基本算法中,最初需要接触的就是图的遍历算法,根据访问节点的顺序,可分为深度优先搜索(DFS)和广度优先搜索(BFS). DFS(深度优先搜索)算法 Depth-First-Search 深度优先 ...

  4. STL_算法_01_查找算法

    1. 来自教程:第6讲 PPT.15 ◆ 常用的查找算法: 1.1.按条件查找N个相邻的元素 ( adjacent 是 邻近的意思) iterator = adjacent_find(iterator ...

  5. 树的遍历算法-只有一个变量T-递归和非递归

    void PostOrderTraverse(BTNode *T) { //就用到了一个变量T if(T==NULL) return; PostOrderTraverse(T->lchild); ...

  6. 经典算法 Morris遍历

    内容: 1.什么是morris遍历 2.morris遍历规则与过程 3.先序及中序 4.后序 5.morris遍历时间复杂度分析 1.什么是morris遍历 关于二叉树先序.中序.后序遍历的递归和非递 ...

  7. javascript数据结构与算法--二叉树遍历(后序)

    javascript数据结构与算法--二叉树遍历(后序) 后序遍历先访问叶子节点,从左子树到右子树,再到根节点. /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * * */ ...

  8. javascript数据结构与算法--二叉树遍历(先序)

    javascript数据结构与算法--二叉树遍历(先序) 先序遍历先访问根节点, 然后以同样方式访问左子树和右子树 代码如下: /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * ...

  9. javascript数据结构与算法--二叉树遍历(中序)

    javascript数据结构与算法--二叉树遍历(中序) 中序遍历按照节点上的键值,以升序访问BST上的所有节点 代码如下: /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * ...

随机推荐

  1. 使用POI读取/创建Execl(.xlsx)文件

    最近项目中用到了解析Execl表格的功能,在网上百度了一下自己写了一个小Demo.由于项目中使用的是Execl2007,就是后缀为.xlsx的,所以只研究了解析和创建Execl2007的文件,解析Ex ...

  2. 软件包管理:rpm包管理-yum在线管理-IP地址配置和网络yum源

    只需告诉系统你想安装那个包,剩下的所有依赖问题yum都会解决. 有些情况下不能上网,但可以使用光盘. centos的yum是免费的.redhatyum付费. yum管理的其实同样是rpm包.并没有yu ...

  3. photoshop打造超酷炫火焰人像效果

    效果图看上去非常的酷.制作方法跟火焰字过程差不多.唯一不同的是前期的处理,需要用滤镜把人物轮廓路径找出来,去色后再用制作火焰的过程制作.最后把最好的火焰叠加到人物上面,适当用蒙版控制区域即可.原图 最 ...

  4. Java8函数接口实现回调及Groovy闭包的代码示例

    本文适用于想要了解Java8 Function接口编程及闭包表达式的筒鞋. 概述 在实际开发中,常常遇到使用模板模式的场景: 主体流程是不变的,变的只是其中要调用的具体方法. 其特征是:   Begi ...

  5. python 星号*使用方法

    1.乘号 2.表示倍数 def T(msg,time=1): print((msg+',,')*time) >>>T('hi',3) hi,,hi,,hi 3.单个星号* --1-- ...

  6. String内存分析,for-each,参数传递

    上午总结: 蓝白书P245 (一)Iterator用法 import java.util.*; public class HashSetTest{ public static void main(St ...

  7. P4336 [SHOI2016]黑暗前的幻想乡

    P4336 [SHOI2016]黑暗前的幻想乡 矩阵树定理(高斯消元+乘法逆元)+容斥 ans=总方案数 -(公司1未参加方案数 ∪ 公司2未参加方案数 ∪ 公司3未参加方案数 ∪ ...... ∪ ...

  8. spring MVC @Resource不支持Lazy加载及解决方法

    今天迁一系统时发现有个bean使用@Resource注入了另外一个bean,这个被注入的bean是将被deprecated的类,而且只有一两个功能使用到,为了先调整进行测试,增加了@Lazy注解,启动 ...

  9. cmd解释

    cmd是command的缩写.即命令提示符(CMD),是在OS / 2 , Windows CE与Windows NT平台为基础的操作系统(包括Windows 2000和XP中, Vista中,和Se ...

  10. Python3基础 file with 配合文件操作

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...