C++:数据类型

我学过的数据类型有 整型(int/long)、短整型(short)、长整型(long long)、单精度浮点型(float)、双精度浮点型(double)、长双精度浮点型(long double)、字符型(char)、布尔型(bool)、字符串型(string)、结构体类型(struct)。

我还知道 空类型(void)、共用体类型(union)、类类型(class)、枚举类型(enum)、指针类型(*),但还没学过、不熟悉。

数据类型:取值范围汇总

有符号int、long的最大取值范围:2147483647

有符号int、long的最小取值范围:-2147483648

无符号int、long的最大取值范围:4294967295

无符号int、long、long long、short的最小取值范围:0

float的最大取值范围:340282349999999991754788743781432688640

float的最小取值范围:-340282349999999991754788743781432688641

double的最大取值范围:178999999999999996376899522972626047077637637819240219954027593177370961667659291027329061638406108931437333529420935752785895444161234074984843178962619172326295244262722141766382622299223626438470088150218987997954747866198184686628013966119769261150988554952970462018533787926725176560021258785656871583744

double的最小取值范围:-178999999999999996376899522972626047077637637819240219954027593177370961667659291027329061638406108931437333529420935752785895444161234074984843178962619172326295244262722141766382622299223626438470088150218987997954747866198184686628013966119769261150988554952970462018533787926725176560021258785656871583745

long double的最大取值范围:179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216

long double的最小取值范围:-179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137217

bool的取值范围:true、false/0、1

char的最大取值范围:127

char的最小取值范围:-128

string的最大长度:4999213071378415615

具体介绍:

1、整型(int/long):

整型其实在我看来有些争议,就是int和long的区别。int和long在我看来其实并没有区别,有很多人认为long的最大取值范围与long long一样,是9223372036854775807,但其实不是。如果你使用INT_MAX函数给一个数据类型为long的变量赋值,再将其加一,会数据溢出,变为-2147483648。↓就跟这个一样。

#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	long n=INT_MAX;
	n++;
	cout<<n;

	return 0;
}

很显然,long的最大取值范围就是2147483647。int和long占用的字节数其实也一样,通常都是4。

#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cout<<sizeof(int)<<endl<<sizeof(long);

	return 0;
}

以上都是有符号整型(signed int/signed long),还有无符号整型(unsigned int/unsigned long)。

无符号整型所占用的字节数也是4,这点也可以用sizeof关键字获取。

#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cout<<sizeof(unsigned int)<<endl<<sizeof(unsigned long);

	return 0;
}

无符号整型相当于把取值范围内的负数部分加到了正数部分上,并把负数部分舍去了,故其最小取值范围为0。

2、短整型(short):

短整型把取值范围缩小了,最大取值范围为32727。相应的,短整型所占用的字节数也缩小了,利用sizeof关键字可以得到其占用字节数为2。

#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cout<<sizeof(short);

	return 0;
}

以上是有符号短整型(signed short),无符号短整型(unsgined short)也和无符号整型一样把负数部分的取值范围加到了正数部分的取值范围上,再舍去负数部分,故其最小取值范围为0。

#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cout<<sizeof(unsigned short);

	return 0;
}

3、长整型(long long):

long long是C++整数数据类型中取值范围较大的一种整数数据类型,最大取值范围为9223372036854775807。long long占用8字节,可以用sizeof获得。

#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cout<<sizeof(long long);

	return 0;
}

无符号长整型和无符号整型、无符号短整型一样,把负数部分挪到了正数部分上,同时舍去了负数部分,令其最小值为0。

4、单精度浮点型(float)、双精度浮点型(double):

float在学习中很少用,学习中总是用double。float占4字节,double占8字节。虽然float占的内存比double少,但float的精度不如double和long double。若是想要对比float和double的精度差距,我们可以通过这个程序理解。

#include <bits/stdc++.h>
using namespace std;
int main()
{//给float型和double型的变量都赋值圆周率小数点后100位并保留50位小数输出。
	ios::sync_with_stdio(false);
	double a;
	float b;
	a=3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
	b=3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
	cout<<fixed<<setprecision(50)<<a<<endl<<fixed<<setprecision(50)<<b;

	return 0;
}

float的精度不如double,而且double在取值范围上远超float,并且double占的内存也不多,所以在学习C++时总是用double。

5、长双精度浮点型(long double):

long double在前面学习中很少用到,因为long double占的内存多而精度也没比double好,大多数情况下double完全够用了,所以我这个阶段很少用。long double应该是占16字节,我用的Dev-C++和小熊猫C++编译器都是16。大家可以用不同的编译器编译运行试试看,结果欢迎在评论区分享。

#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cout<<sizeof(long double);

	return 0;
}

