Problem with std::list and sorting

J

Jef Driesen

The number of items in my std::list is changed after sorting (using
std::list member function sort). I'm using MSVC6 and the actual (pseudo)
code is posted below. The output from size() is different before and
after the sort. If I remove the sorting (and nothing else), both numbers
remain the same.

int nmerged = 0;
do {
std::list<mum_pair> mregions;

// Add many items using push_back
mregions.push_back(...);

std::cout << mregions.size() << std::endl;
if (mregions.size()) {
// Sort the list
mregions.sort();
// Output first and last element.
std::cout << mregions.front() << "\t"
<< mregions.back() << std::endl;
}
std::cout << mregions.size() << std::endl;

// Update iteration count.
nmerged = ...;
} while (nmerged);

An item in the list is an std::pair with an additional property lambda,
that is used as sorting criteria.

struct mum_pair : public std::pair<mum_region*,mum_region*> {
double lambda;
mum_pair(mum_region*const A, mum_region*const B)
: std::pair<mum_region*,mum_region*>(A,B), lambda(0.0) {};
mum_pair(mum_region*const A, mum_region*const B, double lambda)
: std::pair<mum_region*,mum_region*>(A,B), lambda(lambda) {};
bool operator<(const mum_pair& rhs) const
{
return (lambda > rhs.lambda); // Sort descending.
}
};
 
J

Jef Driesen

Jef said:
The number of items in my std::list is changed after sorting (using
std::list member function sort). I'm using MSVC6 and the actual (pseudo)
code is posted below. The output from size() is different before and
after the sort. If I remove the sorting (and nothing else), both numbers
remain the same.

int nmerged = 0;
do {
std::list<mum_pair> mregions;

// Add many items using push_back
mregions.push_back(...);

std::cout << mregions.size() << std::endl;
if (mregions.size()) {
// Sort the list
mregions.sort();
// Output first and last element.
std::cout << mregions.front() << "\t"
<< mregions.back() << std::endl;
}
std::cout << mregions.size() << std::endl;

// Update iteration count.
nmerged = ...;
} while (nmerged);

An item in the list is an std::pair with an additional property lambda,
that is used as sorting criteria.

struct mum_pair : public std::pair<mum_region*,mum_region*> {
double lambda;
mum_pair(mum_region*const A, mum_region*const B)
: std::pair<mum_region*,mum_region*>(A,B), lambda(0.0) {};
mum_pair(mum_region*const A, mum_region*const B, double lambda)
: std::pair<mum_region*,mum_region*>(A,B), lambda(lambda) {};
bool operator<(const mum_pair& rhs) const
{
return (lambda > rhs.lambda); // Sort descending.
}
};

This is probably a bug in MSVC6. The problem does not exist in MSVC .NET
2003.
 
?

=?iso-8859-1?B?Sm9hcXXtbiBNIEzzcGV6IE118W96?=

Jef said:
The number of items in my std::list is changed after sorting (using
std::list member function sort). I'm using MSVC6 and the actual (pseudo)
code is posted below. The output from size() is different before and
after the sort. If I remove the sorting (and nothing else), both numbers
remain the same.

int nmerged = 0;
do {
std::list<mum_pair> mregions;

// Add many items using push_back
mregions.push_back(...);

Is the "many" more than 32,768? If so, please check

http://www.dinkumware.com/vc_fixes.html

paragraph Fix to <list>, for the likely cause of your problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top