20170808

来源:互联网 发布:数据安全成熟度模型 编辑:程序博客网 时间:2024/06/07 08:40
///*********************************************************////*                 STL-Iterator学习                      *////*********************************************************///#include <iostream>//#include <list>//#include <vector>//////using namespace std;////int main()//{//    //    vector<int> v;//    for (int i = 1; i <= 9; ++i)//        v.push_back(i);//    vector<int>::iterator iter = v.begin();////    //随机迭代器 提供“迭代算数运算”//    cout << *iter<<endl;//    cout << iter[0]<<endl;//    iter += 3;//    cout << *iter << endl;//    iter -= 3;//    cout << *iter << endl;////    list<int> coll;//    for (int i = 1; i <= 9; ++i)//        coll.push_back(i);//    list<int>::iterator pos = coll.begin();////    iter_swap(coll.begin(), ++coll.begin());//    for (pos; pos != coll.end(); ++pos)//        cout << *pos ;//    cout << endl;//    pos = coll.begin(); //做完循环以后迭代器失效了,需要重新赋值////    iter_swap(coll.begin(), --coll.end());//    for (pos; pos != coll.end(); ++pos)//        cout << *pos ;//    cout << endl;//    pos = coll.begin();////    cout << *pos << endl;//    advance(pos, 3);//    cout << *pos << endl;//    advance(pos, -1);//    cout << *pos << endl;//  //    system("pause");//}//#include <iostream>  //#include <list>  //#include <algorithm>  //using namespace std;////int main()//{//    list<int> coll;////    //insert elements from -3 to 9  //    for (int i = -3; i <= 9; ++i) {//        coll.push_back(i);//    }////    //search element with value 5  //    list<int>::iterator pos;//    pos = find(coll.begin(), coll.end(),        //range  //        5);                              //value  ////    if (pos != coll.end()) {//        //process and print difference from the beginning  //        cout << "difference between beginning and 5: "//            << distance(coll.begin(), pos) << endl; //8, 第一个位置的distance是0  //    }//    else {//        cout << "5 not found" << endl;//    }//}///*********************************************************////*                 STL-Iterator-iter_swap学习                      *////*********************************************************///#include <iostream>  //#include <list>  //#include <algorithm>  //#include <vector>////using namespace std;////int main()//{//    list<int> coll;////    //insert elements from 1 to 9  //    for (int i = 1; i <= 9; ++i) {//        coll.push_back(i);//    }////    //swap first and second value  //    iter_swap(coll.begin(), ++coll.begin());////    //swap first and last value  //    iter_swap(coll.begin(), --coll.end());////    vector<int> v;//    for (int i = 1; i <= 9; ++i) //    {//        v.push_back(i);//    }//    //different iterator can swap//    iter_swap(find(coll.begin(),coll.end(),3), v.end()-1);//}///*********************************************************////*                 C++类型转换                           *////*********************************************************/#include <iostream>using namespace std;//const_caststruct A{    int a;};//dynamic_castclass AA{public:    virtual void foo(){ cout << "i am base foo\n"; };};class BB :public AA{public:    void foo(){ cout << "i am derived foo\n"; };};class CC:public AA{};//reinterpret_castint doSomething(){ return 0; };typedef void(*FuncPtr)(); //FuncPtr is 一个指向函数的指针,该函数没有参数,返回值类型为 void  int main(){    //const_cast    const A ra;    const A * pa = new A;    //ra.a = 10;  常对象    A& rb = const_cast<A&>(ra);    rb.a = 10;    A* pb = const_cast<A*>(pa);    pb->a = 20;    //static_cast    int c = 10;    double d = static_cast<double>(c);    int* p = &c;    void *q = static_cast<void*>(p);    void* k = NULL;    int* s = static_cast<int *>(k);    int dd = 9;    s = ⅆ    //dynamic_cast pc指向子类的指针    AA * pc = new BB();    BB * pc2 = dynamic_cast<BB*>(pc); //子类 - 子类 动态类型转换    pc->foo();    pc2->foo();        //相同基类不同子类的转换     BB* pw = new BB;    CC* pw2 = dynamic_cast<CC*>(pw); //pw2为null    pw->foo();    //pw2->foo(); 报错    //父类->子类,动态类型转换,安全的!ps2 将是一个NULL 指针。      AA* ps = new AA();    BB* ps2 = dynamic_cast<BB*>(ps);    ps->foo();    //ps2->foo(); 报错    //reinterpret_cast    FuncPtr funcPtrArray[10]; //10个FuncPtrs指针的数组 让我们假设你希望(因为某些莫名其妙的原因)把一个指向下面函数的指针存入funcPtrArray数组:      //funcPtrArray[0] = &doSomething;// 编译错误!类型不匹配,reinterpret_cast可以让编译器以你的方法去看待它们:funcPtrArray      funcPtrArray[0] = reinterpret_cast<FuncPtr>(&doSomething); //不同函数指针类型之间进行转换          system("pause");}

原创粉丝点击