6、字符型(char):

char在我这个阶段偶尔会使用,在打印各种图形时经常使用。char的取值范围在-128~127,但因为即使超过了这个范围也不会报错或警告,所以经常能“利用”char打出一些奇怪的字符。char占1字节大小,而且只能存放一个字符。char对应的是单引号(''),千万不要使用双引号("")。

#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cout<<sizeof(char);

	return 0;
}

7、字符串(string):

C++中字符串可以被拆分成多个字符,就像一个字符数组。字符串的长度可以用 字符串名.size()函数获得,比如:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	string a,b;
	string c,d;
	a="我是中国人。";//一个汉字或中文字符占两个英文字符的位置
	b="我爱 中国";//一个汉字占两个英文字符的位置
	c="I am Chinese.";
	d="I love China";
	cout<<a.size()<<endl<<b.size()<<endl<<c.size()<<endl<<d.size();

	return 0;
}

如果将字符串拆分成数组,那么它就是从0到 字符串名.size()-1。

字符串的长度不能超过511。字符串变量占用32字节或8字节,依然可以通过sizeof关键字获得。

#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	cout<<sizeof(string);

	return 0;
}

string对应的是双引号(""),千万不要使用单引号('')。

字符串还有一些函数:

1、isupper:

isupper函数用于判断字符串中某个字符是否为大写,返回true或false。虽然返回true或false,但其实还会返回一些其它的东西,所以我们不能写类似于if(isupper(s[i])==true)的语句,如果要判断某个字符串中的字符是否是大写字母只能写if(isupprer(s[i])!=false)或者if(isupper(s[i]))这种类型的。

2、islower:

islower函数用于判断字符串中某个字符是否为小写,返回true或false。它跟isupper函数一样也会返回其它的东西。

3、isalpha:

isalpha函数用于判断字符串中某个字符是否为英文字母,返回true或false。它除了true和false一样也会返回其它的东西。

4、isdigit:

isalpha函数用于判断字符串中某个字符是否为英文字母,返回true或false。它除了true和false一样也会返回其它的东西。
5、toupper:

toupper函数会返回这个字母的大写字母的ASCII编码,若是想要将所有英文字母转换为其对应的大写字母,可以这么写:

#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	string s;
	getline(cin,s);
	for(int i=0;i<=s.size()-1;i++) s[i]=char(toupper(s[i]));
	cout<<s;

	return 0;
}

6、tolower:

tolower函数会返回这个字母的小写字母的ASCII编码,若是想要将所有英文字母转换为其对应的小写字母,可以这么写。

#include <bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	string s;
	cin>>s;
	for(int i=0;i<=s.size()-1;i++) s[i]=char(tolower(s[i]));
	cout<<s;

	return 0;
}

7、getline:

getline函数用于输入字符串。getline与cin的最显著区别在于getline可以输入空格而不让输入中断,但cin输入空格后会中断输入。使用getline输入和使用cin输入若输入换行符都会中断输入。

8、布尔型(bool):

布尔型变量能够是true、false、任意数字、"\n"(n在此是任意数字)。其中,false、"\0"和其它等于0的数字都是false(否),其它为true(是)。

bool型变量有时被我当做旗帜,在山重水复疑无路时经常使用。

9、结构体(struct):

struct是定义结构体类型的关键字。结构体事实上是自定义的数据类型,详情见以下代码。

#include <bits/stdc++.h>
using namespace std;
struct stu//定义了一种数据类型,叫作stu 
{
	string name;//定义一个student的一种属性,代表名称 
	short student_ID;//定义一个student的一种属性,代表学号 
	float score;//定义一个student的一种属性,代表成绩 
}student[44];//这些都是student的属性/结构体成员,每个student都有这些属性 
int main()
{
	ios::sync_with_stdio(false);
	short n;
	cin>>n;
	for(short i=0;i<=n-1;i++) cin>>student[i].name>>student[i].student_ID>>student[i].score;
	for(short i=0;i<=n-1;i++)
	{
		if(student[i].score>=90) cout<<student[i].student_ID<<"号"<<student[i].name<<' '<<"优秀"<<endl;
		else if(student[i].score>=70) cout<<student[i].student_ID<<"号"<<student[i].name<<' '<<"良好"<<endl;
		else if(student[i].score>=60) cout<<student[i].student_ID<<"号"<<student[i].name<<' '<<"合格"<<endl;
		else if(student[i].score<60) cout<<student[i].student_ID<<"号"<<student[i].name<<' '<<"不合格"<<endl;
	}

	return 0;
}

结构体中自定义的数据类型可以是匿名的,但编译器会作出警告。

如果各位有不同的理解或发现有错误,欢迎在评论区说出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值