472,328 Members | 1,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

STL conatainer with a comparison function, what is the standard?

I have some code that builds fine on .Net 2001(?).. VC++ 7.0. But with
gcc 3.42 in MinGW and MS VC++ 6.0 it does not. I can understand VC++
not working, but isn't gcc standard yet? Here is the code, from what I
can tell from searching around, the function in the third term is ok in
some compilers, but not others. Can anyone recommend a free compliant
compiler, is Open Watcom?

vector<CIniFile::Record> content; // Used to hold the sorted content
vector<CIniFile::Record> sections = GetSections(FileName); // Get a
list of Sections
if(!sections.empty()) // Is there anything to process?
{
if(Descending) // Descending or Ascending?
std::sort(sections.begin(), sections.end(),
DescendingSectionSort());
else // Sort the Sections
std::sort(sections.begin(), sections.end(),
AcendingSectionSort());
.....
}

Jul 23 '05 #1
10 2353
Without seeing the declarations for DescendingSectionSort and
AcendingSectionSort... No idea what may be wrong.

Jul 23 '05 #2
tj*****@gmail.com wrote:
I have some code that builds fine on .Net 2001(?).. VC++ 7.0. But with
gcc 3.42 in MinGW and MS VC++ 6.0 it does not. I can understand VC++
not working, but isn't gcc standard yet? Here is the code, from what I
can tell from searching around, the function in the third term is ok in
some compilers, but not others. Can anyone recommend a free compliant
compiler, is Open Watcom?

vector<CIniFile::Record> content; // Used to hold the sorted content
vector<CIniFile::Record> sections = GetSections(FileName); // Get a
list of Sections
if(!sections.empty()) // Is there anything to process?
{
if(Descending) // Descending or Ascending?
std::sort(sections.begin(), sections.end(),
DescendingSectionSort());
else // Sort the Sections
std::sort(sections.begin(), sections.end(),
AcendingSectionSort());
....
}


Hmm, 'gcc' (you actually use 'g++', not 'gcc', to compile C++ progs)
is standards compliant. The 3rd arg to 'sort' must be a function
that itself takes 2 args. Your 'sort' statements are incorrect.
Here's a snip from the docs for 'sort':

<quote>
sort

template<class RanIt>
void sort(RanIt first, RanIt last);

template<class RanIt, class Pred>
void sort(RanIt first, RanIt last, Pred pr);

The first template function reorders the sequence designated by
iterators in the range [first, last) to form a sequence ordered
by operator<. Thus, the elements are sorted in ascending order.

The function evaluates the ordering predicate X < Y at most
ceil((last - first) * log(last - first)) times.

The second template function behaves the same, except that it
replaces operator<(X, Y) with pr(X, Y)
</quote>

