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

文章目录

    • 大小写转换
      • islower和isupper:检查一个字符是否是小写或者大写。
      • Tolower和toupper函数:
      • ASCII码:
    • 二分查找
      • 二分查找的前提:库函数只能对数组进行二分查找,且数组中的元素是单调的。
      • binary_search函数:
      • lower_bound和upper_bound函数
    • 最值查找
      • Min和max函数:
      • min_element和max_element函数:
    • 排序
      • sort简介:
      • 自定义比较函数:
    • 全排列
      • next_permutation函数
      • prev_permutation()函数:
    • 其他库函数
      • memset()函数:
      • swap()函数:
      • reverse()函数:
      • unique()函数

大小写转换

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";        
}

二分查找

二分查找的前提:库函数只能对数组进行二分查找,且数组中的元素是单调的。

  • 一般为单调不减,当然如果是单调不增也可以(需要修改比较函数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;elsecout<<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;
}

最值查找

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)设置输出保留两位小数。

排序

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;
}
  • 第二种方法
#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;
}

全排列

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实现对循环的控制。

其他库函数

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;
}

未完待续,大家一起加油啊!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/783455.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C++第五课-C/C++内存管理】C/C++的内存分布、new/delete、new和delete的实现原理

目录 C/C的内存分布new/deletenew内置类型使用new自定义类型使用newnew失败 delete内置类型使用delete自定义类型使用delete new和delete的实现原理new[] 和delete[]的补充知识 定位new&#xff08;了解&#xff09;常见面试题 C/C的内存分布 频繁的new/delete堆容易产生内存碎…

【数据处理包Pandas】DataFrame数据的基本操作

目录 一、DataFrame数据的查询&#xff08;一&#xff09;查询单行数据&#xff08;二&#xff09;查询多行数据&#xff08;三&#xff09;查询列数据&#xff08;四&#xff09;查询指定的行列数据 二、DataFrame数据的编辑&#xff08;一&#xff09;增加数据&#xff08;二…

拦截器未生效的问题

