0
点赞
收藏
分享

微信扫一扫

【C++】零零碎碎习题日记——2022/3/30

RockYoungTalk 2022-03-30 阅读 73
c++

**

【C++】零零碎碎习题日记——2022/3/30

**

数据类型

1.格式易忘点:

char用 ‘ ’ string用 “ ”

2.数据类型转换:

例:double转int,只是去除小数部分,无四舍五入,+0.5再转换可实现四舍五入

	int a,c;
	double b=3.6;
	a=(int)b;        //向下取整
	c=(int)(b+0.5);  //四舍五入
	cout<<a<<" "<<c<<endl;

动态数组

1.一维动态数组(以int型为例

#include<iostream>
using namespace std;
int main(){
	int n;
	cin>>n;
	int *p=new int[n];   //生成长度为n的一维动态数组p 
	delete []p;          //别忘了释放 
	return 0; 
} 

2.二维动态数组(以int型为例

#include<iostream>
using namespace std;
int main(){
	int n,m;
	cin>>n>>m;
	int **p=new int*[n];   //生成n行m列的动态数组p 
	for(int i=0;i<n;i++){
		p[i]=new int[m];
	}
	delete []p;          //别忘了释放 
	return 0; 
} 

输出指定精度/补零

指定精度

#include <iostream>
#include <iomanip> //必须包含这个头文件
using namespace std;
int main( ){
    double a =3.141596; 
    cout<<fixed<<setprecision(3)<<a<<endl;  //输出小数点后3位
    return 0}

前位补零(输出指定位数

#include <iostream>
#include <iomanip> //必须包含这个头文件
using namespace std;
int main( ){
	int year,month,day;
    year=2022;month=3;day=30; 
    cout<<year<<"/"<<setw(2)<<setfill('0')<<month<<"/"<<setw(2)<<setfill('0')<<day<<endl;   //输出2022/03/30 
    return 0;
}

举报

相关推荐

一些零零碎碎的记录

C++零碎笔记(3)

C++零碎知识

零碎的C++

C++零碎笔记(2)

C++ 自学日记(3)

5_3 gkxx recitation 9 零碎知识点

0 条评论