栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

力扣刷题常用的c++库函数

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

力扣刷题常用的c++库函数

文章目录
  • 1,max和min
    • 1,max函数
    • 2,min函数
  • 2,sort函数
  • 3,reverse()函数
    • 1.reverse函数可以反转一个字符串
    • 2.反转字符数组
    • 3.反转整型数组

1,max和min 1,max函数
#include 
 
int a=2;
int b=3;
max(a,b);
2,min函数
#include 
 
int a=2;
int b=3;
min(a,b);
2,sort函数

void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

(1)第一个参数first:是要排序的数组的起始地址。

(2)第二个参数last:是结束的地址(最后一个数据的后一个数据的地址)

(3)第三个参数comp是排序的方法:可以是从升序也可是降序。如果第三个参数不写,则默认的排序方法是从小到大排序。

#include
#include
using namespace std;
main()
{
  //sort函数第三个参数采用默认从小到大
  int a[]={45,12,34,77,90,11,2,4,5,55};
  sort(a,a+10);
  for(int i=0;i<10;i++)
  cout< 
#include
#include
using namespace std;
bool cmp(int a,int b);
int main()
{
  //sort函数第三个参数自己定义,实现从大到小
  int a[]={45,12,34,77,90,11,2,4,5,55};
  sort(a,a+10,cmp);
  for(int i=0;i<10;i++)
    cout<
  return a>b;
}
3,reverse()函数

用来翻转 [a,b) 之间的内容

template  void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
     while ((first!=last)&&(first!=--last))
     {
          std::iter_swap (first,last);
          ++first;
     }
}

1.reverse函数可以反转一个字符串
#include 
using namespace std;
int main(){
    string str;
    str = "Hello,world!";
    reverse(str.begin(),str.end());
    cout< 
2.反转字符数组 
#include
using namespace std;
int main(){
    char a[10];
    for(int i = 0; i<10; i++){
        a[i] = 'a' + i;
    }
    reverse(a,a+10);
    puts(a);
    return 0;
}


3.反转整型数组
#include
using namespace std;
int main(){
    int a[10];
    for(int i = 0; i<10; i++){
        a[i] = i;
    }
    reverse(a,a+10);
    for(int i = 0; i<10; i++){
        cout<
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/968374.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号