动态分配的vector指针
vector<int> *get_num(int n)
{
vector<int> *pv = new vector<int>(n+1);
int i , x;
for(i = 0; i < n; i++)
{
cin>>x;
<span style="background-color: rgb(255, 0, 0);">(*pv)[i] = (x);
</span> }
return pv;
}
如果是动态分配的vector指针,那么当为vector中插入元素时只能用数组的方法(上述代码红色),如果用.push_back()函数的方法,就会出错,不知道为什么?
使用shared_ptr的vector指针
shared_ptr<vector<int>> get_num(int n)
{
shared_ptr<vector<int>> pv = make_shared<vector<int>>();
int i , x;
for(i = 0; i < n; i++)
{
cin>>x;
<span style="color:#ff0000;">(*pv).push_back(x);</span>
}
return pv;
}
如果是使用shared_ptr的vector指针,那么当为vector中插入元素时只能用.push_back的方法(上述代码红色),如果用数组插入的方法,就会出错,正好和动态分配的vector指针相反 , 不知道为什么?