I don't know where is the problem. The program crashes in this function. Can you help me?
I use these 2 functions for benchmarking (comparing containers speed with different techniques of using it). I use vector "studentai" with all students name and lastname in it. At vector "silpni" are students, which final score is >5. And, of course, "geri" with score <5. This function work well:
void atrinkimas_1(vector <duomenys>& studentai, vector <duomenys>& silpni, vector <duomenys>& geri)
{
sort(studentai.begin(), studentai.end(), tikrinimas_gal);
std::vector<duomenys>::iterator it = std::find_if(studentai.begin(),
studentai.end(), tikrinimas_5);
std::copy(it, studentai.end(), std::back_inserter(geri));
studentai.resize(studentai.size() - geri.size());
std: copy(studentai.begin(), it, std::back_inserter(silpni));
studentai.clear();
}
And this doesn't:
void atrinkimas_2(vector <duomenys>& studentai, vector<duomenys> &silpni)
{
sort(studentai.begin(), studentai.end(), tikrinimas_gal);
std::vector<duomenys>::iterator it = std::find_if(studentai.begin(), studentai.end(), tikrinimas_5);
std::copy(it, studentai.end(), std::back_inserter(silpni));
studentai.resize(studentai.size() - silpni.size());
}
What is the problem?
find_if, and I have no idea ifstd::copyis guaranteed to work whenfirstis a past the end iterator (I suspect it works, but don't want to assume).std:instead ofstd::could introduce a label, thencopyis found via ADL?). Typesduomenysand functiontikrinimas_5is missing. minimal reproducible example, and ensure it actually reproduces the error.std::partitionorstd::stable_partitionif this is your goal? Your code seems to be doing something that looks like an STL algorithm call or two would work, with more efficiency and no bugs (or very few bugs).