C++中一些简单到我都不知道为什么要写总结的内置函数&&不需要引用头文件的特殊运算符

CMATH 头文件走起

  • double ceil(double x)

x取整为不小于x的最小整数

#include<cmath>
#include<iostream>

using namespace std;

int main()
{
	double x;
	cout<<"x=";
	cin>>x;
	cout<<"ceil(x)="<<ceil(x)<<endl;
	system("pause");
	return 0;
}
输出:
x=9.9
ceil(x)=10.0

x=-9.9
ceil(x)=-9.0

  • double floor(double x)

x取整为不大于x的最大整数

#include<cmath>
#include<iostream>

using namespace std;

int main()
{
	double x;
	cout<<"x=";
	cin>>x;
	cout<<"floor(x)="<<floor(x)<<endl;
	system("pause");
	return 0;
}
输出:
x=9.9
floor(x)=9.0

x=-9.9
floor(x)=-10.0

  • double exp(double x)

求e的x次方

#include<cmath>
#include<iostream>

using namespace std;

int main()
{
	double x;
	cout<<"x=";
	cin>>x;
	cout<<"e^x="<<exp(x)<<endl;
	system("pause");
	return 0;
}
输出:
x=1.0
e^x=2.71828

  • double fmod(double x,double y)

x/y的浮点型余数

#include<cmath>
#include<iostream>

using namespace std;

int main()
{
	double x,y;
	cout<<"x=";
	cin>>x;
	cout<<"y=";
	cin>>y;
	cout<<"fmod(x,y)=mod(x/y)="<<fmod(x,y)<<endl;
	system("pause");
	return 0;
}
输入:
x=10.3
y=3
fmod(x,y)=mod(x/y)=1.3

  • double log(double x)

以e为底x的对数

#include<cmath>
#include<iostream>

using namespace std;

int main()
{
	double x;
	cout<<"x=";
	cin>>x;
	cout<<"ln(x)="<<log(x)<<endl;
	system("pause");
	return 0;
}
输出:
x=4.65
ln(x)=1.53687

  • double log10(double x)

以10为底x的对数

#include<cmath>
#include<iostream>

using namespace std;

int main()
{
	double x;
	cout<<"x=";
	cin>>x;
	cout<<"log10(x)="<<log10(x)<<endl;
	system("pause");
	return 0;
}
输出:
x=100
log10(x)=2

  • double pow(double x,double y)
#include<cmath>
#include<iostream>

using namespace std;

int main()
{
	double x,y;
	cout<<"x=";
	cin>>x;
	cout<<"y=";
	cin>>y;
	cout<<"pow(x,y)=x^y="<<pow(x,y)<<endl;
	system("pause");
	return 0;
}
输出:
x=3
y=2
pow(x,y)=x^y=9

  • double sqrt(double x)

x的平方根

#include<cmath>
#include<iostream>

using namespace std;

int main()
{
	double x;
	cout<<"x=";
	cin>>x;
	cout<<"sqrt(x)="<<sqrt(x)<<endl;
	system("pause");
	return 0;
}
输出:
x=2
sqrt(x)=1.41421

  • double fabs(double x)

x的绝对值

#include<cmath>
#include<iostream>

using namespace std;

int main()
{
	double x;
	cout<<"x=";
	cin>>x;
	cout<<"fabs(x)="<<fabs(x)<<endl;
	system("pause");
	return 0;
}
输出:
x=-1.7
fabs(x)=1.7

  • double sin(double x), double cos(double x), double tan(double x)

x的三类三角函数

#include<cmath>
#include<iostream>

using namespace std;

int main()
{
	double x;
	cout<<"x=";
	cin>>x;
	cout<<"sin(x)="<<sin(x)<<endl;
	cout<<"cos(x)="<<cos(x)<<endl;
	cout<<"tan(x)="<<tan(x)<<endl;
	system("pause");
	return 0;
}
输出:
x=1
sin(x)=0.841471
cos(x)=0.540302
tan(x)=1.55741


毫无灵魂的分割线
感觉大学后的blog比较水,于是就把两部分内容放在了一起

