c++中一些常用库函数

embedded/2024/9/22 17:56:54/

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()进行转换。


http://www.ppmy.cn/embedded/9867.html

相关文章

C语言案例——输出以下图案(两个对称的星型三角形)

目录 图片代码 图片 代码 #include<stdio.h> int main() {int i,j,k;//先输出上半部图案for(i0;i<3;i){for(j0;j<2-i;j)printf(" ");for(k0;k<2*i;k)printf("*");printf("\n");}//再输出下半部分图案for(i0;i<2;i){for(j0;j&…

springboot使用Mybatis中兼容多数据源的databaseId(databaseIdProvider)的简单使用方法

最近有兼容多数据库的需求&#xff0c;原有数据库使用的mysql&#xff0c;现在需要同时兼容mysql和pgsql&#xff0c;后期可能会兼容更多。 mysql和pgsql很多语法和函数不同&#xff0c;所以有些sql需要写两份&#xff0c;于是在全网搜索如何在mapper中sql不通用的情况下兼容多…

配置全局代理上网

#准备一台机器(160)&#xff0c;DNS注释掉 直接访问不行 curl https://www.baidu.com #代理访问OK curl -x 192.141.46.161:8888 https://www.baidu.com #CentOS设置全局代理 vi ~/.bashrc #加入以下内容 export http_proxyhttp://192.141.46.161:8888 export https_proxy…

为什么js无法通过contentDocument获取到iframe内容

最近在开发一个附件预览的页面&#xff0c;考虑到附件的安全性由后端对内容进行了加密&#xff0c;前端负责在页面引入iframe标签来加载数据&#xff0c;在这个过程中需要实时获取到iframe标签的渲染状态来判断什么时候loading结束&#xff1a; 这里我通过标签的方式来获取到i…

nacos配置mysql(windows)

nacos默认是使用的内置数据库derby ,可通过配置修改成mysql,修改成mysql之后&#xff0c;之前配置在derby的数据会丢失 本文使用mysql版本为8.0.22 nacos版本为2.3.1 在mysql里面先创建一个数据库test(名称自定义&#xff0c;和后面配置文件里面的一样就好了) 在上面创建的数据…

实验7-4:补全代码,插入操作

实验7-4&#xff1a;补全代码&#xff0c;插入操作 【问题描述】 初始化一维数组中的9个元素a[10]{2,5,6,8,11,15,17,20,25}&#xff0c;要求该数组已经按升序排列&#xff0c;从键盘输入一个整数num&#xff0c;并将其插入到数组a中&#xff0c;要求插入操作完成后&#xff0…

lazarus-ide简介

Lazarus是一个集成开发环境&#xff08;IDE&#xff09;&#xff0c;专为使用Free Pascal编译器的Pascal语言设计。它支持快速应用开发&#xff08;RAD&#xff09;&#xff0c;允许开发者创建跨平台的图形用户界面&#xff08;GUI&#xff09;应用程序。以下是关于Lazarus的来…

null和undefined区别

首先 Undefined 和 Null 都是基本数据类型&#xff0c;这两个基本数据类型分别都只有一个值&#xff0c;就是 undefined 和 null。 undefined 代表的含义是未定义&#xff0c;null 代表的含义是空对象。 一般变量声明了但还没有定义的时候会返回 undefined&#xff0c;null主要…