访问整数和向量对中的元素

时间:2014-04-02 16:00:39

标签: c++ stdvector std-pair

在下面的代码中,我在最后一行收到了段错误:

int MAX_ITER = 4, n = 5;
vector< pair<int, vector<int> > > InputVector(MAX_ITER);

srand((unsigned)time(NULL));

for (int i = 0; i < MAX_ITER; i++) 
  InputVector[i].second.resize(n);

for (int i = 0; i < MAX_ITER; i++) {
  InputVector[i].first = i+1;
  for (int j = 0; j < InputVector[i].second.size(); i++) 
    InputVector[i].second[j] = rand()%2;

如何访问InputVector[i].second的第j个元素?

1 个答案:

答案 0 :(得分:3)

for (int j = 0; j < InputVector[i].second.size(); i++) 
                                                  ^^ should be j++