-4
#include <stdlib.h>
#include <stdio.h>  

vector <vector<string> > vector2;
vector<string> vector;

for(int i = 0; i < vector.size();i++)
{
    vector2[atoi(vector[i+1].c_str())].push_back(vector[i]);
    i++;
}

I try this on Windows, its working fine. But when i try to compile on ubuntu64-bits it has this segmentation error, anyone enlighten to?

Hitesh Vaghani
  • 1,342
  • 12
  • 31
user1745860
  • 207
  • 1
  • 5
  • 11

1 Answers1

3

From the code you've shown, vector[i+1].c_str() will read beyond the end of vector for the last iteration of the loop.

Should you exit your loop one iteration sooner?

for(int i=0; i<vector.size()-1;i++)

If this doesn't help, can you post a fuller example that demonstrates the problem? It isn't currently possible to say whether e.g. vector2 has sufficient capacity or vector's contents will always be convertible into suitable indexes.

simonc
  • 41,632
  • 12
  • 85
  • 103