C++程序设计(一) [cin,cout加速,基本数据类型,保留浮点数]

程序基本概念

1.常量:执行过程中不可改变的量(const int…)

2.变量:执行过程中可以改变的量(int a , long long b…)

3.关键字:不能用作程序中的标识符(变量名不能为关键字)

基本数据类型

int: 整数类型,占用4字节,范围: − 2 31 ∼ 2 31 − 1 ≈ ∣ 2.1 × 1 0 9 ∣ -2^{31}\sim2^{31}-1\approx\left |2.1\times10^{9} \right | 2312311 2.1×109

long long: 64位整数类型,占用8字节,范围: − 2 63 ∼ 2 63 − 1 ≈ ∣ 9.2 × 1 0 18 ∣ -2^{63}\sim2^{63}-1\approx\left |9.2\times10^{18} \right | 2632631 9.2×1018 又称 i n t 64 int64 int64

float: 单精度实数型,占用4字节,范围: − 3.4 × 1 0 38 ∼ 3.4 × 1 0 38 -3.4\times10^{38}\sim3.4\times10^{38} 3.4×10383.4×1038,精度约为7位有效数字

double: 双精度实数型,占用8字节,范围: − 1.7 × 1 0 308 ∼ 1.7 × 1 0 308 -1.7\times10^{308}\sim1.7\times10^{308} 1.7×103081.7×10308,精度约为15位有效数字

char: 字符型,占用1个字节,表示一个字符,需用英文单引号括起来,如:'a','0',char 类型中存放字符的ASCLL码。

bool: 布尔型,用于表示真,假逻辑值,占1个字节。赋值0为false,非0为true

程序基本语句

cin,cout: 在iostream标准库中定义:

#include <iostream>

可控制标准的输入输出流,其中cin为输入,cout为输出
code↓

#include <iostream>//调用标准库
using namespace std;//定义命名空间,避免函数库产生冲突
int main(){
   //调用主函数
	int a,b;//定义两个int类型的变量
	cin>>a>>b;//输入这两个变量
	//std::cin>>a>>b;//如果没有"using namespace std;"就需要加上"std::";
	cout<<a+b;
	//std::cout<<a+b;//同上。只需要在输入和输出时加上即可
	return 0;//告诉机器程序正常结束
}

return 0 结果如下

在这里插入图片描述
如果程序(函数)不是正常结束,加上了 return 0 ,返回值就不会是0

保留浮点数(小数)

可以使用fixed , setprecision(x) setiosflags(ios::fixed) 来控制精度。

其中setiosflags(ios::fixed) 为设置小数点后的精度。

其中setprecision(x) 为将精度设置为x位。

需使用iomanip作为标准库

#include <iomanip>

整体比较 code↓

#include <iostream>
#include <iomanip>//调用标准库
using namespace std;
int main(){
   
	double o=123.456789;
	cout<<setprecision(4)<<o<<endl;//整体保留4位
	//<1>↑
	cout<<fixed<<setprecision(3)<<o<<endl;//小数部分保留3位
	//<2>↑
	setiosflags(ios::fixed);//命令:以下的"setprecision(x)"都是设置小数部分
	cout<<setprecision(3)<<o<<endl;//效果等同于<2>
	cout<<setprecision(4)<<o<<endl;//设置小数部分的精度为4
	//<3>↑
	return 0;//程序正常结束
}

运行结果:↓
在这里插入图片描述

文件输入输出,cin,cout 加速[可以加速到约等于scanf(),printf()]

code↓

#include <bits/stdc++.h>//包含大多数函数库的头文件
using namespace std;
int main(){
   
	ios::sync_with_stdio(false)
	cin.tie(0),cout.tie(0);
	freopen("xxx.in","r",stdin)//从xxx.in进行读入,如果xxx.in不存在则失败
	freopen("xxx.out","w",stdout)
	//将输出写入xxx.out,如果xxx.out不存在则创建这个文件
	return 0;

相关推荐

  1. mysql 点数类型

    2024-02-20 04:40:01       20 阅读
  2. unity中点数保留位小数

    2024-02-20 04:40:01       37 阅读
  3. C++设置点数精度

    2024-02-20 04:40:01       38 阅读
  4. 点数加法

    2024-02-20 04:40:01       46 阅读
  5. KY79 点数加法

    2024-02-20 04:40:01       46 阅读
  6. QT c++ 将点数数组转换成 QByteArray

    2024-02-20 04:40:01       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-20 04:40:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-20 04:40:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-20 04:40:01       87 阅读
  4. Python语言-面向对象

    2024-02-20 04:40:01       96 阅读

热门阅读

  1. CDH 6.x版本 HBase基础调优参数

    2024-02-20 04:40:01       51 阅读
  2. mysql3.7之触发器

    2024-02-20 04:40:01       46 阅读
  3. 2月19日,每日信息差

    2024-02-20 04:40:01       50 阅读
  4. Python 将一维数组或矩阵变为三维

    2024-02-20 04:40:01       65 阅读
  5. 阿里云香港服务器是cn2吗?怎么测试?

    2024-02-20 04:40:01       55 阅读