我正在尝试遍历我的向量数组,但我似乎无法让它工作,我的错误出现在第二个 for 循环中,错误消息如下:
'double' 之前的预期主要表达式
我已经查看了如何迭代常规向量,但是如何迭代向量数组?我遵循了这个结构:
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i);
std::cout << "myvector contains:";
for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
我似乎无法让我的矢量数组版本工作。
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main(int arc, char *argv[])
{
vector<double> vector[7];
double num[7];
ifstream infile("data.txt");
string temp;
for(int i = 0; i <= 6; i++)
{
infile >> temp;
cout << temp << ' ';
}
cout << endl;
while(infile >> num[0] >> num[1] >> num[2] >> num[3] >> num[4] >> num[5] >> num[6])
{
for(int i = 0; i <= 6; i++)
{
vector[i].push_back(num[i]);
}
}
cout << endl;
for(int i = 0; i <= 6; i++)
{
// error on this line
// not sure what is wrong before vector<double>:: iterator it = vector[i].begin()
for(vector<double>::iterator it = vector[i].begin(); it != vector[i].end(); ++it)
{
cout << ' ' << *it;
}
}
return 0;
}