C++笔记 类型转换,IO,STL标准模板库

来源:互联网 发布:电脑软件助手 编辑:程序博客网 时间:2024/06/07 08:50

1 C++ 类型转换
首先引入#include <iostream>
自动转换

void main(){    int i = 0;    double d = 3.4;    i = d; //double自动转换为int类型    cout << i << endl; //打印结果 3    system("pause");}

使用 static_cast

void main(){    int i = 8;    double d = 5.6;    i = static_cast<int>(d);    cout << i << endl; //打印结果 5    system("pause");}

void* ->char* 类型转换

void* func(){    int i = 9;    return &i;}
void main(){    char *c_p = (char*)func1(2); //void* ->char*    //char *c_p = static_cast<char*>(func()); //与上面的这一行结果相同    cout << *c_p << endl;  //打印结果 X    system("pause");}

void* ->const_cast 去常量

void func(const char c[]){    //c[1] = 'a'; 此行代码将会报错 c不可以修改    //char * c_p = (char*)c;//通过指针间接赋值 和下面的const_cast方式结果相同    //c_p[1] = 'x';    //提高了可读性    char* c_p = const_cast<char*>(c);    c_p[1] = 'Y';    cout << c << endl; //打印结果 hYllo}void main(){    char c[] = "hello";    func(c);    system("pause");}

dynamic_cast 父类转为子类

class Person{public:    virtual void print(){        cout << "人" << endl;    }};class Man : public Person{public:    void print(){        cout << "男人" << endl;    }    void fight(){        cout << "男人会战斗" << endl;    }};class Woman : public Person{public:    void print(){        cout << "女人" << endl;    }    void facePait(){ //化妆        cout << "女人会化妆" << endl;    }};
void func(Person* obj){    //调用子类特有的函数 转为实际类型    //Man *m = (Man*)obj; //转型失败并不知道    Man* m = (Man*)dynamic_cast<Man*>(obj);    if (m!=NULL){        m->fight();    }    Woman* w = dynamic_cast<Woman*>(obj);    if (w!=NULL){        w->facePait();    }}
void main(){    Woman w1;    Person *p1 = &w1;    func(p1); //打印结果:女人会化妆    //Woman* w_p = &w1;     //func(w_p);//可以直接用子类赋给父类    system("pause");}

reinterpret_cast 函数指针转型 不具备移植性(和编译平台有关)

void func1(){    cout << "func1" << endl;}void func2(){    cout << "func2"<<endl;}typedef void(*f_p)();
void main(){    //函数指针数组    f_p f_array[6];    //赋值    f_array[0] = func1;    //C方式    //f_array[1] = f_p(func2);    //C++方式    f_array[1] = reinterpret_cast<f_p>(func2);    f_array[1]();    system("pause");}

打印结果: func2

2 IO
首先引入#include <fstream>

文本文件操作

void main(){    char* fname = "E://test.txt";    //输出流    ofstream fout(fname);    //创建失败    if (fout.bad()){        return;    }    fout << "Hello " << endl;    fout << "rose" << endl;    //关闭    fout.close();    //读取    ifstream fin(fname);    if (fin.bad()){        return;    }    char ch;    while (fin.get(ch)){        //输出        cout << ch;    }    fin.close();    system("pause");}

执行结束后,将会看到E盘下的Test.txt。打印结果为:Hello Rose。

二进制文件

void main(){    char *src = "E://src.jpg";    char *dest = "E://dest.jpg";    //输入流    ifstream fin(src, ios::binary);    //输出流    ofstream fout(dest, ios::binary);    if (fin.bad()||fout.bad()){        return;    }    while (!fin.eof()){        //读取        char buff[1024] = {0};        fin.read(buff,1024);        //写入        fout.write(buff,1024);    }    //关闭    fin.close();    fout.close();    system("pause");}

执行结束后,将会看到E盘下的拷贝文件和之前的文件内容一样

对象持久化

class Person{public:    Person(){    }    Person(char *name,int age){        this->name = name;        this->age = age;    }    void print(){        cout << name << "" << age << endl;    }private:    char*name;    int age;};
void main(){    Person p1("Kobe",39);    Person p2("Rose",28);    //输出流    ofstream fout("E://c_obj.data",ios::binary);    fout.write((char*)&p1,sizeof(Person));    fout.write((char*)&p2,sizeof(Person));    fout.close();    //输入流    ifstream fin("E://c_obj.data",ios::binary);    Person tmp;    fin.read((char*)(&tmp),sizeof(Person));    tmp.print();    fin.read((char*)(&tmp), sizeof(Person));    tmp.print();    system("pause");}

执行后,打印结果:Kobe39 Rose28

3 STL(standard template library)标准模板库

#include <vector>void main(){    //动态数组 不需要使用动态内存分配,就可以使用动态数组    vector<int> v;    v.push_back(32);    v.push_back(20);    v.push_back(36);    v.push_back(32);    for (int i = 0; i < v.size();i++){        cout << v[i] << endl;    }    system("pause");}

打印结果:32 20 36 32

#include <string>void main(){    string s1 = "NBA AllStar:";    string s2("Kobe Blyant");    string s3 = s1 + s2;    cout << s3 << endl;    //转C字符串    const char *c_str = s3.c_str();    cout << c_str << endl;    system("pause");}

打印结果
NBA AllStar:Kobe Blyant
NBA AllStar:Kobe Blyant