(c++笔记)第三十二节课:*_cast类型转换,算法

本文详细介绍了C++中的四种类型转换:static_cast、reinterpret_cast、const_cast和dynamic_cast,以及它们的适用场景和潜在风险。同时,文章讲解了STL中的遍历、查找和排序算法,包括for_each、transform、adjacent_find、binary_search、count、count_if、find、find_if、sort、stable_sort、merge、random_shuffle和reverse等,展示了这些算法在实际编程中的应用。

一 类型转换

c 方式强制类型转换存在的问题

1. 过于粗暴
    任意类型之间都能转换,编译器很难判断其正确性
2. 难于定位
    在源码中无法快速定位所有使用强制类型转换的语句

1.1 static_cast强制类型转换

1.用于基本类型间的转换,但是不能用于基本类型指针之间的转换
2.用于有继承关系类对象之间的转换和类指针之间的转换。
3.static_cast是在编译期间转换的,无法在运行时检测类型,所以类型之间转换可能存在风险。
#include <iostream>
using namespace std;
class Parent
{
​
};
class Child :public Parent
{
​
};
int main(void)
{
    int a = 1;
    char ch = 'x';
    a = static_cast<int>(ch);  //用于普通类型之间的转换
    //int *p = static_cast<int *>(&ch);
​
    Parent p;
    Child c;
    //p = c;
    p = static_cast<Parent>(c);  //用于有继承关系的类对象之间的转换
​
    //c = static_cast<Child>(p);
    Parent *p1 = static_cast<Parent *>(&c);   //类对象指针之间的转换
​
}

1.2 reinterpret_cast

#include <iostream>
using namespace std;
int main(void)
{
    int c = 200;
    int b = 100;
    char *p = reinterpret_cast<char *>(&c);  //用于普通指针之间的转换(不安全)
    cout << *(p + 1) << endl;
    //cout << *(&b + 1) << endl;
​
    int *q = reinterpret_cast<int *>(100); //用于数字和指针之间的转换(很容易出现野指针)
    *q = 1;
    return 0;
}

1.3 const_cast

#include <iostream>
using namespace std;
int main(void)
{
    const int a = 1; //常量
    int *p = const_cast<int *>(&a);  
    *p = 100;
    cout << a << endl;
    cout << *p << endl;
​
    const int &m = 1;
    int &n = const_cast<int &>(m);
    n = 100;
    cout << m << endl;
    cout << n << endl;
​
    const int x = 1;
    int &y = const_cast<int &>(x);
    y = 100;
    cout << x << endl;
    cout << y << endl;
    int z = 200;
    const int *p1= &z;
    int *q = const_cast<int *>(p1);
    *q = 300;
    cout << *q << endl;
    cout << *p1 << endl;
    return 0;   
}

1.4 dynamic_cast

#include <iostream>
using namespace std;
class Parent
{
public:
    virtual void f()
    {
​
    }
};
class Child :public Parent
{
public:
    void f()
    {
​
    }
};
int main(void)
{
    Child *c = new Child;
    delete c;
    //c = static_cast<Child *>(new Parent); //派生类指针指向基类对象(错误)
    c = dynamic_cast<Child *>(new Parent);  //运行时候会进行类型检查,不能转换,会返回NULL,比static_cast安全
    if (NULL == c)
    {
        cout << "转换失败" << endl;
    }
    else
    {
        cout << "转换成功" << endl;
        delete c;
    }
​
    return 0;
    
}

二 算法

更易型算法
非更易型算法
排序算法
#include <algorithm> 
#<numeric>   //数学运算的模板函数
#<functional>  //提供了一些模板类,用来声明函数对象

