20157:补足程序7

本文介绍如何使用C++ STL中的equal_range函数来查找已排序数组中特定元素的第一个和最后一个出现的位置。通过一个示例程序展示了如何针对排序后的整数数组找到指定数值的下界和上界,并正确输出这些位置对应的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

20157:补足程序7

描述

补足下面的程序,使得其输出结果是:

18,20

#include <iostream>

#include <algorithm>

#include <string>

#include <queue>

#include <set>

#include <map>

#include <vector>

using namespace std;

int main()

{

 

        inta[10] = {1, 3, 2, 10,7, 18,20,31,47,59};

        sort(a,a+10);

       

        pair<int*,int*>p;

       

        p=

// 在此处补充你的代码

(a,a+10,18);

       

        cout<< * (p.first) << "," << * (p.second) <<endl;

        return0;

}

输入

输出

18,20

样例输入

样例输出

18,20

提示

只填写一个函数名。

解题思路:由题可知,输出有两个数,分别为lower_bound和upper_bound的值,而在这需要只填写一个函数就能同时得到应用这两个函数得到的结果,则只能用equal_range。

即:

#include <iostream>

#include <algorithm>

#include <string>

#include <queue>

#include <set>

#include <map>

#include <vector>

using namespace std;

int main()

{

 

         inta[10] = { 1, 3, 2, 10, 7, 18, 20, 31, 47, 59 };

         sort(a,a + 10);//从小到大排序

         pair<int*,int*> p;

 

         p=

                   //在此处补充你的代码

                   equal_range(a,a + 10, 18);

         cout<< *(p.first) << "," << *(p.second) << endl;

         return0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值