纯小白蓝桥杯备赛笔记--DAY4(竞赛常用库函数)

  1. 大小写转换
  • islower和isupper:检查一个字符是否是小写或者大写。
    • islower:检查小写。
    • isupper:检查大写。
    • 函数的返回值为bool类型。
    • 头文件:或者是<bits/stdc++.h>
  • Tolower和toupper函数:
    • tolower(char ch)将ch转换为小写字母,大写不转换。
    • toupper同理。
  • ASCII码:
    • 大写字母的范围:65~90
    • 小写字母的范围:97~122
      大写转小写:
      ch1=ch+32;//不推荐
      ch1=ch-‘A’+‘a’;//减去大写的那一列,从小写的那一列开始
      实现:小写转大写
#include<bits/stdc++.h>
using namespace std;
char coverch(char ch)
{
        if(islower(ch))ch=toupper(ch);
        else if(isupper(ch))ch=tolower(ch);
        return ch;
}
int main()
{
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        string s;
        getline(cin,s);
        for(auto &i:s)
        {
                i=coverch(i);
        }
        cout<<s<<"\n";        
}
  1. 二分查找
  • 二分查找的前提:库函数只能对数组进行二分查找,且数组中的元素是单调的。
    • 一般为单调不减,当然如果是单调不增也可以(需要修改比较函数sort)
  • binary_search函数:
    • 用法:已排序的序列中查找特定元素。
    • 通过二分查找算法来确定列中是否存在目标元素。
    • 函数返回值是bool类型。
    • 获取找到元素的位置使用lower_bound函数或upper_bound函数。
#include<bits/stdc++.h>
int main()
{
        using namespace std;
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        vector<int>numbers={1,3,5,7,9};
        int target=5;
        bool found=binary_search(numbers.begin(),numbers.end(),target);
        if(found)
        cout<<target<<" "<<"is found"<<endl;
        else
        cout<<target<<"is not found"<<endl;
        return 0;
 } 
  • lower_bound和upper_bound函数
    • lower_bound(start,end,target)返回地址[start,end)中第一个大于等于x的元素的地址。(注:下标=地址-首地址)
    • upper_bound(start,end,target)返回地址[start,end)中第一个大于 x的元素的地址。(注意是左闭右开的区间)
    • 如果不存在则返回最后一个元素的下一个位置,在vector中即end()。
    • 实现
#include<bits/stdc++.h>
using namespace std;
int main()
{
        vector<int> s={5,1,7,3,10,18,9};
        sort(s.begin(),s.end());
        for(auto &i:s)
        cout<<i<<' ';
        cout<<"\n";
        //找到数组中第一个大于等于8的元素的地址
        cout<<(lower_bound(s.begin(),s.end(),8)-s.begin())<<"\n";
        return 0; 
}
  • 例题:蓝桥1389二分查找数组
#include<bits/stdc++.h>
using namespace std;
int main()
{
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        int data[200];
        for(int i = 0 ; i < 200 ; i ++)
        data[i] = 4 * i + 6;
        int target;
        cin>>target;
        cout<<lower_bound(data,data+200,target)-data<<"\n";
        return 0;
}

注:不是容器(例如:vector和list)对象的不能用begin()函数。修改如下:

#include<bits/stdc++.h>
using namespace std;
int main()
{
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        vector<int>data(200);
        for(int i = 0 ; i < 200 ; i ++)
        data[i] = 4 * i + 6;
        int target;
        cin>>target;
        cout<<lower_bound(data.begin(),data.end(),target)-data.begin()<<"\n";
        return 0;
}
  1. 最值查找
  • Min和max函数:
    • 时间复杂度为O(1),传入参数为数组(用{})的时候时间复杂度为O(n),n为数组大小。
  • min_element和max_element函数:
    • min_element(start,end)返回地址[start,end)中的最小的那个值的地址(迭代器),传入参数为两个地址或迭代器。
    • Max_element(start,end)返回地址[start,end)中的最大的那个值的地址(迭代器),传入参数为两个地址或迭代器。
    • 时间复杂度为O(n),n为数组大小
    • 返回值是具体的数字,而不是一个地址
