C++ vector 使用类作为模板参数
#include<iostream>
#include<string>
#include<vector>class vector3D
{public:float x,y,z; vector3D(int x_,int y_,int z_):x(x_),y(y_),z(z_){}public:float add(){return (x+y+z);}};int main(){int *a = new int[10];a[0] = 1;a[1] = 2;std::cout<<a[0]<<" "<<a[1]<<std::endl;delete []a;vector3D vectorArr(1,2,3);std::cout<<"x:"<<vectorArr.x<<"y:"<<vectorArr.y<<"z:"<<vectorArr.z<<std::endl;vector3D vectorArr1(2,4,6);vector3D vectorArr2(4,5,9);std::vector<vector3D> b;b.push_back(vectorArr);b.push_back(vectorArr1);b.push_back(vectorArr2);for(int i = 0;i < b.size();i++){std::cout<<"b.x = "<<b[i].x << " " << "b.y = "<<b[i].y<<" "<< "b[i].z = " << b[i].z << std::endl; std::cout << "Sum:" << b[i].add() << std::endl;}std::cout<<std::endl;
}
c++多态展示
#include<iostream>
#include<vector>using namespace std;class Base{
public:Base(){}public:virtual void getClassName(){cout<<"class:Base!"<<endl; }
};class A_class:public Base{public:A_class(){}public:void getClassName(){cout<<"class:A"<<endl;}
};class B_class:public Base{public:B_class(){}public:void getClassName(){cout<<"class:B"<<endl;}};int main(){A_class *a = new A_class();
// a->getClassName();B_class *b = new B_class();Base *base = b;base->getClassName(); Base *base1 = a;base1->getClassName(); }