std::for_each_C ++中的std :: for_each()

std::for_each

std :: for_each() (std::for_each())

for_each() is a very useful function which helps to invoke a function fn() on each element in the STL container. This helps actually to write code in short and to reduce size of our codebase.

for_each()是一个非常有用的函数,它有助于在STL容器中的每个元素上调用函数fn() 。 这实际上有助于简短地编写代码并减小代码库的大小。

Below is the syntax for the for_each(),

以下是for_each()的语法,

Syntax:

句法:

for_each(
    InputIterator first, 
    InputIterator last, 
    Function fn);

Where,

哪里,

  • InputIterator first = starting of the container

    InputIterator first =容器的启动

  • InputIterator last = end of the container

    InputIterator last =容器的末尾

  • Function fn = function to be invoked on each element of container

    函数fn =在容器的每个元素上调用的函数

Below are examples of using for_each() efficiently.

以下是有效使用for_each()的示例。

1) Printing all elements

1)打印所有元素

For each can be used to print elements in any container. Below is the example where we have printed a vector and map to understand the usage.

对于每个可用于在任何容器中打印元素。 下面是我们打印矢量和映射以了解用法的示例。

#include <bits/stdc++.h>
using namespace std;

void myfunc1(int i)
{
    cout << i << " ";
}

void myfunc2(pair<char, int> p)
{ //ecah element of a map is pair
    cout << p.first << "->" << p.second << endl;
}

int main()
{
    vector<int> arr{ 3, 4, 2, 6, 5, 1 };
 
    map<char, int> mymap;
 
    mymap['a'] = 3;
    mymap['c'] = 3;
    mymap['b'] = 6;
    mymap['d'] = 4;
    mymap['e'] = 2;
    mymap['f'] = 1;

    cout << "Printing the vector\n";
    for_each(arr.begin(), arr.end(), myfunc1);
    cout << "\n";
 
    cout << "Printing the map\n";
    for_each(mymap.begin(), mymap.end(), myfunc2);
    cout << "\n";
    
    return 0;
}

Output:

输出:

Printing the vector
3 4 2 6 5 1
Printing the map
a->3
b->6
c->3
d->4
e->2
f->1

2) Replace the vowels with '*'

2)用“ *”代替元音

In this example, we will see how to update the elements of the container. We can update by passing a reference to the elements and updates within the function. In an early article, we saw how to replace vowels using find_first_of(). In the below program we have replaced all the vowels using for_each() which is more efficient and takes only O(n).

在此示例中,我们将看到如何更新容器的元素。 我们可以通过传递对元素的引用和函数内的更新来进行更新。 在早期的文章中,我们看到了如何使用find_first_of()替换元音。 在下面的程序中,我们使用了for_each()替换了所有的元音,这更加有效并且仅占用O(n)

#include <bits/stdc++.h>
using namespace std;

void myfunc(char& c)
{ //to update reference is passed

    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'o')
        c = '*';
}

int main()
{
    string str = "includehelp";
    
    cout << "Initially string is: " << str << endl;
    for_each(str.begin(), str.end(), myfunc);
    cout << "After updation\n";
    cout << str << endl;
    
    return 0;
}

Output:

输出:

Initially string is: includehelp
After updation
*nclud*h*lp

So, the advantage of using for_each() is it reduces the codebase size and makes the code product level. Also, the time complexity is O(n). If there is any case, where each and every element undergoes huge processing with a large codebase you can use for_each().

因此,使用for_each()的优势在于它减少了代码库的大小并提高了代码产品级别。 同样,时间复杂度为O(n) 。 如果有任何情况,每个元素都需要使用大型代码库进行大量处理,则可以使用for_each()

翻译自: https://www.includehelp.com/stl/std-for_each-in-cpp.aspx

std::for_each

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值