for_each与for时间相差大吗
  • 板块学术版
  • 楼主2333?
  • 当前回复8
  • 已保存回复8
  • 发布时间2023/8/8 11:54
  • 上次更新2023/11/3 05:13:42
查看原帖
for_each与for时间相差大吗
467775
2333?楼主2023/8/8 11:54

rt,洛谷ide上跑的都是23ms,但是不太确定过来问问~~(如果相差不大那就可以偷懒了)~~ 代码如下:

for
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a[]={1,3,5,7,9};
	set<int> s(a,a+5);	
    for(auto it=s.begin();it!=s.end();it++)
		cout<<*it<<" "; 
	return 0;
}

for_each
#include <bits/stdc++.h>
using namespace std;
void print(int x)
{
    cout<<x<<" ";
}
int main()
{
	int a[]={1,3,5,7,9};
	set<int> s(a,a+5);	
    for_each(s.begin(),s.end(),print);
	return 0;
}

2023/8/8 11:54
加载中...