c++ demo code

/*



//多继承
#include <iostream>
using namespace std;

class Sofa
{
public:
    Sofa();
    ~Sofa();
    
    void sit() {
        cout<<"sit!"<<endl;
    }
    
    void setWeight(int w) {
        this->weight = w;
    }
    
    int getWeight() {
        return this->weight;
    }
    
    void showWeight() {
        cout<<this->weight<<endl;
    }
    
private:
    
    int weight;
    
};

Sofa::Sofa()
{
    cout<<"Sofa,构造!"<<endl;
}

Sofa::~Sofa()
{
    cout<<"Sofa,析构!"<<endl;
}

class Bed
{
public:
    Bed();
    ~Bed();
    
    void lie() {
        cout<<"lie!"<<endl;
    }
    
    void setWeight(int w) {
        this->weight = w;
    }
    
    int getWeight() {
        return this->weight;
    }
    
    void showWeight() {
        cout<<this->weight<<endl;
    }
    
private:
    
    int weight;
};

Bed::Bed()
{
    cout<<"Bed,构造!"<<endl;
}

Bed::~Bed()
{
    cout<<"Bed,析构!"<<endl;
}


class Sofabed : public Bed, public Sofa
{
public:
    Sofabed();
    ~Sofabed();
    
    void showWeight() {
        
        Sofa::showWeight();
        cout<<"&&"<<endl;
        Bed::showWeight();
        
    }
    
private:
    
};

Sofabed::Sofabed()
{
    cout<<"Sofabed,构造!"<<endl;
}

Sofabed::~Sofabed()
{
    cout<<"Sofabed,析构!"<<endl;
}


int main () {
    
    Sofabed myfur;
    myfur.sit();
    myfur.lie();
    
    // myfur.setWeight(12); // super的名字有冲突。不行。
    myfur.Sofa::setWeight(12); // 要写 [obj.classname::function(12)] ;
    myfur.Sofa::showWeight();
    myfur.Bed::setWeight(99);
    myfur.Bed::showWeight(); // sofa & bed 的 member 是两个不一样的。
    
    // 也可以在 derived class 里,overload 名字冲突的函数
    myfur.showWeight();
    
    
    system("pause");
    return 0;
    
}
 
 

//多继承
#include <iostream>
using namespace std;


class CB0
{
public:
    CB0() {
        cout<<"CB0,构造!"<<endl;
    };
    ~CB0() {
        cout<<"CB0,析构!"<<endl;
    };
    
    void fun() {
        cout<<"CB0::fun()"<<endl;
    }
    
    void fun(int i) {
        cout<<"CB0::fun(int i) "<<endl;
    }
    
};

class CD0 : public CB0
{
public:
    CD0() {
        cout<<"CD0,构造!"<<endl;
    };
    ~CD0() {
        cout<<"CD0,析构!"<<endl;
    };
    
    void fun(int i) {
        cout<<"CD0::fun(int i) "<<endl;
    }
};

int main () {
    
    CB0 * obj1 = new CD0;
    obj1->fun();
    obj1->fun(0);
    
    CD0 obj2;
    obj2.fun(0); // 只有这一个了!
    
    system("pause");
    return 0;
}


//多继承
#include <iostream>
using namespace std;


class CA
{
    
public:
    CA(int a):x(a) {
        cout<<"CA构造函数
";
    }
    ~CA() {cout<<"CA析构函数
";}
    int x;
    
private:
    
    
};

class CB : public CA
{
public:
    CB(int a, int b):CA(a),y(b){
        cout<<"CB构造函数
";
    }
    ~CB() {
        cout<<"CB析构函数
";
    }
    int y;
    
private:
    
};

class CC : public CA
{
public:
    CC(int a, int b):CA(a) {
        this->z = b;
        cout<<"CC 构造
";
    };
    ~CC() {
        cout<<"CC 析构
";
    };
    int z;
    
private:
    
};

class CD : public CB, public CC
{
public:
    CD(int a , int b, int c, int d,int e) : CC(c,d),CB(a,b) {
        cout<<"CD 构造
";
        this->w = e;
    }
    ~CD() {
        cout<<"CD 解构
";
    }
    
    void ShowVal() {
        cout << "x=" << CB::x << " y=" << y;
        cout << " x=" << CC::x << " z=" << z;
        cout << " w=" << w << endl;
    }
    
private:
    int w;
};

void fun1() {
    
    CD cd1(1,2,3,4,5); // 按理说 cb的x 和 cc的x 其实是一样的。并不是[后一个值,把前一个值覆盖掉!]
    cout<<"xxxxxxxxxxxxxxxxxxxxxxxxxx"<<endl;
    cd1.ShowVal(); // 但事实上不一样!
    
    // 避免这种情况,用【虚继承】!!!!!
    // 【虚基类】!!!!!!!!!!!!!
    
}


int main() {
    
    fun1();
    
    system("pause");
    return 0;
    
}


//多继承
#include<iostream>
using namespace std;

class Child1
{
public:
    Child1() :a(0), b(0), c(0) { cout << "child 构造
"; }
    ~Child1()
    {
        cout << "child 析构,,,
";
    }
    void c1_print()
    {
        cout << "a b c is" << a << " " << b << " " << c << endl;
    }
    
    int a;
    int b;
    int c;
};
class Child2
{
public:
    Child2() :a(1), b(2), c(3) { cout << "child 构造
"; }
    ~Child2()
    {
        cout << "child 析构,,,
";
    }
    void c2_print()
    {
        cout << "a b c is" << a << " " << b << " " << c << endl;
    }
    int a;
    int b;
    int c;
};
class Child3 : public Child1,  public Child2
{
public:
    Child3() : Child1(),Child2(), b(20), c(30) { cout << "child 构造
"; }
    ~Child3()
    {
        cout << "child 析构,,,
";
    }
    void c3_print()
    {
        //cout << "a b c is" << a << " " << b << " " << c << endl;
    }
    int a;
    int b;
    int c;
};
int main()
{
    
    Child3 c3;
    c3.a = 123;
    //c3.Child1::a = 123;
    //c3.c1_print();
    
    return 0;
}



//多线程
#include "iostream"
#include "thread"
using namespace std;

void functionw()
{
    cout<<"hello world"<<endl;
}

int main()
{
    
    thread t(functionw); //t()内为要在此线程执行的方法
    t.join();                       //t加入主线程,主线程等待他执行完毕再执行
    //t.detach();                //并发执行,和主线程同时执行,可能导致主线程执行完毕它                                 // 没有机会执行,并且被detach的不能在join,除非加判断入下
    
    // if(t.joinable())
    // {
    // t.join();
   //  }
    cout<<"住县城"<<endl;
    return 0;
}



 //多线程
#include "iostream"
#include "thread"
using namespace std;

void functionw()
{
    for(int i=0;i<10;i++)
    {
        cout<<"form t,i love u"<<endl;
    }
}

int main()
{
    thread t(functionw);//线程执行的另一种方式
    try
    {
        for(int i=0;i<10;i++)
        {
            cout<<"form main,i love u"<<endl;
        }
    }
    catch(...)
    {
        t.join();
        throw;  //保证t和main有一个执行
    }
}
 
 

//向量
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> iVec;
    cout << "容器 大小为: " << iVec.size() << endl;//取得大小
    cout << "容器 容量为: " << iVec.capacity() << endl; //1个元素, 容器容量为1
    iVec.push_back(1);//在后面存入一个数值
    cout << "容器 大小为: " << iVec.size() << endl;
    cout << "容器 容量为: " << iVec.capacity() << endl; //2个元素, 容器容量为2
    iVec.push_back(2);
    cout << "容器 大小为: " << iVec.size() << endl;
    cout << "容器 容量为: " << iVec.capacity() << endl; //3个元素, 容器容量为4
    iVec.push_back(3);
    cout << "容器 大小为: " << iVec.size() << endl;
    cout << "容器 容量为: " << iVec.capacity() << endl; //4个元素, 容器容量为4
    iVec.push_back(4);
    iVec.push_back(5);
    
    
    cout << "
容器 大小为: " << iVec.size() << endl;
    cout << "容器 容量为: " << iVec.capacity() << endl; //5个元素, 容器容量为8
    iVec.push_back(6);
    cout << "容器 大小为: " << iVec.size() << endl;
    cout << "容器 容量为: " << iVec.capacity() << endl; //6个元素, 容器容量为8
    iVec.push_back(7);
    cout << "容器 大小为: " << iVec.size() << endl;
    cout << "容器 容量为: " << iVec.capacity() << endl; //7个元素, 容器容量为8
    iVec.push_back(8);
    cout << "容器 大小为: " << iVec.size() << endl;
    cout << "容器 容量为: " << iVec.capacity() << endl; //8个元素, 容器容量为8
    iVec.push_back(9);
    cout << "容器 大小为: " << iVec.size() << endl;
    cout << "容器 容量为: " << iVec.capacity() << endl; //9个元素, 容器容量为16
    // vs2005/8 容量增长不是翻倍的,如
    // 9个元素   容量9
    // 10个元素 容量13
    //测试effective stl中的特殊的交换 swap()
    cout << "当前vector 的大小为: " << iVec.size() << endl;
    cout << "当前vector 的容量为: " << iVec.capacity() << endl;
    vector<int>(iVec).swap(iVec);
    cout << "临时的vector<int>对象 的大小为: " << (vector<int>(iVec)).size() << endl;
    cout << "临时的vector<int>对象 的容量为: " << (vector<int>(iVec)).capacity() << endl;
    cout << "交换后,当前vector 的大小为: " << iVec.size() << endl;
    cout << "交换后,当前vector 的容量为: " << iVec.capacity() << endl;
    return 0;
}

//vector迭代器
#include <iostream>
#include <vector>
using namespace std;
int main(){
int a[6]={1,2,3,4,5,6};
vector<int> b;
vector<int> c(a,a+4);
for(vector<int>::iterator it=c.begin();it<c.end();it++){
    b.push_back(*it);
    }
for(vector<int>::iterator it=b.begin();it<b.end();it++){
    cout<<*it<<endl;
    }
}

 
 


#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
    vector<int>obj;//创建一个向量存储容器 int
    for(int i=0;i<10;i++) // push_back(elem)在数组最后添加数据
    {
        obj.push_back(i);
        cout<<obj[i]<<",";
    }
    
    for(int i=0;i<5;i++)//去掉数组最后一个数据
    {
        obj.pop_back();
    }
    
    cout<<"
"<<endl;
    
    for(int i=0;i<obj.size();i++)//size()容器中实际数据个数
    {
        cout<<obj[i]<<",";
    }
    
    return 0;
}

//重载+
#include<iostream>
using namespace std;
class A
{
private:
    int a;
public:
    A();
    A(int n);
    A operator+(const A & obj);
    A operator+(const int b);
    friend A operator+(const int b, A obj);
    void display();
} ;
A::A()
{
    a=0;
}
A::A(int n)//构造函数
{
    a=n;
}
A A::operator +(const A& obj)//重载+号用于 对象相加
{
    return this->a+obj.a;
}
A A::operator+(const int b)//重载+号用于  对象与数相加
{
    return A(a+b);
}
A operator+(const int b,  A obj)
{
    return obj+b;//友元函数调用第二个重载+的成员函数  相当于 obj.operator+(b);
}
void A::display()
{
    cout<<a<<endl;
}
int main ()
{
    A a1(1);
    A a2(2);
    A a3,a4,a5;
    a1.display();
    a2.display();
    
    int m=1;
    a3=a1+a2;//可以交换顺序,相当月a3=a1.operator+(a2);
    a3.display();
    
    a4=a1+m;//因为加了个友元函数所以也可以交换顺序了。
    a4.display();
    
    a5=m+a1;
    a5.display();
}



//重载+运算符
#include <iostream>
using namespace std;

class Box
{
    double length;      // 长度
    double width;     // 宽度
    double height;      // 高度
public:
    
    double getVolume(void)
    {
        return length * width * height;
    }
    void setLength( double len )
    {
        length = len;
    }
    
    void setwidth( double bre )
    {
        width = bre;
    }
    
    void setHeight( double hei )
    {
        height = hei;
    }
    
     //改写部分 2018.09.05
     //重载 + 运算符,用于把两个 Box 对象相加
     //因为其是全局函数,对应的参数个数为2。
     //当重载的运算符函数是全局函数时,需要在类中将该函数声明为友员。
    friend Box operator+(const Box& a, const Box& b);
    
};

Box operator+(const Box& a, const Box& b)
{
    Box box;
    box.length = a.length + b.length;
    box.width = a.width + b.width;
    box.height = a.height + b.height;
    // cout << box.length << "--" << box.width << "--" << box.height << endl;
    return box;
}

// 程序的主函数
int main( )
{
    
    Box Box1;                // 声明 Box1,类型为 Box
    Box Box2;                // 声明 Box2,类型为 Box
    Box Box3;                // 声明 Box3,类型为 Box
    double volume = 0.0;     // 把体积存储在该变量中
    
    // Box1 详述
    Box1.setLength(6.0);
    Box1.setwidth(7.0);
    Box1.setHeight(5.0);
    
    // Box2 详述
    Box2.setLength(12.0);
    Box2.setwidth(13.0);
    Box2.setHeight(10.0);
    
    // Box1 的体积
    volume = Box1.getVolume();
    cout << "Volume of Box1 : " << volume <<endl;
    
    // Box2 的体积
    volume = Box2.getVolume();
    cout << "Volume of Box2 : " << volume <<endl;
    
    // 把两个对象相加,得到 Box3
    Box3 = Box1 + Box2;
    
    // Box3 的体积
    volume = Box3.getVolume();
    cout << "Volume of Box3 : " << volume <<endl;
    
    return 0;
}
 
 
 

//虚继承
#include <iostream>
using namespace std;
//基类

class D
{
public:
    D(){cout<<"D()"<<endl;}
    ~D(){cout<<"~D()"<<endl;}
protected:
    int d;
};

class B:virtual public D
{
public:
    B(){cout<<"B()"<<endl;}
    ~B(){cout<<"~B()"<<endl;}
protected:
    int b;
};

class A:virtual public D
{
public:
    A(){cout<<"A()"<<endl;}
    ~A(){cout<<"~A()"<<endl;}
protected:
    int a;
};

class C:public B, public A
{
public:
    C(){cout<<"C()"<<endl;}
    ~C(){cout<<"~C()"<<endl;}
protected:
    int c;
};

int main()
{
    cout << "Hello World!" << endl;
    C c;   //D, B, A ,C
    cout<<sizeof(c)<<endl;
    return 0;
}


//继承
#include <iostream>

using namespace std;

// 基类
class Shape
{
public:
    void setWidth(int w)
    {
        width = w;
    }
    void setHeight(int h)
    {
        height = h;
    }
protected:
    int width;
    int height;
};

// 派生类
class Rectangle: public Shape
{
public:
    int getArea()
    {
        return (width * height);
    }
};

int main(void)
{
    Rectangle Rect;
    
    Rect.setWidth(5);
    Rect.setHeight(7);
    
    // 输出对象的面积
    cout << "Total area: " << Rect.getArea() << endl;
    
    return 0;
}

//继承
#include <iostream>
using namespace std;
// 基类 Shape
class Shape
{
public:
    void setWidth(int w)
    {
        width = w;
    }
    void setHeight(int h)
    {
        height = h;
    }
protected:
    int width;
    int height;
};

// 基类 PaintCost
class PaintCost
{
public:
    int getCost(int area)
    {
        return area * 70;
    }
};

// 派生类
class Rectangle: public Shape, public PaintCost
{
public:
    int getArea()
    {
        return (width * height);
    }
};

int main(void)
{
    Rectangle Rect;
    int area;
    
    Rect.setWidth(5);
    Rect.setHeight(7);
    
    area = Rect.getArea();
    
    // 输出对象的面积
    cout << "Total area: " << Rect.getArea() << endl;
    
    // 输出总花费
    cout << "Total paint cost: $" << Rect.getCost(area) << endl;
    
    return 0;
}


//内联函数
#include <iostream>
using namespace std;
inline int Max(int x, int y)
{
    return (x > y)? x : y;
}

// 程序的主函数
int main( )
{
    
    cout << "Max (20,10): " << Max(20,10) << endl;
    cout << "Max (0,200): " << Max(0,200) << endl;
    cout << "Max (100,1010): " << Max(100,1010) << endl;
    return 0;
}
 
 


#include <iostream>
using namespace std;
class Box
{
public:
    // 构造函数定义
    Box(double l=2.0, double b=2.0, double h=2.0)
    {
        cout <<"Constructor called." << endl;
        length = l;
        breadth = b;
        height = h;
    }
    double Volume()
    {
        return length * breadth * height;
    }
    int compare(Box box)
    {
        return this->Volume() > box.Volume();
    }
private:
    double length;     // Length of a box
    double breadth;    // Breadth of a box
    double height;     // Height of a box
};

int main(void)
{
    Box Box1(3.3, 1.2, 1.5);    // Declare box1
    Box Box2(8.5, 6.0, 2.0);    // Declare box2
    if(Box1.compare(Box2))
    {
        cout << "Box2 is smaller than Box1" <<endl;
    }
    else
    {
        cout << "Box2 is equal to or larger than Box1" <<endl;
    }
    return 0;
}
 
 
 


#include <iostream>
using namespace std;
class Box
{
public:
    // 构造函数定义
    Box(double l=2.0, double b=2.0, double h=2.0)
    {
        cout <<"Constructor called." << endl;
        length = l;
        breadth = b;
        height = h;
    }
    double Volume()
    {
        return length * breadth * height;
    }
private:
    double length;     // Length of a box
    double breadth;    // Breadth of a box
    double height;     // Height of a box
};

int main(void)
{
    Box Box1(3.3, 1.2, 1.5);    // Declare box1
    Box Box2(8.5, 6.0, 2.0);    // Declare box2
    Box *ptrBox;                // Declare pointer to a class.
    // 保存第一个对象的地址
    ptrBox = &Box1;
    // 现在尝试使用成员访问运算符来访问成员
    cout << "Volume of Box1: " << ptrBox->Volume() << endl;
    // 保存第二个对象的地址
    ptrBox = &Box2;
    // 现在尝试使用成员访问运算符来访问成员
    cout << "Volume of Box2: " << ptrBox->Volume() << endl;
    return 0;
}

 
 
 //变量调用构造函数
#include <iostream>
using namespace std;

class Cpoint{
public:
    static int value;
    static int num;
    Cpoint(int x,int y){
        xp=x;yp=y;
        value++;
        cout << "调用构造:" << value << endl;
    }
    
    ~Cpoint(){num++; cout << "调用析构:" << num << endl;}
    
private:
    int xp,yp;
};

int Cpoint::value=0;
int Cpoint::num=0;
class CRect{
public:
    CRect(int x1,int x2):mpt1(x1,x2),mpt2(x1,x2){
        cout << "调用构造
";
    }
    ~CRect(){cout << "调用析构
";}
private:
    Cpoint mpt1,mpt2;
};

int main()
{
    CRect p(10,20);
    cout << "Hello, world!" << endl;
    return 0;
}
 
 


#include <iostream>
using namespace std;
class Box
{
public:
    static int objectCount;
    // 构造函数定义
    Box(double l=2.0, double b=2.0, double h=2.0)
    {
        cout <<"Constructor called." << endl;
        length = l;
        breadth = b;
        height = h;
        // 每次创建对象时增加 1
        objectCount++;
    }
    double Volume()
    {
        return length * breadth * height;
    }
private:
    double length;     // 长度
    double breadth;    // 宽度
    double height;     // 高度
};

// 初始化类 Box 的静态成员   其实是定义并初始化的过程
int Box::objectCount = 0;
//也可这样 定义却不初始化
//int Box::objectCount;
int main(void)
{
    Box Box1(3.3, 1.2, 1.5);    // 声明 box1
    Box Box2(8.5, 6.0, 2.0);    // 声明 box2
    
    // 输出对象的总数
    cout << "Total objects: " << Box::objectCount << endl;
    
    return 0;
}
 
 




#include <iostream>

using namespace std;

class Box
{
public:
    static int objectCount;
    // 构造函数定义
    Box(double l=2.0, double b=2.0, double h=2.0)
    {
        cout <<"Constructor called." << endl;
        length = l;
        breadth = b;
        height = h;
        // 每次创建对象时增加 1
        objectCount++;
    }
    double Volume()
    {
        return length * breadth * height;
    }
    static int getCount()
    {
        return objectCount;
    }
private:
    double length;     // 长度
    double breadth;    // 宽度
    double height;     // 高度
};

// 初始化类 Box 的静态成员
int Box::objectCount = 0;

int main(void)
{
    
    // 在创建对象之前输出对象的总数
    cout << "Inital Stage Count: " << Box::getCount() << endl;
    
    Box Box1(3.3, 1.2, 1.5);    // 声明 box1
    Box Box2(8.5, 6.0, 2.0);    // 声明 box2
    
    // 在创建对象之后输出对象的总数
    cout << "Final Stage Count: " << Box::getCount() << endl;
    
    return 0;
}

 



#include <iostream>
using namespace std;

int main()
{
    int i,j,k;   // p[2][3][4]
    
    int ***p;
    p = new int **[2];
    for(i=0; i<2; i++)
    {
        p[i]=new int *[3];
        for(j=0; j<3; j++)
            p[i][j]=new int[4];
    }
    
    //输出 p[i][j][k] 三维数据
    for(i=0; i<2; i++)
    {
        for(j=0; j<3; j++)
        {
            for(k=0;k<4;k++)
            {
                p[i][j][k]=i+j+k;
                cout<<p[i][j][k]<<" ";
            }
            cout<<endl;
        }
        cout<<endl;
    }
    
    // 释放内存
    for(i=0; i<2; i++)
    {
        for(j=0; j<3; j++)
        {
            delete [] p[i][j];
        }
    }
    for(i=0; i<2; i++)
    {
        delete [] p[i];
    }
    delete [] p;
    return 0;
}


#include <iostream>     // cout
#include <algorithm>    // unique, distance
#include <vector>       // vector
using namespace std;
bool myfunction (int i, int j) {
    return (i==j);
}

int main () {
    int myints[] = {10,20,20,20,30,30,20,20,10};           // 10 20 20 20 30 30 20 20 10
    vector<int> myvector (myints,myints+9);
    
    // using default comparison:
    vector<int>::iterator it;
    it = unique (myvector.begin(), myvector.end());   // 10 20 30 20 10 ?  ?  ?  ?
    //                ^
    for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << ' ' << *it;
    cout << '
';
    myvector.resize( distance(myvector.begin(),it) ); // 10 20 30 20 10
    
    // using predicate comparison:
    unique (myvector.begin(), myvector.end(), myfunction);   // (no changes)
    
    // print out content:
    cout << "myvector contains:";
    for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << ' ' << *it;
    cout << '
';
    
    return 0;
}




#include <iostream>
#include <vector>
#include <algorithm>


typedef unsigned long U32;

typedef vector < U32 >  VectorType;

int add(VectorType *pVector ,U32 m)
{
    if(NULL == pVector)
    {
        return -1;
    }
    
    pVector->push_back(m); //添加元素到末尾//无返回值
    
    return 0;
}


int remove(VectorType *pVector ,U32 m)
{
    if(NULL == pVector)
    {
        return -1;
    }
    
    VectorType::iterator iVector = find(pVector->begin(), pVector->end(), m);
    
    if(iVector != pVector->end())
    {
        pVector->erase(iVector); //参数只能是迭代器
        cout<<" erase success "<< m<<endl;
    }
    else
    {
        cout<<"  not find in pVector"<< m<<endl;
    }
    
    return 0;
}

int remove_last_one(VectorType *pVector )
{
    if(NULL == pVector)
    {
        cout<<"pVector is NULL,(remove_last_one)"<<endl;
        return -1;
    }
    
    if(pVector->empty())
    {
        cout<<"pVector is empty , remove_last_one"<<endl;
        return -1;
    }
    
    pVector->pop_back(); //删除最后一个元素
    return 0;
}

int remove_all(VectorType *pVector )
{
    if(NULL == pVector)
    {
        cout<<"pVector is NULL,(remove_all)"<<endl;
        return -1;
    }
    
#if 0
    VectorType::iterator iVector = pVector->begin();//;find(pVector->begin(), pVector->end(), m);
    
    while(iVector != pVector->end())
    {
        cout<<" remove_all a "<< (*iVector)<<endl;
        iVector = pVector->erase(iVector);
        cout<<" remove_all b "<< (*iVector)<<endl;
    }
    
#endif  //这种做法导致大量的移动操作,但好处是如果元素是指针,我们可以先把内存
    //释放了再删除
    //erase的返回值是指向删除元素的下一个元素的迭代器指针
    
#if 1
    while(pVector->size() != 0)
    {
        //pop_back方法无返回值
        pVector->pop_back();//删除操作避免大量移动的方法,如果元素有申请堆栈的内存,不可用此方法
    }
#endif
#if 0
    //以下方法不可行
    
    VectorType::iterator iVector = pVector->end();//;find(pVector->begin(), pVector->end(), m);
    
    while(iVector != pVector->begin())
    {
        cout<<" remove_all a "<< (*iVector)<<endl;
        iVector = pVector->erase(iVector);
        cout<<" remove_all b "<< (*iVector)<<endl;
    }
#endif
    return 0;
}

void dump(VectorType *pVector)
{
    if(NULL == pVector)
    {
        cout<<"pVector is NULL"<<endl;
        return ;
    }
    
    if(pVector->empty()) //判断是否为空
    {
        cout<<"pVector is empty"<<endl;
        return ;
    }
    
    VectorType::iterator iVector = pVector->begin();
    while(iVector != pVector->end())
    {
        cout<<" dump "<< (*iVector)<<endl;
        ++iVector;
    }
    
    return ;
}

int find(VectorType *pVector ,U32 m,int *pnFlag)
{
    if(NULL == pVector || NULL == pnFlag)
    {
        return -1;
    }
    
    *pnFlag = 0;
    VectorType::iterator iVector = find(pVector->begin(), pVector->end(), m);
    if(iVector != pVector->end())
    {
        cout<<" find success "<< m<<endl;
        *pnFlag = 1;
        return 0;
    }
    else
    {
        cout<<"  not find in pVector"<< m<<endl;
        return -1;
    }
}

int insert_one(VectorType *pVector ,U32 val, int iPlace)
{
    int i = 0;
    if(NULL == pVector || iPlace < 0)
    {
        cout<<"insert_one  param error"<<endl;
        return -1;
    }
    
    VectorType::iterator iVector = pVector->begin();
    while(iVector != pVector->end())
    {
        //cout<<" dump "<< (*iVector)<<endl;
        if(i == iPlace)
        {
            iVector = pVector->insert(iVector , val); //此时insert的返回值是迭代器,插入成功后iVector指向插入的位置
            cout<<" insert_one   after iVector point "<< (*iVector)<<endl;
            return 0;
        }
        
        i++;
        ++iVector;
    }
    
    iVector = pVector->insert(pVector->end() , val);
    
    return 0;
}

int print_size(VectorType *pVector)
{
    if(NULL == pVector)
    {
        cout<<"pVector is NULL"<<endl;
        return -1;
    }
    
    if(pVector->empty()) //判断是否为空
    {
        cout<<"pVector is empty  (get_size)"<<endl;
    }
    
    cout<<"pVector capacity: "<< pVector->capacity() <<  " size:"<<pVector->size()<<endl;
    
    return 0;
}

int main()
{
    VectorType vArray(20);//初始化20个元素的容器
    int nFlag =0;
    
    vArray.clear();//把容器的里的元素全部清掉
    
    //添加元素
    add(&vArray,5);
    add(&vArray,4);
    add(&vArray,3);
    add(&vArray,2);
    add(&vArray,1);
    add(&vArray,0);
    
    //插入一个元素 值为6
    U32 val = 6;
    int place = 2;
    insert_one(&vArray, val, place);
    
    //打印capatity   size
    print_size(&vArray);
    
    dump(&vArray);
    
    //删除末尾元素
    remove_last_one(&vArray);
    
    
    print_size(&vArray);
    
    
    U32 element = 1;
    find(&vArray ,element,&nFlag);
    
    remove(&vArray ,element);
    find(&vArray ,element,&nFlag);
    
    element = 8;
    //移除元素element
    remove(&vArray ,element);
    
    //查找element
    find(&vArray ,element,&nFlag);
    
    //移除所有元素
    remove_all(&vArray);
    
    dump(&vArray);
    
    print_size(&vArray);
    
    vector < U32 >().swap(vArray);//销毁向量的做法
    
    print_size(&vArray);
    
}
*/
原文地址:https://www.cnblogs.com/sea-stream/p/10892924.html