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

1,max和min

1,max函数

#include <algorithm>
 
int a=2;
int b=3;
max(a,b);

2,min函数

#include <algorithm>
 
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<iostream>
#include<algorithm>
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<<a[i]<<" ";
}
#include<iostream>
#include<algorithm>
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<<a[i]<<" ";
}
//自定义函数
bool cmp(int a,int b){
  return a>b;
}

sort()函数和lambda表达式

#include<algorithm>
int main()
{
	vector<int> ar = { 1,2,3,4,5 };
	sort(ar.begin(), ar.end(), [](int a, int b)->bool {return a > b; });
	for (auto x : ar)
	{
		cout << x << " ";
	}
	cout << endl;
	return 0;
}

在这里插入图片描述

3,reverse()函数

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

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

1.reverse函数可以反转一个字符串

#include <algorithm>
using namespace std;
int main(){
    string str;
    str = "Hello,world!";
    reverse(str.begin(),str.end());
    cout<<str;
    return 0;
}

2.反转字符数组

#include<bits/stdc++.h>
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<bits/stdc++.h>
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<<a[i]<<' ';
    }
    cout<<endl;
    return 0;
}

4,swap函数

#include <iostream>
#include <algorithm>
using namespace std;
int main( )
{
    int a = 10;
    int b = 5;
    std::swap( a, b );
    cout << "a = " << a << ", b = " << b << endl;
} 

5,memset()函数

C 库函数 **void memset(void str, int c, size_t n) 复制字符 c(一个无符号字符)到参数 str 所指向的字符串的前 n 个字符。

#include <stdio.h>
#include <string.h>
 
int main ()
{
   char str[50];
 
   strcpy(str,"This is string.h library function");
   puts(str);
 
   memset(str,'$',7);
   puts(str);
   
   return(0);
}

输出结果:

This is string.h library function
$$$$$$$ string.h library function

6,memcpy()

#include<string.h>
将seq.data数据复制到data里面
memcpy(data, seq.data, sizeof(data));

7,strcpy()

#include<string.h>
char *strcpy(char *dest, const char *src)
将src的内容复制到dest,并返一个指向dest的指针

8,substr()string的切割赋值

string s="";
s=s1.substr(pos, len);
//pos的默认值是0,len的默认值是s.size() - pos,即不加参数会默认拷贝整个s)
//若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾
//从第pos位开始复制n位给s

9,abs()求绝对值函数

int num=abs(a-b);求a-b的绝对值

10,从屏幕中输入一行数字到数组里面

已知数组个数

int main() {
	vector<int>ar;
	int n, m;
	cin >> n;
	while (n--&& cin >> m) {
		ar.push_back(m);
	}
	for (auto x : ar) {
		cout << x << endl;
	}
	return 0;
}

在这里插入图片描述
在这里插入图片描述

int main() {
	
	int n;
	cin >> n;
	vector<int>ar(n);
	for (int i = 0; i < n; ++i) {
		cin >> ar[i];
	}
	for (auto x : ar) {
		cout << x << endl;
	}
	return 0;

在这里插入图片描述
在这里插入图片描述

int main() {
	
	int n,m;
	cin >> n;
	vector<int>ar;
	for (int i = 0; i < n; ++i) {
		cin >>m;
		ar.push_back(m);
	}
	for (auto x : ar) {
		cout << x << endl;
	}
	return 0;
}

未知数组个数

int main() {
	int m;
	vector<int>ar;
	while(cin>>m) {
		ar.push_back(m);
		if (cin.get() == '\n') break;
	}
	for (auto x : ar) {
		cout << x << endl;
	}
	return 0;
}

在这里插入图片描述

11,map set

在这里插入图片描述
map插入

ret = mymap.insert(make_pair("C语言教程", "http://c.biancheng.net/c/"));

map查询

1,第1种:用count函数来判断关键字是否出现,其缺点是无法定位数据出现的位置,由于map的一对一的映射特性,就决定了count函数的返回值只有两个,要么是0,要么是1,当要判断关键字出现时返回1.

2,用find函数来定位数据出现的位置,它返回一个迭代器,当数据出现时,它返回数据所在位置的迭代器;如果没有要查找的数据,返回end()
map<int, string>::iterator iter=mapStudent.find(1);

map删除

void erase (iterator position);//删除迭代器指向的位置的元素
size_type erase (const key_type& x);//删除key的对
void erase (iterator first, iterator last);//删除从first到last的迭代器指向的元素

c++提供的大小写转换库函数

int main()
{
	string str = "THIS IS A STRING";
	for (int i = 0; i < str.size(); i++)
		str[i] = tolower(str[i]);
	cout << str << endl;
	for (int i = 0; i < str.size(); ++i) {
		str[i] = toupper(str[i]);
	}
	cout << str << endl;
	return 0;
}
  • 7
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,关于力扣刷题C++常用操作,我可以给你一些常见的操作和技巧: 1. 使用 STL 容器和算法库:STL(Standard Template Library)是 C++ 标准库中的一个重要组成部分,包含了许多常用的容器和算法。在力扣刷题中,使用 STL 可以大大提高代码的效率和可读性。例如,vector 可以用来存储动态数组,sort 可以用来排序等等。 2. 使用 auto 关键字:auto 关键字可以自动推导变量类型,可以减少代码量和提高可读性。例如,auto x = 1; 可以自动推导出 x 的类型为 int。 3. 使用 lambda 表达式:lambda 表达式是 C++11 中引入的一种匿名函数,可以方便地定义一些简单的函数对象。在力扣刷题中,使用 lambda 表达式可以简化代码,例如在 sort 函数中自定义比较函数。 4. 使用位运算:位运算是一种高效的运算方式,在力扣刷题中经常会用到。例如,左移运算符 << 可以用来计算 2 的幂次方,右移运算符 >> 可以用来除以 2 等等。 5. 使用递归:递归是一种常见的算法思想,在力扣刷题中也经常会用到。例如,二叉树的遍历、链表的反转等等。 6. 使用 STL 中的 priority_queue:priority_queue 是 STL 中的一个容器,可以用来实现堆。在力扣刷题中,使用 priority_queue 可以方便地实现一些需要维护最大值或最小值的算法。 7. 使用 STL 中的 unordered_map:unordered_map 是 STL 中的一个容器,可以用来实现哈希表。在力扣刷题中,使用 unordered_map 可以方便地实现一些需要快速查找和插入的算法。 8. 使用 STL 中的 string:string 是 STL 中的一个容器,可以用来存储字符串。在力扣刷题中,使用 string 可以方便地处理字符串相关的问题。 9. 注意边界条件:在力扣刷题中,边界条件往往是解决问题的关键。需要仔细分析题目,考虑各种边界情况,避免出现错误。 10. 注意时间复杂度:在力扣刷题中,时间复杂度往往是评判代码优劣的重要指标。需要仔细分析算法的时间复杂度,并尽可能优化代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值