02x数据类型8-15
基本的内置类型
C++ 为程序员提供了种类丰富的内置数据类型和用户自定义的数据类型。下表列出了七种基本的 C++ 数据类型:
|
|
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
下表显示了各种变量类型在内存中存储值时需要占用的内存,以及该类
型的变量所能存储的最大值和最小值。
注意:不同系统会有所差异,一字节为 8 位。
注意:默认情况下,int、short、long都是带符号的,即 signed。
注意:long int 8 个字节,int 都是 4 个字节,早期的 C 编译器定义了 long int 占用 4 个字节,int 占用 2 个字节,新版的 C/C++ 标准兼容了早期的这一设定。
|
|
|
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sizeof 用法
#include<iostream> #include<limits>usingnamespacestd; intmain(){ cout << "type: tt" << "************size**************"<< endl; cout << "bool: tt" << "所占字节数:" << sizeof(bool); cout << "t最大值:" << (numeric_limits<bool>::max)(); cout << "tt最小值:" << (numeric_limits<bool>::min)() << endl; cout << "char: tt" << "所占字节数:" << sizeof(char); cout << "t最大值:" << (numeric_limits<char>::max)(); cout << "tt最小值:" << (numeric_limits<char>::min)() << endl; cout << "signed char: t" << "所占字节数:" << sizeof(signedchar); cout << "t最大值:" << (numeric_limits<signedchar>::max)(); cout << "tt最小值:" << (numeric_limits<signedchar>::min)() << endl; cout << "unsigned char: t" << "所占字节数:" << sizeof(unsignedchar); cout << "t最大值:" << (numeric_limits<unsignedchar>::max)(); cout << "tt最小值:" << (numeric_limits<unsignedchar>::min)() << endl; cout << "wchar_t: t" << "所占字节数:" << sizeof(wchar_t); cout << "t最大值:" << (numeric_limits<wchar_t>::max)(); cout << "tt最小值:" << (numeric_limits<wchar_t>::min)() << endl; cout << "short: tt" << "所占字节数:" << sizeof(short); cout << "t最大值:" << (numeric_limits<short>::max)(); cout << "tt最小值:" << (numeric_limits<short>::min)() << endl; cout << "int: tt" << "所占字节数:" << sizeof(int); cout << "t最大值:" << (numeric_limits<int>::max)(); cout << "t最小值:" << (numeric_limits<int>::min)() << endl; cout << "unsigned: t" << "所占字节数:" << sizeof(unsigned); cout << "t最大值:" << (numeric_limits<unsigned>::max)(); cout << "t最小值:" << (numeric_limits<unsigned>::min)() << endl; cout << "long: tt" << "所占字节数:" << sizeof(long); cout << "t最大值:" << (numeric_limits<long>::max)(); cout << "t最小值:" << (numeric_limits<long>::min)() << endl; cout << "unsigned long: t" << "所占字节数:" << sizeof(unsignedlong); cout << "t最大值:" << (numeric_limits<unsignedlong>::max)(); cout << "t最小值:" << (numeric_limits<unsignedlong>::min)() << endl; cout << "double: t" << "所占字节数:" << sizeof(double); cout << "t最大值:" << (numeric_limits<double>::max)(); cout << "t最小值:" << (numeric_limits<double>::min)() << endl; cout << "long double: t" << "所占字节数:" << sizeof(longdouble); cout << "t最大值:" << (numeric_limits<longdouble>::max)(); cout << "t最小值:" << (numeric_limits<longdouble>::min)() << endl; cout << "float: tt" << "所占字节数:" << sizeof(float); cout << "t最大值:" << (numeric_limits<float>::max)(); cout << "t最小值:" << (numeric_limits<float>::min)() << endl; cout << "size_t: t" << "所占字节数:" << sizeof(size_t); cout << "t最大值:" << (numeric_limits<size_t>::max)(); cout << "t最小值:" << (numeric_limits<size_t>::min)() << endl; cout << "string: t" << "所占字节数:" << sizeof(string) << endl; // << "t最大值:" << (numeric_limits<string>::max)() << "t最小值:" << (numeric_limits<string>::min)() << endl; cout << "type: tt" << "************size**************"<< endl; return0; }
typedef 声明
您可以使用 typedef 为一个已有的类型取一个新的名字。下面是使用 typedef 定义一个新类型的语法:
typedef type newname;
派生数据类型
|
|
|
---|---|---|
数组 |
|
int arr[5] = {1, 2, 3, 4, 5}; |
指针 |
|
int* ptr = &x; |
引用 |
|
int& ref = x; |
函数 |
|
int func(int a, int b); |
结构体 |
|
struct Point { int x; int y; }; |
类 |
|
class MyClass { ... }; |
联合体 |
|
union Data { int i; float f; }; |
枚举 |
|
enum Color { RED, GREEN, BLUE }; |
类型别名
|
|
|
---|---|---|
typedef |
|
typedef int MyInt; |
using |
|
using MyInt = int; |
标准库类型
|
|
|
---|---|---|
std::string |
|
std::string s = "Hello"; |
std::vector |
|
std::vector<int> v = {1, 2, 3}; |
std::array |
|
std::array<int, 3> a = {1, 2, 3}; |
std::pair |
|
std::pair<int, float> p(1, 2.0); |
std::map |
|
std::map<int, std::string> m; |
std::set |
|
std::set<int> s = {1, 2, 3}; |
常见创建变量错误
写值时的单双引号
一个字符变量想写多个字符的方法
转义字符
|
|
---|---|
\ |
|
' |
|
" |
|
? |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t
水平制表符的对齐效果
v
垂直制表符对齐效果
字符串型
-
C风格型字符串: char charname[] = "ABCDEF"
,注意是双引号。 -
C++风格字符串: string strname = "ABCDEF"
,注意是双引号。头文件要包含#include <string>
#include<iostream> #include<limits>#include<string>usingnamespacestd;intmain(){char ch1[] = "Hello World"; // C-style stringstring str1 = "Hello C++"; // C++ stringcout << ch1 << endl;cout << str1 << endl;return0;}
布尔类型
bool:表示布尔类型,只有 true(1) 和 false(0) 两个值。
#include<iostream> usingnamespacestd;intmain(){bool flag = true;cout << flag << endl; flag = 0;cout << flag << endl;cout << sizeof(flag) << endl; }
数据输入
从键盘获取输入: cin >> 变量
#include<iostream> usingnamespacestd;intmain(){int a = 10; //int是C++中的整型cout << "a的值为" << a << endl;cout << "输入a的新值" << endl;cin >> a;cout << "a的新值为" << a << endl;float b = 10.5; //float是C++中的浮点类型cout << "b的值为" << b << endl;cout << "输入b的新值" << endl;cin >> b;cout << "b的新值为" << b << endl;char c = 'a'; //char是C++中的字符类型cout << "c的值为" << c << endl;cout << "输入c的新值" << endl;cin >> c;cout << "c的新值为" << c << endl;bool d = true; //bool是C++中的布尔类型cout << "d的值为" << d << endl;cout << "输入d的新值" << endl;cin >> d; //只要非0都是真cout << "d的新值为" << d << endl;string e = "Hello World"; //string是C++中的字符串类型cout << "e的值为" << e << endl;cout << "输入e的新值" << endl;cin >> e;cout << "e的新值为" << e << endl;return0;}
原文始发于微信公众号(泷羽Sec-静安):02x数据类型8-15
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论