char 和 wchar
char:是单字节
wchar:是宽字节
宽字节是为了配合unicode编码格式而诞生的
需要输出宽字节的时候,需要使用wcout,用cout会出现一堆数字
需要输出中文的时候要加入wcout.imbue(locale(“chs”));这句话
// helloworld.cpp: 定义控制台应用程序的入口点。
//
#include <iostream>
using namespace std;
//命名空间
struct A{};
int main()
{
/********* 一些c++有c没有的数据类型 **********/
//单字节字符
char a = 'A';
wcout << a << " " << sizeof(a) << endl;
//宽字节字符 宽字输出用wcont
wchar_t b = L'S';
wcout << b << " " << sizeof(a) << endl;
//默认locale是en_us,如果要改成中文,要加入 wcout.imbue(locale("chs"))
wchar_t d = L'中';
wcout.imbue(locale("chs"));
wcout << d << " " << sizeof(a) << endl;
return 0;
}
bool类型
c++的bool类型是首字符小写的true和false表示布尔值,这个对用习惯python的我特别的不友好
如果用数字表示布尔值,那么0为假,其他数字都为真。
// helloworld.cpp: 定义控制台应用程序的入口点。
//
#include <iostream>
using namespace std;
//命名空间
struct A{};
int main()
{
/**bool类型**/
bool b1 = true;
bool b2 = false;
bool b3 = 0; //0为假
bool b4 = -1; // 非0为真
cout << b1 << endl;
cout << b2 << endl;
cout << b3 << endl;
cout << b4 << endl;
return 0;
}
long 和 long long 类型
long 占四个字节,longlong占8个字节 实现了32位机器上可以扩展8字节的数据
// helloworld.cpp: 定义控制台应用程序的入口点。
//
#include <iostream>
using namespace std;
//命名空间
struct A{};
int main()
{
/** long 和 longlong 类型 **/
long a = 2147483647; // 2^31-1
long b = 2147483648; // 2^31 溢出
long c = 4294967296; // 2^32 溢出
cout << a << " " << sizeof(a) << endl;
cout << b << " " << sizeof(b) << endl;
cout << c << " " << sizeof(c) << endl;
long long d = 4294967296;
cout << d << " " << sizeof(d) << endl;
return 0;
}
输出
2147483647 4
-2147483648 4
0 4
4294967296 8
auto类型
auto类型 编译器自动分析表达式的类型
使用auto类型的时候一定要赋初值,不赋初值会报错。
// helloworld.cpp: 定义控制台应用程序的入口点。
//
#include <iostream>
using namespace std;
//命名空间
struct A{};
int main()
{
/** auto 类型 **/
auto a = 99;
auto b = "Shanghai";
auto c = L"b";
auto d = 1.56f;
auto e = &a;
int f[2][3];
auto g = f;
// auto h; //需要初始值
cout << a << " " << sizeof(a) << " " << typeid(a).name() << endl;
cout << b << " " << sizeof(b) << " " << typeid(b).name() << endl;
wcout << c << " " << sizeof(c) << " " << typeid(c).name() << endl;
cout << d << " " << sizeof(d) << " " << typeid(d).name() << endl;
cout << e << " " << sizeof(e) << " " << typeid(e).name() << endl;
cout << g << " " << sizeof(g) << " " << typeid(g).name() << endl;
return 0;
}
输出
99 4 int
Shanghai 4 char const *
b 4 wchar_t const *
1.56 4 float
003FF89C 4 int *
003FF86C 4 int (*)[3]