C++ 尝试使用for循环(C+;+;)创建和填充向量时出现超出范围错误

C++ 尝试使用for循环(C+;+;)创建和填充向量时出现超出范围错误,c++,vector,outofrangeexception,C++,Vector,Outofrangeexception,我试图创建一个向量,其中每个元素都是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"; }

我试图创建一个向量,其中每个元素都是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";
}
因此,如果我定义了向量的大小,我可以将它保持在它的约束范围内。为什么如果我尝试让for循环指定元素的数量,我会得到一个超出范围的错误

谢谢

您需要使用而不是添加via


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

向量的大小不会神奇地增长,因为您使用的是
[]
。在第一个示例中,它是从0个元素开始的,而您从未对它进行过扩展。

默认构造函数(此处称为:
vector multiples\u of三;
)创建了一个空向量。您可以使用
push_back
或更好的方法填充它们,如果您知道要添加的对象的数量,则将该数量传递给构造函数,这样它可以立即保留所需的内存量,而不是不断增加(这意味着分配内存并将旧的menory复制到新的)向量


另一种方法是从空的默认构造向量调用
reserve
,并使用
push_back
填充它<代码>保留保留足够的内存以保留所需数量的对象,但不更改向量的大小。
reserve
的优点是,不会为每个对象调用默认构造函数(如
resize
或参数化构造函数),这是不必要的,因为您在创建向量后立即覆盖初始化循环中的对象。

这非常有效:

#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;
}
#包括
#包括
使用名称空间std;
//这是经过编辑的版本
int main(){
向量乘以三(334);//注意变化:我声明了大小

对于(int i=0;i Direct assignment可能有效,也可能无效),通常您会调用
push_back
,检查是否需要调整大小,然后创建一个足够大的新向量以进行assignment,并将成员复制到新向量。我觉得问题在这里得到了回答。
#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;
}