質問

#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?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top