c++中怎样往vector中添加对象??
来源:百度知道 编辑:UC知道 时间:2024/05/23 13:22:17
怎样往vector中添加对象啊?下面这样为什么错阿?
int main(){
vector<cat> cats;//cat是先前定义好的一个类
cats.push_back(new cat());
}
应该怎么写啊
int main(){
vector<cat> cats;//cat是先前定义好的一个类
cats.push_back(new cat());
}
应该怎么写啊
//push_back()的参数为const的.
//你用vector保存的对象相当于一个数组对象了.
#include <iostream>
#include <vector>
using namespace std;
class cat
{
protected:
int a;
public:
void set(int a)
{
this->a = a;
}
};
int main()
{
int a =0;
vector<cat> vc(8);
vc[0].set(a);
return 0;
}
楼上正解