1. (20 scores)#
点击查看考点
模板, 排序, 二分查找
Write two function template:
A function template named
sort
that sorts an array in non-descending order.A function template named
search
that binary searches the non-descending array.
In the main
function, define a 10-element integer and string array, randomly generate 10 integer number (in the range of \([0, 100]\)) and read in 10 strings, call function sort
, and then arbitrarily read in a number and a string, call function template search
, output search results.
翻译
编写两个函数模板
函数模板
sort
将数组排序为非降序.函数模板
search
对非降序数组进行二分查找.
在 main
函数中, 定义一个长度为 10 的整数数组和字符串数组, 随机地生成 10 个整数 (\([0, 100]\)) 和读入 10 个字符串, 调用 sort
函数, 然后任意读入一个整数和一个字符串, 调用 search
函数, 输出查找结果.
点击查看解答参考
跟 21 年第 1 题有些类似的地方.
此外, 虽然不能直接用对应的 STL 算法, 但二分查找本身可以用寻找划分点这个 STL 算法实现, 因为所谓二分查找即找到有序数组中的一个元素, 之前的元素都小于它, 之后的元素都不小于它. 即 [在线代码 7MaTzMocn].