17 排在第几个
在 13、15、17、23、24 里从头找 17。运行下面这段程序:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int lfind(const vector<int>& a, int target) {
// 线性查找:从头逐个比,找不到返回 -1
for (int i = 0; i < (int)a.size(); i++) {
if (a[i] == target) return i;
}
return -1;
}
int main() {
vector<int> A = {13, 15, 17, 23, 24};
cout << lfind(A, 17) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)