【违规紫衫】关于迭代器
  • 板块学术版
  • 楼主V_Me_50
  • 当前回复7
  • 已保存回复7
  • 发布时间2023/9/7 22:40
  • 上次更新2023/11/2 22:23:30
查看原帖
【违规紫衫】关于迭代器
236660
V_Me_50楼主2023/9/7 22:40

看书的时候看到了lower_bound这个迭代器,试了试很慢,我怀疑是电脑不行,问问下面这两个代码哪个速度快?或者更好用?

迭代器

//
#define debug
#include <bits/stdc++.h>

using namespace std;

int main(){
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  vector<int> a = {1, 1, 1, 1, 2, 3};
  vector<int>::iterator sweetb;
  sweetb = lower_bound(a.begin(), a.end(), 2);
  cout << sweetb - a.begin();
	return 0;
}


下面是没使用迭代器的代码

//
#define debug
#include <bits/stdc++.h>

using namespace std;

int main(){
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int op, a[114] = {1, 1, 1, 1, 2, 3};
  for(int i = 0; i < 6; i++){
  	if(a[i] == 2){
  		op = i;
		}
	}
	cout << op << endl;
	return 0;
}

所以有哪位大佬可以告诉我这俩代码的时间复杂度一样吗?空间复杂度一样吗?哪个在哪种条件下使用更好?

2023/9/7 22:40
加载中...