#include<bits/stdc++.h>
using namespace std;
int main()
{
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        vector<int>v={5,1,3,9,11};
        cout<<*min_element(v.begin(),v.end())<<"\n";
        return 0;
}

注:这段代码中的星号(*)表示解引用,即通过地址(迭代器)得到值。

  • nth_element()函数
    • 格式:nth_element(start,k,end)
    • 用途,找出第k大个元素,则该元素的位置属于正确位置,其他元素的位置虽然是任意的,但是前面的都比它小,后面的都比它大。
    • 返回值为void()
    • 时间复杂度为O(n)
#include<bits/stdc++.h>
using namespace std;
int main()
{
        vector<int>v={5,1,7,3,10,18,9};
        nth_element(v.begin(),v.begin()+3,v.end());//返回值为空,说明这条语句不能输出
        for(auto &i:v)
        cout<<i<<"\n";
        return 0; 
}
  • 例题:497求最高分,最低分和平均分。(两种方法)
#include<bits/stdc++.h>
using namespace std;
using ll=long long;//数组记得开long long
const int N=10001;
int a[N];
int main()
{
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        int n;cin>>n;
        ll sum=0;
        for(int i=1;i<=n;i++)
        {
        cin>>a[i];
                sum+=a[i];        
                }
        cout<<*max_element(a+1,a+n+1)<<"\n";//定义的是数组时使用数组名,定义的是容器时使用begin函数 
                cout<<*min_element(a+1,a+n+1)<<"\n";
                cout<<fixed<<setprecision(2)<<1.0*sum/n<<"\n";
                return 0;          
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        int n;
        int s[10001];
        int maxa=0;int mina=100;double sum=0;
        double c=0;
        cin>>n;
        for(int i=0;i<n;i++)
        {
                cin>>s[i];
                maxa=max(maxa,s[i]);
                mina=min(mina,s[i]);
                sum+=s[i];
         } 
         c=sum/double(n);
         cout<<maxa<<endl;
         cout<<mina<<endl;
         printf("%.2lf",c);
         return 0;
}

注:c++如何保留小数:

#include <iostream>
#include <iomanip>

int main() {
    double num = 3.14159;
    std::cout << std::fixed << std::setprecision(2) << num << std::endl;
    return 0;
}

使用std::fixed和std::setprecision(2)设置输出保留两位小数。
4. 排序

  • sort简介:
    • sort函数包含在头文件中。
    • 在使用前需要#include或者使用万能头文件。
    • 用途:指定范围内的元素进行排序。
    • sort算法使用的是快速排序(QuickSort)或者类似快速排序的改进算法,时间复杂度为O(nlogn)。
    • 用法:sort(起始地址,结束地址的下一位,*比较函数)
 #include<bits/stdc++.h>
using namespace std;
int main()
{
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        int a[100];
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        cin>>a[i];
        //对数组进行排序
        sort(a+1,a+n+1);
        //输出
        for(int i=1;i<=n;i++)cout<<a[i]<<' ';
        return 0; 
 } 
#include<bits/stdc++.h>
using namespace std;
int main()
{
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        vector<int>a={5,1,3,9,11};
        //对数组进行排序
        sort(a.begin(),a.end());
        //输出
        for(auto i:a)cout<<i<<' ';
        return 0; 
 } 
  • 自定义比较函数:在sort函数末尾传入第三个表达式,这个表达式可以是函数也可以是lambda表达式。
    • 函数:
#include<bits/stdc++.h>
using namespace std;
bool cmp(const int &u,const int &v)
{
        return u>v;
}
int main()
{
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        vector<int>a={5,1,3,9,11};
        //对数组进行排序
        sort(a.begin(),a.end(),cmp);
        //输出
        for(int i=0;i<a.size();i++)cout<<a[i]<<' ';
        return 0; 
 } 
  • lambda表达式:(匿名函数,一般用在只在程序中用一下,其他地方用不到的情况)
