各类函数的作用于实现

本文深入探讨了C++中的tolower函数如何将字符转换为小写,并通过实例展示了其应用。同时,文章介绍了max_element和min_element函数在数组中的用法,以及lower_bound和upper_bound在STL集合中的作用。通过具体代码示例,读者可以掌握这些函数的正确使用方法。

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

tolower函数

功 能: 把字符转换成小写字母,非字母字符不做出处理

#include<cstdio>
#include<cmath>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<list>

using namespace std;

int main()
{
    string s = "THIS IS A STRING";
    for(int i=0; i<s.size(); i++)
       s[i] = tolower( s[i] );//tolower函数将字母字符的大写转换为小写,其他字符不变化
    cout<<s<<endl;
    return 0;
}


max_element于min_element:

max_element :返回一个 iterator ,指出序列中最大的元素。重载版本使用自定义的比较操作

min_element :类似与 max_element ,不过返回最小的元素。




#include<cstdio>
#include<cmath>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<list>

using namespace std;

int main()
{
    int a[10];
    for(int i=0; i<5; i++)scanf("%d", &a[i]);
    int b = max_element( a, a+5 ) - a;
    int c = min_element( a, a+5 ) - a;
    printf("%d\n%d\n", a[b], a[c]);
    return 0;
}



数组中的 lower_bound 于upper_bound:

lower_bound :返回第一个大于或等于key的iterator 

upper_bound:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于key的第一个元素位置
如果key大于所有的数则返回last;


这里特别注意的一点是在STL的set的中:

lower_bound返回的是第一个大于等于key的定位器;

upper_bound返回的是大于等于key的最后一个定位器


数组中:


#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<sstream>
#include<stdlib.h>
#include<algorithm>

using namespace std;

int main()
{
    int v[10];
    memset(v, 0, sizeof(v));
    v[0] = 1;
    v[1] = 2;
    v[2] = 2;
    v[3] = 2;
    v[4] = 3;
    int a = lower_bound( v, v+5, 2 ) - v;
    printf("%d\n", a);
    int b = upper_bound( v, v+5, 1 ) - v;
    printf("%d\n", b);
    return 0;
}

set中的:


int main()  
{  
    set<int> s;  
    s.insert(1);  
    s.insert(3);  
    s.insert(4);  
    cout<<*s.lower_bound(2)<<endl;  
    cout<<*s.lower_bound(3)<<endl;  
    cout<<*s.upper_bound(3)<<endl;  
    return 0;  
}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值