包括算术类型和空类型
带符号类型和无符号类型
#include <iostream> int main() { bool b = 42; // b为真 int i = b; // i为1 std::cout << b << " " << i << std::endl; i = 3.14; // i为3 double pi = i; // pi为3.0 std::cout << i << " " << pi << std::endl; unsigned char c1 = -1; signed char c2 = 256; std::cout << c1 << " " << c2 << std::endl; return 0; }
略
引用和指针
引用相当于别名,与一个已经存在的对象绑定起来
引用必须在定义时赋值
#include <iostream> int main() { int val = 1024; int &refVal = val; //refVal指向val, refVal表示一个引用与val绑定起来 return 0; }
指针本身就是一个对象,允许对指针赋值和拷贝
指针无需再定义时赋值
#include <iostream> int main() { double val = 3.14; double *pVal = &val; //pVal表示指针指向变量val std::cout << pVal << std::endl; //地址 std::cout << *pVal << std::endl; //值 return 0; }
#include <iostream> int main() { double val = 3.14; double *pVal = &val; *pVal = 0; //通过指针改变val值 std::cout << pVal << std::endl; //地址 std::cout << *pVal << std::endl; //值 std::cout << val << std::endl; //值为0 return 0; }
void 指针* 无法访问内存空间所存的对象
指向指针的指针
#include <iostream> int main() { int val = 1024; int *pVal = &val; int **ppVal = &pVal; return 0; }
值不能被改变的变量
typeof
typeof longlong LL;
using
using SI = Sales_item;
c++ 11 新特性
#include <iostream> int main() { auto i = 0; auto c ='c'; std::cout << i << " " << c << std::endl; return 0; }
#include <iostream> #include <string> struct Sales_data { std::string bookNo; unsigned units_sold = 0; double revenue = 0.0; }; int main() { Sales_data data1; data1.bookNo = "111"; data1.revenue = 11.11; data1.units_sold = 1; std::cout << data1.bookNo << " " << data1.revenue << " " << data1.units_sold << std::endl; return 0; }
略