【问题标题】:What's the right way to use vector of vector in C++?在 C++ 中使用向量的正确方法是什么?
【发布时间】:2019-03-18 03:06:11
【问题描述】:

我正在尝试通过以下代码找出使用向量向量的正确方法:

#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);
}

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

【问题讨论】:

  • v[0] = 1; // OK不行好的。向量的大小为 0。使用 resize,而不是 reserve
  • 几件事,真的不需要为向量传递大小,它们是动态的,只需使用 push_back()。是的,您可以随时调用 resize(size)。你到底想用 vv[0].push_back(1) 做什么。请记住,您正在尝试将整数向量 (vector) 推入向量中。 1 不是一个向量 而是一个整数。
  • @tkausl 谢谢。我滥用了储备金。使用 resize(),代码按预期工作。
  • @OmidCompSCI 谢谢。是的,我应该使用调整大小。使用 vv[0].push_back(1),我的意思是 push_back int 1 到 vv 中的第一个向量,以确保 vv[0] 是可访问的。

标签: c++ c++11 vector stl


【解决方案1】:

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

#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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    相关资源
    最近更新 更多