+ -
当前位置:首页 → 问答吧 → c++中关于向量的一个问题

c++中关于向量的一个问题

时间:2011-12-17

来源:互联网

我们知道向量是这样表示的:vector<int> a(10),或者vector<int> a(10,1);等等方式表示的,今天看到下面的代码还是有点不明白。
先定义了一个vector<vector<int> > a;,在.cpp中出现了“a.resize(3 ,vector<int>(5));” 这样的代码,以及“vector<vector<int> > b(a[0].size() ,vector<int>(a.size()));”这样的代码,为何在“vector<int>(5)” 这样的地方没有一个变量?这下实在没有搞懂。在此请教大侠们多指点!

作者: wuleeemail   发布时间: 2011-12-17

vector(size_type __n, const value_type& __value = value_type(),
const allocator_type& __a = allocator_type())
  : _Base(__n, __a)
这里后两个参数带有默认值,你可以不提供的

作者: qscool1987   发布时间: 2011-12-17


resize(3 ,vector<int>(5));
===
vector<int> tmp(5);
resize(3 ,tmp);

作者: mingliang1212   发布时间: 2011-12-17

引用 2 楼 mingliang1212 的回复:
resize(3 ,vector<int>(5));
===
vector<int> tmp(5);
resize(3 ,tmp);

不是这样的,应该是
vector<int> temp(5,0);

作者: qscool1987   发布时间: 2011-12-17

看走眼了,。。。
vector<int> temp(5,0)和vector<int> temp(5)一个意思

作者: qscool1987   发布时间: 2011-12-17