c++中一些常用库函数

1.最大公约数

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

cpp 复制代码
#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),或者直接写该公式。

cpp 复制代码
#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>** ,该函数就是将数字 转化为字符串

cpp 复制代码
#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类型的数字。

cpp 复制代码
#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类型的数字。

cpp 复制代码
#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类型的数字。

cpp 复制代码
#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()进行转换。

相关推荐
武当豆豆1 小时前
C++编程学习阶段性总结
开发语言·c++
学不动CV了2 小时前
C语言32个关键字
c语言·开发语言·arm开发·单片机·算法
你怎么知道我是队长2 小时前
python-enumrate函数
开发语言·chrome·python
小屁孩大帅-杨一凡2 小时前
如何解决ThreadLocal内存泄漏问题?
java·开发语言·jvm·算法
大熋2 小时前
Playwright Python 教程:网页自动化
开发语言·python·自动化
赟赟、嵌入式3 小时前
imx6ul Qt运行qml报错This plugin does not support createPlatformOpenGLContext!
开发语言·qt
A7bert7773 小时前
【YOLOv8-obb部署至RK3588】模型训练→转换RKNN→开发板部署
linux·c++·人工智能·python·yolo
cdg==吃蛋糕3 小时前
selenium 使用方法
开发语言·python
Y1nhl4 小时前
力扣_二叉树的BFS_python版本
python·算法·leetcode·职场和发展·宽度优先
爱掉发的小李4 小时前
前端开发中的输出问题
开发语言·前端·javascript