473,322 Members | 1,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem with std::list and sorting

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.
}
};
Jul 22 '05 #1
3 2110
Jef Driesen wrote:
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.
Jul 22 '05 #2
Jef Driesen wrote:
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.
--
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo

Jul 22 '05 #3
Joaquín M López Muñoz wrote:
Jef Driesen wrote:
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.


In my case "many" was more than 32,768. This bugfix does fix my problem.
Thanks for your answer.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
5
by: JustSomeGuy | last post by:
Can you access elements of the std::list like they were an array? eg. std::list<int> xlist; int y; .... y = xlist; // Returns the 10th element of a list.
11
by: velthuijsen | last post by:
I tried taking a list and pass it through std::sort like the following: sort(Unsorted.begin(), Unsorted.end()); I got an error back stating that the list iterator doesn't have a binary...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
5
by: matthias_k | last post by:
Hi, I need to sort elements of a std::list using a function predicate, something like: bool predicate( const& M m1, const& M m2 ) { return m1.somedata < m2.somedata; } I tried to call...
1
by: vertigo | last post by:
Hello I have: std::list<ObjPtr *> mylist; i wanted to sort that pointers using my own sorting function: mylist.sort(MySortingFunction); I can't create that function, i receive many errors :(...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.