之前我们谈到了<iostream><iomanip>中的一些特殊功能
今天我们就来进行一些简单的补充


sizeof

sizeof是C++中的一种单目运算符,不需要引用头文件
对于sizeof,我们最直观的印象大概还停留在这句上:

memset(a,sizeof(a),0);

我们笼统地知道,sizeof返回的是变量的大小(size)
但是具体是什么就不得而知了

实际上,sizeof() 是一种内存容量度量函数,功能是返回一个变量或者类型的大小(以字节为单位)

#include<iostream>

using namespace std;

int main()
{
	int a;
	int arr[10];
	double b;
	char c;
	bool flag;
	long long LL;
	cout<<"Do sizeof :"<<endl;
	cout<<"int a : "<<sizeof(a)<<endl;
	cout<<"int arr[10] : "<<sizeof(arr)<<endl;
	cout<<"double b : "<<sizeof(b)<<endl;
	cout<<"char c : "<<sizeof(c)<<endl;
	cout<<"bool flag : "<<sizeof(flag)<<endl;
	cout<<"long long LL : "<<sizeof(LL)<<endl;
	system("pause");
	return 0;
}

在这里插入图片描述


static_cast<type-id>(expression)

static_cast是一个c++运算符,功能是把一个表达式转换为某种类型

static_cast相当于传统的C语言里的强制转换
该运算符把expression转换为new_type类型
用来强迫隐式转换,如non-const对象转为const对象
编译时检查,用于非多态的转换,可以转换指针及其他,但没有运行时类型检查来保证转换的安全性

我一开始还在质疑,如果我要将int类型强制转化为double类型,直接*1.0比就可以了吗,为什么要这么麻烦呢?
实际上, static_cast<>() 拥有一些极为强大的功能,在后续的学习中会用到:

  • 用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。
    进行上行转换(把派生类的指针或引用转换成基类表示)是安全的;
    进行下行转换(把基类指针或引用转换成派生类表示)时,由于没有动态类型检查,所以是不安全的。
  • 用于基本数据类型之间的转换,如把int转换成char,把int转换成double
  • 把空指针转换成目标类型的空指针
  • 把任何类型的表达式转换成void类型

注意
static_cast不能转换掉expression的const、volatile、或者__unaligned属性

现在初步掌握基本数据类型之间的转换:

char a = 'a';
int b = static_cast<char>(a);//正确,将char型数据转换成int型数据

double *c = new double;
void *d = static_cast<void*>(c);//正确,将double指针转换成void指针

int e = 10;
const int f = static_cast<const int>(e);//正确,将int型数据转换成const int型数据

const int g = 20;
int *h = static_cast<int*>(&g);//编译错误,static_cast不能转换掉g的const属性

fixed

使浮点数值以浮点格式 ( 而不是科学计数法 ) 输出 ,默认输出小数点后六位,如果不够位数用0补齐
一般情况下,都是与setprecision一起食用
setprecision和fixed合用的话,可以控制小数点后有几位

#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
	double a;
	cin>>a;
	cout<<a<<endl;
	//默认六位数字,包括整数部分
	cout<<setiosflags(ios_base::scientific)<<setprecision(3)<<a<<endl;
	cout<<resetiosflags(ios_base::scientific);
	//科学计数法
	cout<<setprecision(4)<<a<<endl;
	//规定四位数字,包括整数部分
	cout<<fixed<<setprecision(4)<<a<<endl;
	//规定小数部分四位数字
	system("pause");
	return 0;
}

在这里插入图片描述


showpoint

即使数值为整数,仍强制打印小数点和尾部的0

#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
	double a;
	cin>>a;
	cout<<a<<endl;
	cout<<showpoint<<a<<endl;
	//默认六位数字,包括整数部分
	system("pause");
	return 0;
}

看起来很厉害的亚子
然而在我的验证下,发现showpoint的功能完全可以用fixed和setprecision模拟
这就导致我无法观察到showpoint真正的特殊功能 /手动狗头

发布了941 篇原创文章 · 获赞 192 · 访问量 32万+

猜你喜欢

转载自blog.csdn.net/wu_tongtong/article/details/103060756