Question

In the following code, I receive segfault at the last line:

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;

How to access jth element of InputVector[i].second?

Was it helpful?

Solution

for (int j = 0; j < InputVector[i].second.size(); i++) 
                                                  ^^ should be j++
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top