栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【C++标准库 第二版】11.3 for

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【C++标准库 第二版】11.3 for

0.引入

for_each()算法非常灵活,它允许你以不同的方式访问、处理、修改每一个元素. 然而请注意,自C++11起,range-based for循环提供了更方便更自然的行为. 因此,for_each()恐将日渐丧失其重要性.

链接:分析

1.函数原型

for_each的原型如下:

namespace std {
template 
Operation for_each (Iterator act, Iterator end, Operation op)
{
   while (act != end) // as long as not reached the end
   { 
     op(*act); // - call op() for actual element
     ++act; // - move iterator to the next element
   }
   return op;
  }
}


2.函数的一些说明
UnaryProc for_each (InputIterator beg, InputIterator end, UnaryProc op)
(1)对区间[beg,end)中的每一个元素调用:op(elem);
(2)返回 op(它已在算法内部被改动过)的一个拷贝(副本).Since C++11, the returned op is moved;
(3)op可以改动元素。然而,与transform()之间的比较,后者具有相同能力,但行事稍有不同;
(4)op的任何返回值都会被忽略;
(5)复杂度:线性。调用op()共numElems次.
3.不修改元素程序实例

 algostuff.hpp

// 本文件:foreach1.cpp
// 编译方法:g++ foreach1.cpp -std=c++11 -o foreach1

#include "algostuff.hpp"
using namespace std;

void print(int elem)
{ 
	cout << elem << ' ';
}

int main()
{
	vector coll;

	INSERT_ELEMENTS(coll,1,9);

	
        
    
	for_each (coll.cbegin(), coll.cend(), [](int elem){cout << elem << ' ';});
    cout << endl;
            
    
	for_each (coll.cbegin(), coll.cend(),print);
    cout << endl;
	
    
    for (auto elem:coll)
	{
		cout << elem << ' ';
	}
	cout << endl;
}
4.修改元素的实例

// g++ foreach2.cpp -std=c++11 -o foreach2 -fpermissive
// 本文件是foreach2.cpp
#include "algostuff.hpp"
using namespace std;
#define NONE "33[m"
#define YELLOW "33[1;33m"
#define DASH "*********************************"
int main()
{
    vector coll1;
    INSERT_ELEMENTS(coll1, 1, 9);
    
    for_each(coll1.begin(), coll1.end(), [](int &elem) { elem += 10; });
    PRINT_ELEMENTS(coll1);
    
    for_each(coll1.begin(), coll1.end(), [=](int &elem) { elem += *coll1.begin(); });
    PRINT_ELEMENTS(coll1);

    cout << YELLOW << DASH << NONE << endl;

    vector coll2;
    INSERT_ELEMENTS(coll2, 1, 9);
    
    for_each(coll2.begin(), coll2.end(), [](int &elem) { elem += 10; });
    PRINT_ELEMENTS(coll2);
    
    for_each(coll2.begin(), coll2.end(), [&](int &elem) { elem += *coll2.begin(); });
    PRINT_ELEMENTS(coll2);
}
5.利用for_each()的返回值


#include "algostuff.hpp"
using namespace std;

// function object to process the mean value
class MeanValue {
  private:
    long num;    // number of elements
    long sum;    // sum of all element values
  public:
    // constructor
    MeanValue () : num(0), sum(0) {
    }

    // function call
    // - process one more element of the sequence
    void operator() (int elem) {
        num++;          // increment count
        sum += elem;    // add value
    }

    // return mean value (implicit type conversion)
    operator double() {
        return static_cast(sum) / static_cast(num);
    }
};

int main()
{
    vector coll;

    INSERT_ELEMENTS(coll,1,8);
    // process and print mean value
    double mv = for_each (coll.begin(), coll.end(),  // range
                          MeanValue());              // operation
    cout << "mean value: " << mv << endl;
}

C++中的operator关键字的两个作用: (1)操作符重载;(2)隐式转换

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847072.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号