【问题标题】:Getting out-of-range error when trying to create and populate a vector using a for loop (C++)尝试使用 for 循环 (C++) 创建和填充向量时出现超出范围的错误
【发布时间】:2014-06-03 07:32:41
【问题描述】:

我正在尝试创建一个向量,其中每个元素都是 1000 以下的 3 的倍数。我尝试了两种方法,其中只有一种有效。不起作用的方式是:

int main() {
    vector<int> multiples_of_three;
    for (int i = 0; i <= 1000/3; ++i)
        multiples_of_three[i] = 3*i;
        cout << multiples_of_three[i] << "\n";
}

这给出了一个超出范围的错误,特别是在 multiples_of_three[i] 上。下一段代码起作用了:

int main() {
    vector<int> multiples_of_three(334);
    for (int i = 0; i <  multiples_of_three.size(); ++i) {
        multiples_of_three[i] = 3*i;
        cout <<  multiples_of_three[i];
}

因此,如果我定义了向量的大小,我可以将其保持在它的约束范围内。为什么如果我尝试让 for 循环决定元素的数量,我会得到超出范围的错误?

谢谢!

【问题讨论】:

  • 直接分配可能有效也可能无效,通常您会调用push_back,它会检查是否需要调整大小,然后创建一个足够大的新向量以进行分配,并将成员复制到新的向量
  • 我觉得问题在这里得到了解答。 stackoverflow.com/questions/11007054/…

标签: c++ vector outofrangeexception


【解决方案1】:

这很好用:

#include <iostream>
#include <vector>
using namespace std;

//this one is the edited version

int main() {
    vector<int> multiples_of_three(334);      //notice the change: I declared the size
    for (int i = 0; i <= 1000 / 3; ++i){
        multiples_of_three[i] = 3 * i;
        cout << multiples_of_three[i] << "\n";
    }
    system("pause");
}


Consider these two examples below:

//=========================the following example has errors =====================
int main() {

    vector<int> multiples_of_three;
    multiples_of_three[0] = 0;  // error
    multiples_of_three[1] = 3;  // error

    cout << "Here they are: " << multiples_of_three[0]; cout << endl;
    cout << "Here they are: " << multiples_of_three[1]; cout << endl;

    cout << endl;
    system("pause");

return 0;
}
//============================the following example works==========================

int main() {
    vector<int> multiples_of_three;
    multiples_of_three.push_back(0);
    multiples_of_three.push_back(3);

    cout << "Here they are: " << multiples_of_three[0]; cout << endl;
    cout << "Here they are: " << multiples_of_three[1]; cout << endl;

    cout << endl;
    system("pause");
return 0;
}

因此,除非您已经声明了大小,否则永远不要直接使用索引来分配值(如第一个示例中所示)。但是,如果已经分配了值,则可以使用索引来检索值(如第二个示例中所示)。如果您想使用索引来分配值,请首先声明数组的大小(与编辑版本一样)!

【讨论】:

    【解决方案2】:

    默认构造函数(在此处调用:vector&lt;int&gt; multiples_of_three;)创建一个空向量。您可以使用push_back 填充它们,或者如果您知道必须添加的对象数量则更好,将该数字传递给构造函数,因此它会立即保留所需的内存量而不是不断增长(这意味着分配内存和复制旧的记忆进入新的)向量。

    另一种选择是从空的默认构造向量中调用reserve 并使用push_back 填充它。 reserve 保留足够的内存来保留所需数量的对象,但不会更改向量的大小。 reserve 的优点是不会为每个对象调用默认构造函数(因为它将使用 resize 或参数化构造函数完成),因为您在创建后立即在初始化循环中覆盖了对象向量。

    【讨论】:

      【解决方案3】:

      您需要使用push_back() 而不是通过the indexer 添加。

      The indexer 只能用于对边界内的向量进行读/写访问。

      【讨论】:

        【解决方案4】:

        矢量不会因为使用[] 而神奇地变大。在第一个示例中它以 0 个元素开始,但您从未增长过它。

        【讨论】:

          猜你喜欢
          • 2015-02-08
          • 1970-01-01
          • 1970-01-01
          • 2021-08-05
          • 1970-01-01
          • 2014-07-20
          • 2011-09-07
          • 2023-03-13
          • 2021-09-02
          相关资源
          最近更新 更多