c++中一些常用库函数

1.最大公约数

需要包括头文件#include<algorithm>,直接写__gcd(a,b),就是求a与b的最大公约数。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<list>
#include<deque>
#define int long long
#define x first
#define y second
using namespace std;
const int N=1e7+5,INF=0x3f3f3f3f,mod=1e9+7,M=5050;
typedef long long LL;
typedef pair<int,int>PII;
void solve(){
	int a,b;
	cin>>a>>b;
	cout<<__gcd(a,b);
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int _;
	//cin>>_;
	while(_--) solve();
    return 0;
}

2.最小公倍数

求最小公倍数没有库函数,但是可以调用lcm函数,就是用a*b/__gcd(a,b),或者直接写该公式。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<list>
#include<deque>
#define int long long
#define x first
#define y second
using namespace std;
const int N=1e7+5,INF=0x3f3f3f3f,mod=1e9+7,M=5050;
typedef long long LL;
typedef pair<int,int>PII;
int lcm(int a,int b){
	return a*b/__gcd(a,b);
}
void solve(){
	int a,b;
	cin>>a>>b;
	cout<<lcm(a,b);
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int _;
	//cin>>_;
	while(_--) solve();
    return 0;
}

3.to_string函数

需要包括头文件#include<cstring>,该函数就是将数字转化为字符串

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<list>
#include<deque>
#define int long long
#define x first
#define y second
using namespace std;
const int N=1e7+5,INF=0x3f3f3f3f,mod=1e9+7,M=5050;
typedef long long LL;
typedef pair<int,int>PII;
void solve(){
	int a;
	string s;
	cin>>a;
	s=to_string(a);
	cout<<s;//输出的为字符串 
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int _;
	//cin>>_;
	while(_--) solve();
    return 0;
}

4.stoi函数

需要包括头文件#include<cstring>,该函数就是将字符串类型的数字转为int类型的数字。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<list>
#include<deque>
#define int long long
#define x first
#define y second
using namespace std;
const int N=1e7+5,INF=0x3f3f3f3f,mod=1e9+7,M=5050;
typedef long long LL;
typedef pair<int,int>PII;
void solve(){ 
	string s;
	cin>>s;
	int a;
	a=stoi(s); 
	cout<<a; 
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int _;
	//cin>>_;
	while(_--) solve();
    return 0;
}

5.stol函数

需要包括头文件#include<cstring>​​​​​​​,该函数就是将字符串类型的数字转为long int类型的数字。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<list>
#include<deque>
#define int long long
#define x first
#define y second
using namespace std;
const int N=1e7+5,INF=0x3f3f3f3f,mod=1e9+7,M=5050;
typedef long long LL;
typedef pair<int,int>PII;
void solve(){ 
	string s;
	cin>>s;
	int a;
	a=stol(s); 
	cout<<a; 
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int _;
	//cin>>_;
	while(_--) solve();
    return 0;
}

6.stoll函数

需要包括头文件#include<cstring>​​​​​​​,该函数就是将字符串类型的数字转为long  long int类型的数字。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<list>
#include<deque>
#define int long long
#define x first
#define y second
using namespace std;
const int N=1e7+5,INF=0x3f3f3f3f,mod=1e9+7,M=5050;
typedef long long LL;
typedef pair<int,int>PII;
void solve(){ 
	string s;
	cin>>s;
	int a;
	a=stoll(s); 
	cout<<a; 
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int _;
	//cin>>_;
	while(_--) solve();
    return 0;
}

7.emplace_back()函数

该函数是c++11的特性,可以在一些vector,list,string等容器的尾部添加一个元素,它与push_back()的区别就是:

push_back():向容器中加入一个右值元素(临时对象)时,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放入容器中。原来的临时变量释放。这样造成的问题就是临时变量申请资源的浪费。

emplace_back():引入了右值引用,转移构造函数,在插入的时候直接构造,只需要构造一次即可。

也就是说,两者的底层实现的机制不同。push_back() 向容器尾部添加元素时,首先会创建这个元素,然后再将这个元素拷贝或者移动到容器中(如果是拷贝的话,事后会自行销毁先前创建的这个元素);而 emplace_back() 在实现时,则是直接在容器尾部创建这个元素,省去了拷贝或移动元素的过程。

总结:

1.push_back 可以接收左值也可以接受右值,接收左值时使用拷贝构造,接收右值时使用移动构造

2.emplace_back 接收右值时调用类的移动构造

3.emplace_back 接收左值时,实际上的执行效果是先对传入的参数进行拷贝构造,然后使用拷贝构造后的副本,也就是说,emplace_back在接收一个左值的时候其效果和push_back一致!所以在使用emplace_back 时需要确保传入的参数是一个右值引用,如果不是,请使用std::move()进行转换。

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: ACM C常用库函数是指用于算法竞赛常见算法实现的C语言标准库函数。这些函数可以帮助竞赛选手减少重复造轮子的时间,提高代码的效率和可读性。以下是一些常见的ACM C常用库函数: 1. qsort()函数:快速排序函数,用于对数组进行排序。 2. bsearch()函数:二分查找函数,用于在有序数组查找元素。 3. memset()函数:用于对变量的内存空间进行赋值。 4. memcpy()函数:用于将一个内存的数组复制到另一个内存空间。 5. sprintf()函数:用于将格式化的输出写入一个字符串。 6. fscanf()和fprintf()函数:用于文件的输入输出。 7. stdio.h的printf()和scanf()函数:C语言常用的标准输入输出函数。 8. math.h的数学函数:如sin,cos,tan,exp,log等。 以上这些函数都是ACM C常用库函数非常重要的一部分,掌握好它们对算法竞赛有很大的帮助。由于算法竞赛时间十分紧张,所以熟悉这些函数可以帮助选手快速完成算法实现,提高算法竞赛的效率和成功率。 ### 回答2: ACM (Association for Computing Machinery) C语言常用库函数是指在解决ACM竞赛各种算法问题时,常常使用的C语言函数库。以下是一些常用库函数: 1. 数学函数库(math.h):可以进行数学计算,包括求平方根、三角函数、对数函数、指数函数等。例如,fabs()用于求绝对值,sqrt()用于求平方根,sin()用于求正弦值等。 2. 字符串函数库(string.h):可以操作字符串,包括拷贝、比较、查找等操作。例如,strcpy()用于字符串拷贝,strcmp()用于字符串比较,strcat()用于字符串拼接等。 3. 标准输入输出函数库(stdio.h):可以进行文件操作、标准输入输出等操作。例如,printf()用于输出格式化字符串,scanf()用于标准输入等。 4. 内存操作函数库(stdlib.h):可以进行动态内存分配、随机数生成等操作。例如,malloc()用于动态内存分配,free()用于释放分配的内存,rand()用于随机数生成等。 5. 时间与日期函数库(time.h):可以获取系统时间,处理时间与日期等操作。例如,time()用于获取系统时间,ctime()用于将时间转换成字符串等。 以上是ACM C语言常用库函数的一部分,ACM竞赛常常使用这些函数库来辅助解决各种算法问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值