#include<bits/stdc++.h>
using namespace std;
int main()
{
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        vector<int>a={5,1,3,9,11};
        //对数组进行排序
        sort(a.begin(),a.end(),[](const int &u,const int &v)//方括号内也可以放&表示引用 
        {
                return u>v;
        });
        //输出
        for(int i=0;i<a.size();i++)cout<<a[i]<<' ';
        return 0; 
 } 
  • 也可以用在结构体中,其中函数的参数只有一个。
 struct Node
 {
         int u,v;
         bool operator < (const Node &m)const//这里可以把perator < 整体看做一个函数名 
         {
                 return u==m.u?v<m.v:u<m.u;//以u为第一关键字,v为第二关键字 
         }
  } 
  • 例题:1265:对一个数组分别实现由小到大和由大到小输出
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+3;
int a[N];
int main()
{
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  int n;
  cin>>n;
  for(int i=1;i<=n;++i)cin>>a[i];
  sort(a+1,a+n+1);
  for(int i=1;i<=n;++i)cout<<a[i]<<" \n"[i==n];//到第n个的时候输出一个换行
  for(int i=n;i>=1;--i)cout<<a[i]<<" \n"[i==1];//当i等于1的时候跟上一个回车
  return 0;
}

注:12,13行后面的字符判断"\n"之前有一个空格一定要记得打上。

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+3;
int a[N];
int main()
{
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  int n;
  cin>>n;
  for(int i=1;i<=n;++i)cin>>a[i];
  sort(a+1,a+n+1);
  for(int i=1;i<=n;++i)cout<<a[i]<<" \n"[i==n];//到第n个的时候输出一个换行
  //当i等于1的时候跟上一个回车
  sort(a+1,a+n+1,[](const int &u,const int &v)
  {
          return u>v;
  });
  for(int i=1;i<=n;++i)cout<<a[i]<<" \n"[i==n];
  return 0;
}
、、2#include<bits/stdc++.h>
using namespace std;
const int N=5e5+3;
int a[N];
bool cmp(const int &u,const int &v)
{
        return u>v;
}
int main()
{
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  int n;
  cin>>n;
  for(int i=1;i<=n;++i)cin>>a[i];
  sort(a+1,a+n+1);
  for(int i=1;i<=n;++i)cout<<a[i]<<" \n"[i==n];//到第n个的时候输出一个换行
  //当i等于1的时候跟上一个回车
  sort(a+1,a+n+1,cmp);
  for(int i=1;i<=n;++i)cout<<a[i]<<" \n"[i==n];
  return 0;
}
  1. 全排列
  • next_permutation函数
    • 该函数用于生成当前序列的下一个排列。它按照字典序对序列进行重新排序,如果存在下一个序列,则将当前序列更改为下一个排列,并返回true,如果当前序列已经是最后一个排列,则将序列更改为第一个排列,并返回false。
    • 如果想要输出全部的排列就要求定义的数组是最小的。
#include<bits/stdc++.h>
using namespace std;
int main()
{
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  vector<int>nums={1,2,3}; 
  cout<<"初始的数组:";
  for(int num:nums)
  {
          cout<<num<<" ";
  }
  cout<<endl;
  //生成下一个排列
  while(next_permutation(nums.begin(),nums.end())) 
  {
          cout<<"下一个排列是:";
          for(int num:nums)
          {
                  cout<<num<<" ";
           } 
           cout<<endl;
  }
  return 0;
}
  • prev_permutation()函数:
    • 生成当前序列的上一个排列。同样,要想生成全排列要求第一个是最大的数组。
#include<bits/stdc++.h>
using namespace std;
int main()
{
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  vector<int>nums={3,2,1}; 
  cout<<"初始的数组:";
  for(int num:nums)
  {
          cout<<num<<" ";
  }
  cout<<endl;
  //生成下一个排列
  while(prev_permutation(nums.begin(),nums.end())) 
  {
          cout<<"下一个排列是:";
          for(int num:nums)
          {
                  cout<<num<<" ";
           } 
           cout<<endl;
  }
  return 0;
}

