C++ 什么';在C+中使用vector of vector的正确方法是什么+;?

C++ 什么';在C+中使用vector of vector的正确方法是什么+;?,c++,c++11,vector,stl,C++,C++11,Vector,Stl,我正试图找到正确的方法,将向量的向量与以下代码一起使用: #include <vector> using namespace std; void f(int size) { vector<int> v; v.reserve(size); v[0] = 1; // OK vector<vector<int> > vv; vv.reserve(size); // vv.pu

我正试图找到正确的方法,将向量的向量与以下代码一起使用:

#include <vector>
using namespace std;

void f(int size) {
      vector<int> v;
      v.reserve(size);

      v[0] = 1; // OK

      vector<vector<int> > vv;
      vv.reserve(size);

      // vv.push_back(v); // everything is OK with this

      vv[0].push_back(1); // Crash
      vv[0] = {1}; // Crash
}

int main() {
    f(3);
}
#包括
使用名称空间std;
空f(整数大小){
向量v;
v、 储备(规模);
v[0]=1;//确定
向量vv;
vv.储备(大小);
//v.推回(v);//这一切都好
vv[0]。推回(1);//崩溃
vv[0]={1};//崩溃
}
int main(){
f(3);
}

但我想知道为什么我不能像向量一样使用向量的向量?为什么我不能将vv(vector of vector)的成员直接与push_back a vector一起使用?

请参考更新和注释的示例。这可能有助于澄清:

#include <vector>
#include <stdexcept>

void f(int size) 
{
    // don't use using namespace standard at global scope.
    // if you must use it, use it at the tightest possible scope
    // for better, only bring in the names you need
    using std::vector;

    // creates an empty vector with size() == 0 and capacity() == 0
    vector<int> v;

    // this reserves storage, it does not create objects in v or extens its size()
    v.reserve(size);   
    // v.capacity() is now >= size
    // in this example, this step is actually un-necessary because...

    // ... a resize will increase capacity() if necessary
    v.resize(size);

    // there is still possible UB here, if size == 0.
    // we should check for that...
    if (v.size() < 1)
        throw std::out_of_range("size is less than 1");
    v[0] = 1; // This is now OK

    // ...which is essentially equivalent to this...
    v.at(0) = 1;

    // create an empty vector of vectors
    // vv.size() == vv.capacity() == 0
    vector<vector<int> > vv;
    vv.reserve(size);
    // now vv.size() == 0 and vv.capacity() >= size

    // this would result in:
    // vv.size() == 1, vv.capacity() >= max(vv.size(), size);
    // vv.push_back(v); // everything is OK with this

    if(1)
    {
        // at the moment, vv[0] results in UB because .size() is zero
        // so lets's add an entry in vv
        vv.resize(1);
        // vv.size() == 1, vv.capacity() >= max(vv.size(), size);

        // these are now valid
        vv[0].push_back(1); 
        vv[0] = {1}; 
    }
    else
    {
        // this would also be ok
        auto make_vector = [] {
            auto va = vector<int>();
            va.push_back(1);
            return va;
        };
        vv.push_back(make_vector());

        // as would this
        vv.emplace_back(std::vector({1}));
    }
}

int main() {
    f(3);
}
#包括
#包括
空f(整数大小)
{
//不要在全局范围内使用命名空间标准。
//如果必须使用它,请在尽可能小的范围内使用它
//为了更好,只带上你需要的名字
使用std::vector;
//创建一个大小为()且容量为()的空向量
向量v;
//这会保留存储空间,不会在v中创建对象或扩展其大小()
v、 储备(规模);
//v.容量()现在大于等于大小
//在这个例子中,这个步骤实际上是不必要的,因为。。。
//…如果需要,调整大小将增加容量()
v、 调整大小;
//如果size==0,这里仍然可能有UB。
//我们应该检查一下。。。
如果(v.size()<1)
抛出标准::超出范围(“大小小于1”);
v[0]=1;//现在可以了
//…这基本上等同于此。。。
v、 at(0)=1;
//创建向量的空向量
//vv.size()==vv.capacity()==0
向量vv;
vv.储备(大小);
//现在vv.size()==0和vv.capacity()>=size
//这将导致:
//vv.size()==1,vv.capacity()>=max(vv.size(),size);
//v.推回(v);//这一切都好
如果(1)
{
//目前,vv[0]导致UB,因为.size()为零
//让我们在vv中添加一个条目
调整尺寸(1);
//vv.size()==1,vv.capacity()>=max(vv.size(),size);
//这些现在是有效的
vv[0]。推回(1);
vv[0]={1};
}
其他的
{
//这也可以
自动生成_向量=[]{
自动va=向量();
va.推回(1);
返回va;
};
vv.推回(生成向量());
//就像这样
vv.emplace_back(std::vector({1}));
}
}
int main(){
f(3);
}

v[0]=1;//正常
不正常。向量的大小为0。使用
resize
,而不是
reserve
。几乎不需要为向量传递大小,它们是动态的,可以使用push_back()。是的,您可以随时调用resize(大小)。你到底想用vv[0]做什么呢?向后推(1)。请记住,您正在尝试将整数向量(vector)推入向量中。1不是矢量,而是整数。@tkausl谢谢。我滥用了保留。使用resize(),代码将按预期工作。@OmidCompSCI谢谢。是的,我应该使用调整大小。对于vv[0]。push_back(1),我的意思是将int 1推回到vv中的第一个向量,以确保vv[0]是可访问的。