前言
由于vector重载了下标运算符,且vector所储存的数据是连续存储,因此我们可以将vector参数直接当作一维数组来使用,但二维vector能否直接转换成二维数组来使用呢?
不可以。
vector转一维数组
首先,参考vector转一维数组的情况:
using std::vector;
template <typename T>
T *vector2array(vector<T> v)
{
return (T *)&v[0];
}
再来看看vector的内存占用:
void sizeofvector()
{
vector<int> v;
vector< vector<int> > vv;
cout<<"==1==\nsizeof(vv): "<<sizeof(vector< vector<int> >)<<" "<<sizeof(vv)<<endl;
cout<<"sizeof(v): "<<sizeof(vector<int>)<<" "<<sizeof(v)<<endl;
for(int i(0); i < 100; i++)
v.push_back(i);
for(int i(0); i <