2.1 遍历算法

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class Print
{
public:
    void operator()(int x)
    {
        cout << x << endl;
    }
};
void show(int x)
{
    cout << x << endl;
}
int main(void)
{
    int array[5] = { 1,2,3,4,5 };
    vector<int> v(array,array + 5);
​
    //for_each
    //for_each(v.begin(), v.end(), show);  //回调函数
    for_each(v.begin(), v.end(), Print());  //函数对象的形式遍历
    
    //transform遍历过程中可以修改数据
    string s("hello world");
    transform(s.begin(),s.end(),s.begin(),::toupper);
    cout << s << endl;
    return 0;
    
}
​
​

2.2 查找算法

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
bool GreaterTwo(int x)
{
    return x > 2;
}
class Print
{
public:
    void operator()(int x)
    {
        cout << x << endl;
    }
};
class GreaterThree
{
public:
    bool operator()(int x)
    {
        return x > 3;
    }
};
void show(int x)
{
    cout << x << endl;
}
int main(void)
{
    int array[7] = { 6,2,4,5,2,5 };   
    vector<int> v(array, array + 6);
    auto it = adjacent_find(v.begin(), v.end());
    if (it == v.end())
    {
        cout << "不存在重复且相邻的元素" << endl;
    }
    else
    {
        cout << *it << endl;
    }
    bool ret = binary_search(v.begin(), v.end(), 4);  //查找有序的序列,二分查找法  // [1,3) 3 [4,7)
    if (ret)
    {
        cout << "元素存在" << endl;
    }
    else
    {
        cout << "元素不存在" << endl;
    }
​
    int num = count(v.begin(), v.end(), 2);
    cout << num << endl;
    num = count_if(v.begin(), v.end(), GreaterTwo);  //一元谓词 回调函数
    cout << num << endl;
​
    it = find(v.begin(), v.end(), 3);
    if (it == v.end())
    {
        cout << "元素不存在" << endl;
    }
    else
    {
        cout << *it << endl;
    }
    it = find_if(v.begin(), v.end(), GreaterThree());
    if (it == v.end())
    {
        cout << "元素不存在" << endl;
    }
    else
    {
        cout << *it << endl;
    }
    return 0;
}

2.3 排序算法

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <time.h>
using namespace std;
bool GreaterTwo(int x)
{
    return x > 2;
}
class Print
{
public:
    void operator()(int x)
    {
        cout << x << endl;
    }
};
class GreaterThree
{
public:
    bool operator()(int x)
    {
        return x > 3;
    }
};
void show(int x)
{
    cout << x << endl;
}
int main(void)
{
    vector<int> v1;
    vector<int> v2;
    for (int i = 0; i < 10; i++)
    {
        v1.emplace_back(rand() % 10);
        v2.emplace_back(rand() % 10);
    }
    
    sort(v1.begin(), v1.end(), less<int>());
    stable_sort(v2.begin(), v2.end(), less<int>());
    for (auto &v : v1)
    {
        cout << v << " ";
    }
    cout << endl;
    for (auto &v : v2)
    {
        cout << v << " ";
    }
    cout << endl;
​
    vector<int> v3;
    v3.resize(20);
    merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
    for (auto &v : v3)
    {
        cout << v << " ";
    }
    cout << endl;
​
    random_shuffle(v1.begin(), v1.end());
    for (auto &v : v1)
    {
        cout << v << " ";
    }
    cout << endl;
​
    reverse(v3.begin(), v3.end());
    for (auto &v : v3)
    {
        cout << v << " ";
    }
    cout << endl;
​
    string s("hello world");
    reverse(s.begin(), s.end());
    cout << s << endl;
    return 0;
}

2.4 拷贝替换

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <time.h>
using namespace std;
​
void show(int x)
{
    cout << x << endl;
}
int main(void)
{
    vector<int> v1(5, 1);
    vector<int> v2(5, 2);
    v2.resize(6);
    copy(v1.begin(),v1.end(),++(v2.begin()));
    for_each(v2.begin(), v2.end(), show);
    cout << endl;
​
    swap(v1, v2);
    for_each(v2.begin(), v2.end(), show);
    cout << endl;
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值