C++ STL中的常用遍历算法分享

目录
  • 1.for_each
    • 功能描述
    • 函数原型
  • 2.transform
    • 功能描述
    • 函数原型

1.for_each

功能描述

实现容器遍历

函数原型

for_each(itertor beg,iterator end,_func);
//遍历算法 遍历容器元素
//beg 开始迭代器
//end 结束迭代器
//_func函数或者函数对象

代码

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

//普通函数
void print01 (int val)
{
	cout<< val << " ";
} 

//放寒暑
class print02
{
public:
	void operator()(int val)
	{
		cout<< val << " ";
	}
};
void test01()
{
   vector<int>v;
   for(int i = 0;i < 10;i ++)
   {
   	  v.push_back(i);
   }
   for_each(v.begin(),v.end(),print01);
   cout<<endl;
   for_each(v.begin(),v.end(),print02());
   cout<<endl; 

}
int main()
{
	test01();
} 

运行结果

2.transform

功能描述

搬运容器到另一个容器

函数原型

transform (iterator beg1,iterator endl, iterator beg2,_func);
//beg1 源容器开始迭代器
//end1 源容器结束迭代器
//beg2 目标函数开始迭代器
//_func 函数或者函数对象

代码

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

class Transform
{
public:
    int operator()(int v)
	{
		return  v;
	}
};
class MyPrint{
	public:
		void operator()(int val)
		{
			cout<< val <<" ";
		}
};
void test01()
{
	vector<int>v;
	for(int i = 0;i < 10;i ++)
    {
   	  v.push_back(i);
    }
    vector<int>vTarget;   //目标函数
    vTarget.resize(v.size());   //目标容器要提前开辟空间
	transform(v.begin(), v.end(), vTarget.begin(),Transform());

	for_each(vTarget.begin(),vTarget.end(), MyPrint());
	cout<<endl; 

}
int main()
{
	test01();
}

运行结果

到此这篇关于C++ STL中的常用遍历算法分享的文章就介绍到这了,更多相关C++ STL遍历算法内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C++ STL list 遍历删除出错解决方案

    C++ STL list 遍历删除崩溃 错误用法一 下面这种用法会在for的地方崩溃,分析 第一次for循环的时候 it=0,当t.erase(it)执行完成之后 it就变成了 -17891602 表明it不能再作为迭代器进行运算,自然会报错. #include <map> #include <list> using namespace std; typedef std::list<int > TESTLIST; int _tmain(int argc, _TCHAR*

  • c++ STL常用遍历算法

    需要引入头文件#include<algorithm> 1.for_each #include<iostream> using namespace std; #include <vector> #include <algorithm> class MyPrint { public: void operator()(int val) const{ cout << val << " "; } }; void printV

  • C++ STL中的常用遍历算法分享

    目录 1.for_each 功能描述 函数原型 2.transform 功能描述 函数原型 1.for_each 功能描述 实现容器遍历 函数原型 for_each(itertor beg,iterator end,_func);//遍历算法 遍历容器元素//beg 开始迭代器//end 结束迭代器//_func函数或者函数对象 代码 #include <iostream> using namespace std; #include <vector> #include <al

  • 深入解析C++ STL中的常用容器

    STL是C/C++开发中一个非常重要的模板,而其中定义的各种容器也是非常方便我们大家使用.下面,我们就浅谈某些常用的容器.这里我们不涉及容器的基本操作之类,只是要讨论一下各个容器其各自的特点.STL中的常用容器包括:顺序性容器(vector.deque.list).关联容器(map.set).容器适配器(queue.stac). 1.顺序性容器 (1)vectorvector是一种动态数组,在内存中具有连续的存储空间,支持快速随机访问.由于具有连续的存储空间,所以在插入和删除操作方面,效率比较慢

  • 浅析STL中的常用算法

    一.非变异算法 是一组不破坏操作数据的模板函数,用来对序列数据进行逐个处理.元素查找.子序列搜索.统计和匹配.非变异算法具有极为广泛的适用性,基本上可应用与各种容器. 1查找容器元素find 它用于查找等于某值的元素.它在迭代器区间[first,last)(闭开区间)上查找等于value值的元素,如果迭代器i所指的元素满足*i=value,则返回迭代器i:未找到满足条件的元素,返回last.函数原型:find( v1.begin(), v1.end(), num_to_find ); 复制代码

  • C++ STL中五个常用算法使用教程及实例讲解

    目录 前言 sort()排序 常用遍历算法for_each() 常用遍历算法 搬运transform() 查找算法find 删除操作erase() 实例应用 前言 在C++中使用STL算法都要包含一个算法头文件 #include<algorithm> 这样我们才能使用这个STL算法函数 sort()排序 Sort函数包含在头文件为#include<algorithm>的c++标准库中,是一个专门用来排序的高效的函数,我们在解决问题时可以方便快捷的排列顺序. sort()函数中有三个

  • Python二叉树的定义及常用遍历算法分析

    本文实例讲述了Python二叉树的定义及常用遍历算法.分享给大家供大家参考,具体如下: 说起二叉树的遍历,大学里讲的是递归算法,大多数人首先想到也是递归算法.但作为一个有理想有追求的程序员.也应该学学非递归算法实现二叉树遍历.二叉树的非递归算法需要用到辅助栈,算法着实巧妙,令人脑洞大开. 以下直入主题: 定义一颗二叉树,请看官自行想象其形状, class BinNode( ): def __init__( self, val ): self.lchild = None self.rchild =

  • python中list常用操作实例详解

    本文实例讲述了python中list常用操作.分享给大家供大家参考.具体分析如下: 1.定义list >>> li = ["a", "b", "mpilgrim", "z", "example"] >>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[0] 'a' >>> li[4]

  • C#中DataGridView常用操作实例小结

    本文实例讲述了C#中DataGridView常用操作.分享给大家供大家参考.具体如下: public void Binder1() { DataTable tableType = DataBase.SQLDBHelper.GetDataTable("select top 200 unit_code,unit_name from unit "); DataTable table = DataBase.SQLDBHelper.GetDataTable("select top 2

  • C/C++ 常用排序算法整理汇总分享

    (伪)冒泡排序算法: 相邻的两个元素之间,如果反序则交换数值,直到没有反序的记录为止. #include <stdio.h> void BubbleSort(int Array[], int ArraySize) { int x, y, temporary; for (x = 0; x < ArraySize - 1; x++) { for (y = x + 1; y < ArraySize; y++) { if (Array[x] > Array[y]) { tempora

  • C++ STL中常见的算法使用方式

    目录 什么是STL? 0. < algorithm> 是什么: 1. Non-modifying sequence operations: 1.1 find:(Find value in range) 1.2 count:(Count appearances of value in range) 1.3 equal:(Test whether the elements in two ranges are equal) 1.4 search:(Search range for subsequen

随机推荐