看书的时候看到了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;
}
所以有哪位大佬可以告诉我这俩代码的时间复杂度一样吗?空间复杂度一样吗?哪个在哪种条件下使用更好?