C++学习第九课

九、其他

目录

九、其他

1、 nullptr

2、类型推导

3、初始化列表

4、面试题

5、 进制输出

6、设定输出域宽度

7、文件IO

8、线程池


1、 nullptr

NULL在源码中就是一个0,因此可能会存在二义性的问题。

#include <iostream>
#include <memory>
using namespace std;
 
void func(int a)
{
    cout << "a=" << a << endl;
}
 
void func(int *b)
{
    cout << "b=" << b << endl;
}
 
int main()
{
    func(NULL); // a = 0
 
    return 0;
}

在C++11中使用nullptr代替NULL,作为空指针的表示方式。

#include <iostream>
#include <memory>
using namespace std;
 
void func(int a)
{
    cout << "a=" << a << endl;
}
 
void func(int *b)
{
    cout << "b=" << b << endl;
}
 
int main()
{
    func(nullptr); // b = 0
 
    return 0;
}

2、类型推导

使用auto关键字可以推导类型,C++11引入的。

#include <iostream>
using namespace std;
 
 
int main()
{
    auto i = 10;    // i的类型被推导为整形(int)
    cout << i << endl;
 
    auto i2 = 10.3; // i2的类型被推导为浮点型
    cout << i2 << endl;
 
    auto i3 = new auto(10); // i3被推导为int* i3 = new int(10);
    cout << *i3 << endl;
 
    auto i4 = "hello";
    cout << i4 << endl;
 
    auto i5 = 'a';
    cout << i5 << endl;
 
    delete i3;
 
    return 0;
}

decltype可以推导表达式类型,需要注意的是,decltype只会分析表达式的类型,不会具体计算表达式的值。

#include <iostream>
using namespace std;
 
 
int main()
{
    auto x = 1;
    auto y = 2;
 
    decltype(x*y+100+2)z = 888.98; // int * int + int + int = int
    cout << z << endl;  /// 888
 
    return 0;
}

3、初始化列表

C++11 中引入了列表的初始化(初始化列表,通用统一初始化、一致性初始化)语法,可以使用{}对对象进行初始化。

#include <iostream>
#include <array>
#include <vector>
using namespace std;
 
 
class Student
{
private:
    string name;
    int age;
public:
    Student(string name,int age):name(name),age(age){}
 
    void show()
    {
        cout << name << " " << age << endl;
    }
};
 
 
int main()
{
    array<int,5> arr1 = {1,2,3,4,5};
    for(int i:arr1)
    {
        cout << i << " ";
    }
    cout << endl;
 
    array<int,5> arr2 = {1,2,3};
    for(int i:arr2)
    {
        cout << i << " ";
    }
    cout << endl;
 
    vector<int> vec1 = {1,2,3};
    for(int i:vec1)
    {
        cout << i << " ";
    }
    cout << endl;
 
 
    int arr3[3] = {1,2,3};
    for(int i:arr3)
    {
        cout << i << " ";
    }
    cout << endl;
 
    int arr4[] = {4,4,4,4,4,4};
    cout << sizeof(arr4)/sizeof(int) << endl;   // 6
 
    int a{};
    cout << a << endl;  // 0
 
    Student s = {"张三",18};
    s.show();
 
    return 0;
}

4、面试题

【面试题】C++11学过那些特性?

1、 智能指针(后三个)

2、 for-each

3、 nullptr

4、 auto类型推导

5、 初始化列表

6、 array

7、 类型转换(四种cast函数)

8、 继承构造

9、 override

10、 constexpr

11、线程池

5、 进制输出

C++11中可以对整数进行不同进制的输出。

#include <iostream>
 
using namespace std;
 
 
int main()
{
    // 为了区分不同进制,可以增加进制显式功能,此功能持久
    cout << showbase;
    // 默认为十进制
    cout << dec << 1234 << endl;    // 1234
    cout << oct << 1234 << endl;    // 2322
 
    // 输出的进制设定是持久的
    cout << 9 << endl;  // 11
    cout << hex << 256 << endl;     // 100
 
    // 取消显式功能
    cout << noshowbase;
    cout << 16 << endl;     // 10
 
 
    return 0;
}

6、设定输出域宽度

可以使用setw()来指定一个整数或者一个字符串输出占用的域宽度。

● 当设定域宽度小于数据本身时,任然会显式为数据本身的宽度。

● 当设定域宽度大于数据本身是,会显示未设定的域宽度。

需要注意的是,输出域宽度不是持久的,需要每次输出设定。

#include <iostream>
#include <iomanip>
using namespace std;
 
 
int main()
{
    // 仍然按照实际的宽度输出
    cout << setw(5) << 123456 << setw(5) << 123456 << endl;
    // 使用输出域宽度
    cout << setw(10) << 123456 << setw(10) << 123456 << endl;
 
    cout << setw(10);
    cout << 123456 << endl;
 
    cout << 123456 << endl; // 输出域无效,因为输出域只能作用于下一行。
 
    cout << setw(10);
    cout << 123455 << endl;
 
    return 0;
}

7、文件IO

#include <iostream>
#include <fstream> // 头文件
using namespace std;
 
 
int main()
{
    // 文件输入流对象,用于读取数据
    // 参数1:读取的路径
    // 参数2:读取方式,二进制读取
    ifstream ifs("D:\\MP4\\C++011_day2\\meeting_01.mp4",ios_base::binary);
    if(!ifs)
    {
        cout << "文件或路径不存在" << endl;
        return -1;
    }
 
    // 文件输出流对象,用于输出数据
    // 参数1:写出的路径
    // 参数2:写出方式为二进制
    ofstream ofs("C:\\Users\\Administrator\\Desktop\\meeting_01.mp4",ios_base::binary);
    if(!ofs)
    {
        cout << "输出路径不存在" << endl;
        return -1;
    }
    // 把文件指针移动到文件尾部
    // 参数1:相对偏移量
    // 参数2:移动的位置
    ifs.seekg(0,ios::end);
    cout << "文件总大小:" << ifs.tellg() << "bytes" << endl;
 
    // 把文件流指针移动回头部
    ifs.seekg(0,ios::beg);
 
    // 准备一个buffer
    char buf[2048];
    long long hasRead = 0;   // 已经读取的文件大小
 
    while(ifs)
    {
       // 读取固定长度的数据到buf中
       ifs.read(buf,2048);
       // 输出设定长度的数据到目标位置
       // 参数1:数据来源
       // 参数2:输出的数据量ifs.gcount为上一次文件指针的偏移量。
       ofs.write(buf,ifs.gcount());
 
       hasRead += ifs.gcount();
       cout << "已经拷贝的字节数:" << hasRead << endl;
    }
    // 收尾
    ifs.close();
    ofs.flush();    // 清空缓存区
    ofs.close();
    cout << "拷贝成功" << endl;
    return 0;
}

8、线程池

线程池先鸽一下,过两天单独写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值