int searchInsert(vector<int>& nums, int target) {
/*从小到大排序,进行二分查找*/
int head = 0, tail = nums.size() - 1;
while (tail >= head) {
if (target < nums[(tail + head) / 2]) tail = (tail + head) / 2 - 1;
else if (target > nums[(tail + head) / 2]) head = (tail + head) / 2 + 1;
else return (tail + head) / 2;
}
return -1;
}
c++进行二分查找代码