back to vectors

Started by
3 comments, last by neilski_2003 17 years, 5 months ago
Hi guys, I posted a couple of days ago about using vectors and the insert function etc and got told that although what i was doing would work it was also extremely odd and basically wrong in many ways. So i went back to the problem to try and be more direct and do exactly what the question asked. Only problem is now i get a whole new error. And i'm not entirely sure why. Anyways here is the code that is causing all the problems.
[source lang = "cpp"]

vector<Student_info> extract_fails_insert(vector<Student_info>& students)
{
	vector<Student_info>::size_type i = 0;
	// set up a variable to hold how many fails there have been
	vector<Student_info>::size_type passes = 0;
	//capture the size of the original vector for resizing later
	vector<Student_info>::size_type size = students.size();
	vector<Student_info>::iterator iter = students.begin();

	while (iter != students.end())
	{
		if (fgrade(*iter))
		{
			++iter;
		}
		else
		{
			++passes;
			students.insert(students.begin(), *iter);
		}
	}
	//adds the pass marks to the start of the student vector
	students.resize(passes);

	//returns the reduced student vector
	return students;
}


So basically what should be happening is this. I pass in a vector and then go through each element (using an iterator) to test whether it is a pass or a fail. If its a fail then it should just move on to the next element. If its a pass it should insert the record at the start of students and then continue. once it has made its way through students then it should resize it so that only the passes are left in the vector and then return this vector. Unfortunately when i compile that i get a "Vector Iterators Incompatible" error, and i have no idea why. It is obviously caused during the call to the insert function but i am not sure how i could sort this out. Maybe i could copy the current bit of info into a local variable and then place this into the insert function rather than using *iter. I don't know. So if anyone can shed any light on this it would be most appreicated. Cheers Neil
Advertisement
Inserting an element into the middle (or beginning) of a vector invalidates all iterators that point to elements after the insertion point. Your loop inserts elements into the beginning of the vector, and so after the first insertion, the iterator iter itself becomes invalid.

I'd use a second vector and insert the passes into it if I were you.

Zahlman's reply to your previous thread was based on the assumption that there were no additional constraints. At the time he wrote his reply you had not mentioned that this was a book exercise and that you had to use insert and resize. Since you do need to use insert and resize his last two bits of advice changing the underlying algorithm do not apply (although you should make sure you understand them for future reference as this is how you'd want to do things when you come to "real" coding), although his first three still do. Your old technique was a good way to do this using insert and resize.

If you do want to do this without a second vector, don't insert at the beginning. insert invalidates iterators, including the iterator you are using to walk through the container, hence your problem. Instead, insert at the end and walk through the vector with an index instead of an iterator.

Σnigma
cheers guys. I thought that would have been the problem - i thought the question was a bit strange to begin with as from what i had read this would happen (i.e the iterator would be invalidated)

Maybe i had i right in the first place. But as the other fella pointed out it seems a bit irish to do it that way as there are more direct routes of doing it. I guess they just want to get you using insert and resize so you know how, even if it isn't the most efficient way to do it.

Thanks again

Neil.
Oh and if i sounded ungrateful about Zahlmans help that was not my intent. Infact much the opposite. Whats the point in posting if you are not able to accept where you have gone wrong? After all theres a big difference between things working and things being efficient.

This topic is closed to new replies.

Advertisement