码迷,mamicode.com
首页 > 编程语言 > 详细

C++primer(第五版)第二章的学习笔记(也有对部分习题的解答和指出c++11特性)

时间:2014-11-02 09:32:11      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:c++primer第五版   c++11特性   源代码   变量和基本类型第二章相关习题   

算术类型分为两类:整型(字符和bool在内)和浮点型。

C/C++算术类型
类型 含义 最小尺寸
bool 布尔类型 未定义
char 字符 8位
wchar_t 宽字符 16位
char16_t Unicode字符 16位
char32_t
Unicode字符
32位
short 短整型 16位
int  整型 16位
long 长整型 32位
long long 长整型
64位
unsigned long 无符号长整型 32位
double 双精度浮点数 10位有效数字
long double 扩展精度浮点数 10位有效数字
float
单精度浮点数
6位有效数字
size_t 无符号整型 32位
string 字符串 32位
C++源代码如下:bubuko.com,布布扣

#include<iostream>
#include<string>
#include <limits>
using namespace std;
int main()
{
    cout << "type: \t\t" << "************size**************"<< endl;
    cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
    cout << "\t最大值:" << (numeric_limits<bool>::max)();
    cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
    cout << "char: \t\t" << "所占字节数:" << sizeof(char);
    cout << "\t最大值:" << (numeric_limits<char>::max)();
    cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
    cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
    cout << "\t最大值:" << (numeric_limits<signed char>::max)();
    cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
    cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
    cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
    cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
    cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
    cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
    cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
    cout << "short: \t\t" << "所占字节数:" << sizeof(short);
    cout << "\t最大值:" << (numeric_limits<short>::max)();
    cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
    cout << "int: \t\t" << "所占字节数:" << 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: \t\t" << "所占字节数:" << sizeof(long);
    cout << "\t最大值:" << (numeric_limits<long>::max)();
    cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
     cout << "long long: \t\t" << "所占字节数:" << sizeof(long long);
    cout << "\t最大值:" << (numeric_limits<long long>::max)();
    cout << "\t最小值:" << (numeric_limits<long long>::min)() << endl;
    cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
    cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
    cout << "\t最小值:" << (numeric_limits<unsigned long>::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(long double);
    cout << "\t最大值:" << (numeric_limits<long double>::max)();
    cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
    cout << "float: \t\t" << "所占字节数:" << 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: \t\t" << "************size**************"<< endl;
    return 0;
}

numeric_limits的解释:

数值类型的极值是一个与平台相关的特性。c++标准程序库通过template numeric_limits提供这些极值,取代传统C语言所采用的预处理常数。你仍然可以使用后者,其中整数常数定义于<climits>和<limits.h>,浮点常数定义于<cfloat>和<float.h>,新的极值概念有两个优点,一是提供了更好的类型安全性,二是程序员可借此写出一些template以核定这些极值。

注意一些转义序列:

转义序列
名称 符号
 报警响铃符 \a
回车符 \r
进纸符 \f
响铃 \7
换行符 \n;\12
空字符;空格 \0;\40

总结:

习题注意无符号打印负数时,-1对应为4294967295;-32对应为4294967264;对应-b对应为4294967295-(b-1)=4294967296-b;类型匹配,不然易处错误;

C++11特性本章总结如下:

1.另外上面的long long类型为C++11中的新定义;

2.列表初始化:

例子     long double ld=3.1415926536;

            int a{ld},b={ld};//错误,转换未执行;

            int c(ld);d=ld;//正确:转换执行,类似c语言强制类型转换丢失部分信息;

3.空指针nullptr:用字面值nullptr来初始化指针

   int *p1=nullptr;//等价于int *p1=0;

  int *p1=NULL;//等价于int *p1=0;需要头文件(cstdlib);

4.constexpr和常量表达式:允许将变量声明为constexpr类型以便由编译器来验证变量的值是否为一个常量表达式。

     constexpr int mf=20;//20是常量表达式;

     constexpr int limit=mf+1;//mf+1是常量表达式;

     constexpr int sz=size();//只有当size()为constexpr函数时,才是一条正确语句声明;

5.auto类型说明符:auto让编译器通过初始值来推算变量的类型。(许多编译器暂不支持)

    auto item=val1+val2;//item初始化为val1和val2相加的结果;

   auto i=0,*p=&i;//i是整数,p是整型指针;

  auto sz=0,pi=3.14;//声明多个变量类型匹配必须一致,否则就报错;

6.decltype类型指示符:选择并返回操作数的数据类型。(特别注意:编译器分析表达式并得到它的类型,却不实际计算表达式)

   decltype(f()) sum=x;//sum的类型就是函数f的返回类型;

习题2.37

     int a=3,b=4;

decltype(a) c=a;

decltype(a=b) d=a;

从而 a=3,b=4,c=3,d=3;

当然,decltype(a)的结果就是引用类型,因此必须被初始化;

  const int ci=0,%cj=ci;

  decltype(ci)x=0;//正确,x的类型const int;

 decltype(cj)z;// 错误,z是一个引用,未被初始化;

7.类内初始值:创建对象时,类内初始值将用于初始化数据成员。没有初始值的成员将被默认初始化。

    


            

C++primer(第五版)第二章的学习笔记(也有对部分习题的解答和指出c++11特性)

标签:c++primer第五版   c++11特性   源代码   变量和基本类型第二章相关习题   

原文地址:http://blog.csdn.net/wangjin123456789/article/details/40681291

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!