You are using the 'second template' function; so, your
DescendingSectionSort() and AscendingSectionSort() must
each take 2 args of type CIniFile::Record (or ref's to same).

Somewhere should be something like this:

bool DescendingSectionSort(const CIniFile::Record& X,
const CIniFile::Record& Y)
{
// compare code here
}

The same type of code for AscendingSectionSort() goes here...

Then your sort statements would look like this:

std::sort(sections.begin(), sections.end(),
DescendingSectionSort);

Here's a complete simple example:

#include <iostream>
#include <vector>

struct Stuff
{
int a;
double b;

Stuff(int ia, double fb) : a(ia), b(fb) {}
};

bool DescStuff(const Stuff& s1, const Stuff& s2)
{
return s1.a > s2.a;
}

int main()
{
std::vector<Stuff> stuff;

stuff.push_back(Stuff(1, 1.0));
stuff.push_back(Stuff(2, 2.0));
stuff.push_back(Stuff(3, 3.0));

// sort 'stuff' into descending order on 'a'
std::sort(stuff.begin(), stuff.end(), DescStuff);

std::vector<Stuff>::iterator it;

// print the sorted 'stuff'
for (it = stuff.begin(); it != stuff.end(); it++)
std::cout << (*it).a << std::endl;

return 0;
}

Regards,
Larry
Jul 23 '05 #3
tj*****@gmail.com wrote:
I have some code that builds fine on .Net 2001(?).. VC++ 7.0. But with
gcc 3.42 in MinGW and MS VC++ 6.0 it does not. I can understand VC++
not working, but isn't gcc standard yet? Here is the code, from what I
can tell from searching around, the function in the third term is ok in
some compilers, but not others. Can anyone recommend a free compliant
compiler, is Open Watcom?

vector<CIniFile::Record> content; // Used to hold the sorted content
vector<CIniFile::Record> sections = GetSections(FileName); // Get a
list of Sections
if(!sections.empty()) // Is there anything to process?
{
if(Descending) // Descending or Ascending?
std::sort(sections.begin(), sections.end(),
DescendingSectionSort());
else // Sort the Sections
std::sort(sections.begin(), sections.end(),
AcendingSectionSort());
....
}


As I said in my earlier post, the syntax of your "sort"
statements is incorrect.

The way you have the "std::sort" statements written, with
"DescendingSectionSort()" and "AscendingSectionSort()",
should produce a compile-time error. There should not
be any "()" following "DescendingSectionSort" and
"AscendingSectionSort" in the "std::sort" statements.

The code should be:

if(Descending) // Descending or Ascending?
std::sort(sections.begin(), sections.end(),
DescendingSectionSort );
else // Sort the Sections
std::sort(sections.begin(), sections.end(),
AcendingSectionSort );

So, it is ".Net 2001(?).. VC++ 7.0" that is wrong (non-Standard),
and VC++ 6.0 and GCC which are correct (Standard).

Regards,
Larry
Jul 23 '05 #4
"Larry I Smith" <la***********@verizon.net> wrote in message
news:y5uqe.3435$2K4.1249@trnddc08...
tj*****@gmail.com wrote: As I said in my earlier post, the syntax of your "sort"
statements is incorrect.


They could be correct, if for example DecendingSectionSort were a functor,
defined something like:
struct DecendingSectionSort : public std::binary_function<CIniFile::Record,
CIniFile::Record, bool>
{
bool operator()(CIniFile::Record& lhs, const CIniFile::Record& rhs)
const
{
// compare lhs and rhs here and return ordering indicator
}
};


Jul 23 '05 #5
Phil Staite wrote:
"Larry I Smith" <la***********@verizon.net> wrote in message
news:y5uqe.3435$2K4.1249@trnddc08...
tj*****@gmail.com wrote:

As I said in my earlier post, the syntax of your "sort"
statements is incorrect.


They could be correct, if for example DecendingSectionSort were a functor,
defined something like:
struct DecendingSectionSort : public std::binary_function<CIniFile::Record,
CIniFile::Record, bool>
{
bool operator()(CIniFile::Record& lhs, const CIniFile::Record& rhs)
const
{
// compare lhs and rhs here and return ordering indicator
}
};


Yes, you are correct. Since GCC fails to compile the code,
I suspect that the OP's functions are not functors.

Regards,
Larry
Jul 23 '05 #6
They are local functions, not functors. .Net compiles fine, gcc(g++)
does not.

I have another function that will cause the same compile error.

bool SortCriterion(fooObject * O1, fooObject * O2) {

if (O1->GetFoo() <= O2->GetFoo())
return FALSE;
else
return TRUE;
};

This is then fed to sort as the third arguement. As I understand it the
standard can take a function like the one above as the compare
arguement, why does g++ not like it?

Jul 23 '05 #7
tj*****@gmail.com wrote:
They are local functions, not functors. .Net compiles fine, gcc(g++)
does not.

I have another function that will cause the same compile error.

bool SortCriterion(fooObject * O1, fooObject * O2) {

if (O1->GetFoo() <= O2->GetFoo())
return FALSE;
else
return TRUE;
};

This is then fed to sort as the third arguement. As I understand it the
standard can take a function like the one above as the compare
arguement, why does g++ not like it?


GCC is about a standard compliant as it gets...

It is .Net that is incorrect. There should be an option
to disable Microsoft-extensions. Disable those extensions
to force .NET to conform to the standard.

I don't know what 'TRUE' and 'FALSE' are (neither does the
standard). The C++ boolean keywords are 'true' and 'false'.

I included a working example and the doc for std::sort in my
original post. What is it about that doc that you do not
understand?

The 3rd arg must be a function that returns 'bool'
and takes 2 args of the type stored in the container
(or a ref to same).

If your container stores 'fooObject' instances, your code
would be:

bool SortCriterion(const fooObject& O1, const fooObject& O2)
{
if (O1.GetFoo() <= O2.GetFoo())
return false;
else
return true;
}

Does your container store 'fooObject' instances (fooObject)
or does it store 'fooObject' pointers (fooObject *)?

Provide some example code -and- the actual compiler error
messages generated by that example code.

Regards,
Larry
Jul 23 '05 #8
Larry I Smith wrote:

GCC is about a standard compliant as it gets...

It is .Net that is incorrect.


That's a rather strong statement in the absence of actual, complete code
that illustrates the problem. (By the way, G++ has many standard
conformance problems).

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #9
Pete Becker wrote:
Larry I Smith wrote:

GCC is about a standard compliant as it gets...

It is .Net that is incorrect.


That's a rather strong statement in the absence of actual, complete code
that illustrates the problem. (By the way, G++ has many standard
conformance problems).


I'm referring to STL; I should have been more clear.
MS has many non-portable extensions (the old "embrace
and extend" vendor lock in ploy). If trying to write
portable code, these extensions should be disabled
for MS compilers (using the /Za option).

Any GCC extensions should also be disabled (-ansi -pedantic).

The OP appears to be trying to write portable code
(.NET, VC 6, gcc).

I've asked the OP to post actual code and the matching
compiler errors.

Regards,
Larry
Jul 23 '05 #10
Larry I Smith wrote:
Pete Becker wrote:
Larry I Smith wrote:
GCC is about a standard compliant as it gets...

It is .Net that is incorrect.


That's a rather strong statement in the absence of actual, complete code
that illustrates the problem. (By the way, G++ has many standard
conformance problems).

I'm referring to STL;


That was clear.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #11

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

Similar topics

0
by: g | last post by:
Hi all. Maybe this question has been asked many times before, but I was not able to find any pointer. I apologize in advance, for it refers to a...
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin...
46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will...
14
by: Spoon | last post by:
Hello, I've come across the following comparison function used as the 4th parameter in qsort() int cmp(const void *px, const void *py) {...
6
by: Andrea B | last post by:
Hi everybody, a guy told me during a job interview that we can't compare directly 2 floats, because in very rare situations we can get wrong...
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I...
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. ...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.