[英]Working with values from array, containing multiple vectors
So basically I have created an array containing two vectors, and I want to be able to change/output values. 所以基本上我创建了一个包含两个向量的数组,并且我希望能够更改/输出值。 I have read that the following syntax should work, but when I run the program, it just keeps filling up the memory without printing anything.
我读过下面的语法应该起作用,但是当我运行该程序时,它只是不断地填满内存而不打印任何内容。
#include <iostream>
#include <array>
#include <vector>
int main() {
std::array<std::vector<float>, 2> endPieceV;
for (int i = 0; i < 20; i+2) {
endPieceV[0].push_back(i);
endPieceV[1].push_back(i*i+2);
}
std::cout << "Vector 1:" << std::endl;
for (int i = 0; i < endPieceV[0].size(); i++) {
std::cout << endPieceV[0][i] << " ";
}
std::cout << std::endl;
std::cout << std::endl;
std::cout << "Vector 2:" << std::endl;
for (int i = 0; i < endPieceV[0].size(); i++) {
std::cout << endPieceV[1][i] << " ";
}
system("pause");
return 0;
}
I don't understand why it doesn't start printing, but just keeps loading? 我不明白为什么它不开始打印,而只是继续加载?
This loop counter is is incorrect: 该循环计数器不正确:
for (int i = 0; i < 20; i+2)
You're not incrementing i
in this loop. 您没有在此循环中递增
i
。
Another potential error is that you're using the wrong vector as the loop counter: 另一个潜在的错误是您使用错误的向量作为循环计数器:
for (int i = 0; i < endPieceV[0].size(); i++) {
// ^ Wrong, should be 1
std::cout << endPieceV[1][i] << " ";
Even though you mentioned that the vectors are the same size, this is really not the way to write a loop like this. 即使您提到向量的大小相同,实际上也不是编写这样的循环的方法。
You are not incrementing i in first loop 您没有在第一个循环中递增i
for (int i = 0; i < 20; i+2) {
endPieceV[0].push_back(i);
endPieceV[1].push_back(i*i+2);
}
Notice i+2
should be i+=2
注意
i+2
应该是i+=2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.