I'm trying to upgrade from C to C++, and I'm having a lot of issues to get used to it. In C, I'm used to work with pointers and linked list, but that is some tiresome job. Then, I was introduced to std::vectors
, and the world become prettier. Until I had to work with really huge vectors (a vector of 3 million of small vectors (usually some vector<short>
)). Then my nightmare started. The operation is simply (described below).
vector<vector<short>> v;
v = build(); \\create all 3 million of small vectors
for(int i = 0; i < v.size(); i++){
if(someReallyFastTest(v[i])){
v.erase(v.begin() + i);
i--;
}
}
The program does its purpose, but takes days to finish. I tried to depurate the code and then I notice that the command
v.erase(v.begin() + i);
takes seconds to complete the task. Since in each turn I have to erase thousands of elements, it take hours to complete the task. I don't want to go back and work with pointers and linked list so soon. Is there any faster way to do this task?