栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

(c++笔记)第三十二节课:*

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(c++笔记)第三十二节课:*

一 类型转换

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

1. 过于粗暴
    任意类型之间都能转换,编译器很难判断其正确性
2. 难于定位
    在源码中无法快速定位所有使用强制类型转换的语句
1.1 static_cast强制类型转换
1.用于基本类型间的转换,但是不能用于基本类型指针之间的转换
2.用于有继承关系类对象之间的转换和类指针之间的转换。
3.static_cast是在编译期间转换的,无法在运行时检测类型,所以类型之间转换可能存在风险。
#include 
using namespace std;
class Parent
{
​
};
class Child :public Parent
{
​
};
int main(void)
{
    int a = 1;
    char ch = 'x';
    a = static_cast(ch);  //用于普通类型之间的转换
    //int *p = static_cast(&ch);
​
    Parent p;
    Child c;
    //p = c;
    p = static_cast(c);  //用于有继承关系的类对象之间的转换
​
    //c = static_cast(p);
    Parent *p1 = static_cast(&c);   //类对象指针之间的转换
​
}
1.2 reinterpret_cast
#include 
using namespace std;
int main(void)
{
    int c = 200;
    int b = 100;
    char *p = reinterpret_cast(&c);  //用于普通指针之间的转换(不安全)
    cout << *(p + 1) << endl;
    //cout << *(&b + 1) << endl;
​
    int *q = reinterpret_cast(100); //用于数字和指针之间的转换(很容易出现野指针)
    *q = 1;
    return 0;
}
1.3 const_cast
#include 
using namespace std;
int main(void)
{
    const int a = 1; //常量
    int *p = const_cast(&a);  
    *p = 100;
    cout << a << endl;
    cout << *p << endl;
​
    const int &m = 1;
    int &n = const_cast(m);
    n = 100;
    cout << m << endl;
    cout << n << endl;
​
    const int x = 1;
    int &y = const_cast(x);
    y = 100;
    cout << x << endl;
    cout << y << endl;
    int z = 200;
    const int *p1= &z;
    int *q = const_cast(p1);
    *q = 300;
    cout << *q << endl;
    cout << *p1 << endl;
    return 0;   
}
1.4 dynamic_cast
#include 
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(new Parent); //派生类指针指向基类对象(错误)
    c = dynamic_cast(new Parent);  //运行时候会进行类型检查,不能转换,会返回NULL,比static_cast安全
    if (NULL == c)
    {
        cout << "转换失败" << endl;
    }
    else
    {
        cout << "转换成功" << endl;
        delete c;
    }
​
    return 0;
    
}
二 算法
更易型算法
非更易型算法
排序算法
#include  
#   //数学运算的模板函数
#  //提供了一些模板类,用来声明函数对象
2.1 遍历算法
#include 
#include 
#include 
#include 
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 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 
#include 
#include 
#include 
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 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 
#include 
#include 
#include 
#include 
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 v1;
    vector v2;
    for (int i = 0; i < 10; i++)
    {
        v1.emplace_back(rand() % 10);
        v2.emplace_back(rand() % 10);
    }
    
    sort(v1.begin(), v1.end(), less());
    stable_sort(v2.begin(), v2.end(), less());
    for (auto &v : v1)
    {
        cout << v << " ";
    }
    cout << endl;
    for (auto &v : v2)
    {
        cout << v << " ";
    }
    cout << endl;
​
    vector 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 
#include 
#include 
#include 
#include 
using namespace std;
​
void show(int x)
{
    cout << x << endl;
}
int main(void)
{
    vector v1(5, 1);
    vector 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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029873.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号