在vector 中 找返回序列中第一个大于等于q的下标
#include <bits/stdc++.h> using namespace std; static bool cmp(const pair<int,int> &p1,const pair<int,int> &p2){ return p1.first < p2.first; } int main() { int n = 7; int a[] = {1,2,3,3,2,1,2}; vector<pair<int, int>>nums(n, { 0, 0 }); for (int i = 0; i < n; i++) { nums[i].first = a[i]; nums[i] = {nums[i].first, i}; } sort(nums.begin(),nums.end(),cmp); vector<pair<int, int>>::iterator it = nums.begin(); for(; it != nums.end(); it++) cout<<it->first<<" - "<<it->second<<endl; int q = 2; cout<< lower_bound(nums.begin(), nums.end(), make_pair(q, n-1))->second<< endl; cout<< upper_bound(nums.begin(), nums.end(), make_pair(q, n-1))->second<< endl; return 0; }