【问题标题】:dynamically allocating vector containing vector to be allocated dynamically动态分配包含要动态分配的向量的向量
【发布时间】:2020-06-15 12:26:26
【问题描述】:

我正在学习 C++,在练习 Hackerrank 时,我第一次遇到了包含向量的向量。 This 是要解决的问题。 从下面提供的程序中,我想知道:

  1. 是否像我一样声明所需向量的正确方法?
  2. 第 27 行的语句“a.resize(i)”是否按我的预期工作?
#include<iostream>
#include<vector>
#include<array>
bool inrange(int min, int max, int x)
{
    //check if x is in range
    return (x >= min && x <= max);
}

int main(void)
{
    int q{}, n{}; //for no. of queries & no. of elements in array
    std::cin >> q >> n;
    if (inrange(1, 100000, q)) //ensure q to be in specified range
    {
        if (inrange(1, 100000, n))
        {
            //for declaring vector of vectors
            using innertype = std::vector<int>;
            std::vector <innertype> a;
            //array to store no. of elements in each ith array
            std::vector <int> ki;

            for (int i{ 0 }; i < n; ++i)
            {
                //extend vector by one element
>line 27                a.resize(i);
                //get the array for ith position
                int j{ 0 }; char buffer;
                do
                {
                    //extend vector at ith index by one element
                    a[i].resize(j);
                    std::cin >> a[i][j];
                    std::cin >> buffer;
                    ++j;
                } while (buffer != '\n');
                ki.resize(j);
                ki[i] = j;
            }
            //take indexes for query1 and query2 to print requested elements
            int i{}, j{};
            std::cin >> i >> j;
            std::array q1request{ i,j };
            std::cin >> i >> j;
            std::array q2request{ i,j };
            //print elements "a[i][j]"
            std::cout << a[q1request[i]][q1request[j]] << '\n';
            std::cout << a[q1request[i]][q2request[j]];

        }
    }

    return 0;
}

程序在接受两个输入后终止。

调试断言失败! 表达式:向量下标超出范围

【问题讨论】:

    标签: c++ vector visual-c++ dynamic-memory-allocation


    【解决方案1】:

    除了索引之外,您对vector 的使用并没有什么问题(可能有点奇怪,但没有错)。 vector 的调整大小方法的使用被误用。对于向量的预分配,一般使用两种方法。 resize()reserve()

    如果您需要索引vector,可以使用一个。

    std::vector<int> test(10, 0); // creating vector of 10 items set to 0
    test.resize(100); // resizing to allocate space for 100 elements
    // test.size() == 100
    for(size_t i = 0; i < test.size(); ++i)
    {
     test[i] = i;
     std::cout << "Filling " << i << "th element." << std::endl;
    }
    

    另一个通常在不需要索引时使用。

    std::vector<int> test;
    test.reserve(100); // reserve space for at least 100 elements
    // test.size() == 0
    for(size_t i = 0; i < 100; ++i)
    {
     test.push_back(i); // will not allocate new memory
    }
    

    您遇到的问题是您在第一次迭代中调整向量的大小以容纳 0 个元素,然后访问索引 0 处的元素。对于任何其他后续索引也是如此。

    【讨论】:

    • 知道了。但我需要澄清一下为什么您更喜欢使用“size_t”代替“int”,因为它可以帮助我了解更多。
    • 在第一种情况下,它用于避免类型转换的警告 - size() 方法返回 size_t 类型的值。在第二个示例中,它只是一个复制粘贴。 size_t 只是 unsigned long long 的使用。通常,您至少希望使用unsigned 数据类型进行索引,但也有一些例外。
    • 在进行更改 ("a.resize(i+1)") 后,我通过监视窗口观看“缓冲区”发现,它要求用户提供另一个输入。但我希望它在输入整数后获得先前留在 std::cin 缓冲区中的“空白”字符。我错过了什么?
    • 您真的应该将向量的resize() 放在循环之外(将其调整为n)。第二个问题实际上是调试和单步调试代码的案例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 2020-06-20
    • 1970-01-01
    • 2022-01-10
    • 2016-08-11
    • 2014-06-16
    • 2017-03-10
    相关资源
    最近更新 更多