记录一下自己出现的一个问题 配置好拦截器后 protected void addInterceptors(InterceptorRegistry registry) {log.info("开始注册自定义拦截器...");registry.addInterceptor(jwtTokenUserInterceptor).addPathPatterns("/**").excludePathPatterns(&q…

【Java 多线程】从源码出发,剖析Threadlocal的数据结构

文章目录 exampleset(T value)createMap(t, value);set(ThreadLocal<?> key, Object value)ThreadLocalMap和Thread的关系 全貌 ThreadLocal是个很重要的多线程类&#xff0c;里面数据结构的设计很有意思&#xff0c;很巧妙。但是我们平时使用它的时候常常容易对它的使用…

怎么开发高可靠、能处理多种复杂问题、处理各种异常情况的爬虫程序

开发高可靠、能处理多种复杂问题、处理各种异常情况的爬虫程序是一个综合性的任务&#xff0c;涉及多个方面的技术和策略。以下是一些关键步骤和最佳实践&#xff0c;可以帮助你实现这样的爬虫程序&#xff1a; 明确需求和目标&#xff1a; 在开始编写代码之前&#xff0c;明确…

图片转换成base64如何在html文件中使用呢

在HTML文件中使用Base64编码的图片非常简单。Base64编码是一种将二进制数据转换为ASCII字符串的方法,这使得可以直接在网页上嵌入图片数据,而无需引用外部图片文件。以下是如何在HTML中使用Base64编码的图片的步骤: 步骤 1: 将图片转换为Base64编码 首先,你需要将图片文件…

Photoshop笔记大全

文章目录 PS常用快捷键Shift+工具代码PS名词解释PS影子制作PS图像优化PS常用快捷键 Ctrl+R:调出标尺 Ctrl+T:调整变形。拖动时,按住ctrl键,任意变形。按住Shift键,等比例变形。按Esc键,恢复。 Alt+delete:前景色填充 Ctrl+delete:背景色填充 ctrl+x:剪切 ctrl+v…

WPF+Prism 模块化编程(一)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 WPFPrism 模块化编程&#xff08;一&#xff09; 一、Prism项目创建安装二、将项目升级为Prism项目三、将Prism项目升级为支持模块化编程项目 一、Prism项目创建安装 1、新建…

四道题搞懂二叉树深度

二叉树的最大深度&#xff08;LeetCode104&#xff09; 先表示左树的深度&#xff0c;再表示右树的深度。再进行条件判断 class solution {public int maxDepth(TreeNode root) {if (root null) {return 0;}int leftDepth maxDepth(root.left);int rightDepth maxDepth(ro…

zookeeper常见命令详解2

1、version 展示客户端版本 [zkshell: 1] version ZooKeeper CLI version: 3.6.0-SNAPSHOT-29f9b2c1c0e832081f94d59a6b88709c5f1bb3ca, built on 05/30/2019 09:26 GMT2、whoami 展示出所有已经添加&#xff08;addauth&#xff09;用户的信息 [zkshell: 1] whoami Auth sch…

JAVA面试大全之集合IO篇

目录 1、集合 1.1、Collection 1.1.1、集合有哪些类? 1.1.2、ArrayList的底层?

算法系列--动态规划--特殊的状态表示--分析重复子问题

&#x1f495;"轻舟已过万重山!"&#x1f495; 作者&#xff1a;Lvzi 文章主要内容&#xff1a;算法系列–算法系列–动态规划–特殊的状态表示–分析重复子问题 大家好,今天为大家带来的是算法系列--动态规划--特殊的状态表示--分析重复子问题 一.组合总数IV 链接…

蓝桥集训之游戏

蓝桥集训之游戏 核心思想&#xff1a;博弈论 区间dp 设玩家1的最优解为A 玩家2的最优解为B 1的目标就是使A-B最大 2的目标就是使B-A最大 当玩家1取L左端点时 右边子区间结果就是玩家2的最优解B-A 即当前结果为w[L] – (B-A) 当玩家1取R右端点时 左边子区间结果就是玩家2的最…

Mybatis-特殊SQL的执行

1. 模糊查询 在MyBatis中进行模糊查询时&#xff0c;有以下三种常见的实现方式&#xff1a; 1.1. 错误示范 先来个准备操作&#xff0c;并做一个错误示例 根据姓名&#xff0c;模糊查询用户&#xff0c;(x小x) 更新数据表 SQLMapper.java package com.sakurapaid.mybatis3…

力扣top100-两数之和

思路&#xff1a; 用 hashMap 存储遍历过的元素和对应的索引。 每遍历一个元素&#xff0c;看看 hashMap 中是否存在满足要求的目标数字。 所有事情在一次遍历中完成&#xff08;用了空间换取时间&#xff09;。 // twoSum 函数接受一个整数数组 nums 和一个目标值 target&am…

Win10 搭建FTP存储服务器站点【超详细教程】

目录 第一步&#xff1a;打开控制面板>程序 第二步&#xff1a;win10左下角搜索IIS并打开 第三步&#xff1a;右键网站&#xff0c;选择添加FTP站点 第四步&#xff1a;添加FTP站点名称 第五步&#xff1a;添加IP地址和端口 第六步&#xff1a;身份验证与授权信息 第…

Next.js 客户端组件跨页面跳转时锚点不生效,scrollIntoView 不起作用

项目场景 点击页脚“联系我们”链接&#xff0c;跳转到对应页面的“联系我们”模块。 问题描述 在页脚处“联系我们”代码如下&#xff1a; <a href"/about_us#contactUs">联系我们</a>在“联系我们”的组件中写以下代码&#xff1a; export default…

浅谈Spring体系的理解

浅谈Spring知识体系 Spring Framework架构图Spring家族技术生态全景图XMind汇总 本文不涉及细节&#xff0c;主要回答两个问题&#xff1a; Spring家族技术生态全景图有哪些Spring Framework架构下每个模块有哪些东西&#xff0c;以及部分模块之间的关联关系 Spring Framework架…

爬虫工作量由小到大的思维转变---<第六十五章 > Scrapy去重机制:BaseDupeFilter与request_fingerprint研究

前言&#xff1a; 一下子讲了bloom过滤器&#xff1a;爬虫工作量由小到大的思维转变---&#xff1c;第六十四章 &#xff1e; Scrapy利用Bloom过滤器增强爬虫的页面去重效率-CSDN博客 在网络爬虫的设计中&#xff0c;请求去重是一个至关重要的环节&#xff0c;它直接关系到爬虫…

C语言操作符详细讲解

前言 本次博客一定会让刚刚学习C语言小白有所收获 本次操作符讲解不仅分类还会有代码示例 好好看 好好学 花上几分钟就可以避免许多坑 1 操作符的基本使用 1.1操作符的分类 按功能分 算术操作符&#xff1a; 、- 、* 、/ 、% 移位操作符: >> << 位操作符…