题解 | #查找#
查找
https://www.nowcoder.com/practice/8e0c0e8c78944847be9bca54b59d713f
#include<bits/stdc++.h> using namespace std; int main() { set<int> s; //write your code here...... int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { int num; cin >> num; s.insert(num); } set<int>::iterator it; for (int i = 0; i < m; ++i) { int x; cin >> x; it = s.upper_bound(x); if (it != s.end()) { cout << *it << endl; } else { cout << -1 << endl; } } return 0; }
使用upper_bound()
upper_bound(data)用于寻找大于data,返回iterator