Sign in to follow this  
popcorn

c++ sort

Recommended Posts

I am trying to use the sort function to sort some elements in a vector. I have two vectors so for each element in the first vector I want to sort 3 elements in the second vector therefore I have code like:
        vector<string>::const_iterator sIter;
	vector<double>::iterator dIter, start, end;

	start = homework.begin();
	end = homework.begin() + 3;

	for(sIter = students.begin(); sIter != students.end(); ++sIter)
	{		
		sort(start, end);

		start += 3;
		end += 3;
	}

However I generally get a debug assertion failure or something like vector iterator not dereferencable. Any ideas why?

Share this post


Link to post
Share on other sites
Are you sure that homework has no fewer than students.size() * 3 elements?

In any case, there are probably better ways to approach the problem. For example, you could have a Student class or struct, which itself stored the grades for that student as a vector. The students themselves could be stored in a std::map for easy sorting and querying.

There are plenty of other options as well, but in short, there shouldn't be any need to try and maintain and enforce a more or less ad hoc relationship between one container and another as you're doing (as you can see, among other things it's somewhat error prone).

Share this post


Link to post
Share on other sites
Yeah I'm aware there are other methods but I'm following a book and trying to only use the things shown in the chapter. Anyway I solved the problem -


for(sIter = students.begin(); sIter != students.end(); ++sIter)
{
sort(start, end);

if(!(end == homework.end()))
{
start = start + 3;
end = end + 3;
}
}



I had a feeling it had something to do with the iterators and at first I thought something was going wrong inside the sort but it seems to sort fine - basically the iterator cannot be greater than the number of items the vector contains(explains the assertion failure I think) which was what was happening inside the previous for loop code I posted up.

Thanks for replying though.

Share this post


Link to post
Share on other sites
You could also avoid the problem by not calculating and updating 'end' in that way, but instead at the time of the sort call (you don't even need the variable):


vector<double>::iterator start = homework.begin(); // initialize, dammit!

for (vector<string>::const_iterator sIter = students.begin();
sIter != students.end(); ++sIter) // and scope your variables, too
{
// The net effect is if you had incremented 'end' up here
// instead of at the bottom of the loop.
sort(start, start + 3);
}


although taking a structured approach certainly is better, and shame on your book for putting you through a complex example like this before teaching that kind of organizational habit.

I hope this gives you some good ideas about how to make good use of somewhat-complicated *expressions* to make your life easier, though. While I won't try to push you to Lisp-like extremes, try to avoid assigning to variables at every step (especially in a loop - except for changes made to the counter variable, of course).

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
You could also avoid the problem by not calculating and updating 'end' in that way, but instead at the time of the sort call (you don't even need the variable):


vector<double>::iterator start = homework.begin(); // initialize, dammit!

for (vector<string>::const_iterator sIter = students.begin();
sIter != students.end(); ++sIter) // and scope your variables, too
{
// The net effect is if you had incremented 'end' up here
// instead of at the bottom of the loop.
sort(start, start + 3);
}


although taking a structured approach certainly is better, and shame on your book for putting you through a complex example like this before teaching that kind of organizational habit.

I hope this gives you some good ideas about how to make good use of somewhat-complicated *expressions* to make your life easier, though. While I won't try to push you to Lisp-like extremes, try to avoid assigning to variables at every step (especially in a loop - except for changes made to the counter variable, of course).



sort(start, start + 3); is a nice way to express that. With regards to somewhat-complicated *expressions*, to me sometimes code in c++ looks quite confusing and I feel it is beneficial to break the code up into smaller visible steps but I guess it depends on how comfortable you are in expressing things within the language you are using, though I can see the code you posted up is better than mine. (Initialization is something that I will definitely try to keep doing in future). Out of curiosity what are Lisp-like extremes - not that I understand that language or anything?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this