补充:全排列一般使用搜索(dfs)
应用:

#include<bits/stdc++.h>
using namespace std;
int a[10];
int main()
{
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  for(int i=1;i<=4;++i)
  a[i]=i;
  bool tag=true;
  while(tag)
  {
          for(int i=1;i<=4;++i)cout<<a[i]<<' ';
          cout<<'\n';
          tag=next_permutation(a+1,a+1+4);
  }
  return 0;
}

注:next_permutation 函数用于生成给定范围内的下一个排列。如果成功生成了下一个排列,则返回 true,并将数组 a 更新为新的排列;否则,如果没有更多的排列可用,则返回 false。由此tag实现对循环的控制。
6. 其他库函数

  • memset()函数:
    • 用途:设置内存块值。
    • 原型定义在头文件中。
    • 函数声明:void* memset(void* ptr,int value,size_t num);分别是:指针、值、重置的大小。
    • 详解:
      • ptr:指向要设置值的内存块的地址(数组一般是数组名)
      • value:要设置的值,通常是一个整数。
      • num:要设置的字节数。
      • 就是说,将ptr指向的内存块的前num个字节设置为value的值。它返回一个指向ptr的指针。
      • memset()通常用于初始化内存块,将其设置为特定的值。
    • 注:memset()函数对于非字符类型的数组可能会产生未定义行为(每一个字节赋值为1)
#include<bits/stdc++.h>
using namespace std;
int a[10];
int main()
{
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  int a[5];
  memset(a,1,sizeof(a));//设置成0和-1没有影响
  //-1的补码是全1的。
  for(int i=0;i<5;++i)cout<<bitset<32>(a[i])<<'\n';//产看a[i]的二进制 
  return 0;
}
  • swap()函数:
    • 用法:swap(T &a,T &b);函数接受两个参数进行交换。
  • reverse()函数:
    • 用于反转容器中元素顺序的函数。
    • 原型定义在头文件中,函数的声明如下:
    • reverse(first,last)函数接受两个参数:将[first,last)范围内的元素顺序进行反转。
    • reverse可翻转各种类型的容器,如数组、向量、链表等。
#include<bits/stdc++.h>
using namespace std;
int main()
{
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  vector<int>vec={1,2,3,4,5};
  reverse(vec.begin(),vec.end()) ;
  for(int num:vec)
  cout<<num<<" ";
  cout<<endl;
  return 0;
}
  • unique()函数
    • unique(first,last)将[first,last)范围内的相邻重复元素去除,并返回一个指向去重后范围的尾后迭代器。去重后的范围中只保留第一个出现的元素,后续重复的元素都被移除。
    • 时间复杂度为O(n),但是使用之前一般要排序,此时时间复杂度为sort(nlogn)。
#include<bits/stdc++.h>
using namespace std;
int main()
{
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  vector<int>vec={1,1,2,2,3,3,4,4,5,5};
  auto it=unique(vec.begin(),vec.end());//unique 函数会将相邻的重复元素移动到向量的末尾,并返回一个指向去重后最后一个元素的下一个位置的迭代器。
  vec.erase(it,vec.end());//vec.erase(it,vec.end()); 这行代码的作用是删除从迭代器 it 开始到向量 vec 末尾的所有元素。
  for(int num:vec)
  {
          cout<<num<<" ";
  }
  cout<<endl;
  return 0;
}
//定义数组的形式:
#include<bits/stdc++.h>
using namespace std;
int main()
{
  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  int vec[]={1,1,2,2,3,3,4,4,5,5};
  int n=unique(vec,vec+10)-vec;//减去a最后得到的是下标
  for(int i=0;i<n;i++)
  cout<<vec[i]<<' '; 
  return 0;
}
  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值