473,884 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Fil eName); // Get a
list of Sections
if(!sections.em pty()) // Is there anything to process?
{
if(Descending) // Descending or Ascending?
std::sort(secti ons.begin(), sections.end(),
DescendingSecti onSort());
else // Sort the Sections
std::sort(secti ons.begin(), sections.end(),
AcendingSection Sort());
.....
}

Jul 23 '05 #1
10 2485
Without seeing the declarations for DescendingSecti onSort and
AcendingSection Sort... No idea what may be wrong.

Jul 23 '05 #2
tj*****@gmail.c om 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(Fil eName); // Get a
list of Sections
if(!sections.em pty()) // Is there anything to process?
{
if(Descending) // Descending or Ascending?
std::sort(secti ons.begin(), sections.end(),
DescendingSecti onSort());
else // Sort the Sections
std::sort(secti ons.begin(), sections.end(),
AcendingSection Sort());
....
}


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
DescendingSecti onSort() and AscendingSectio nSort() must
each take 2 args of type CIniFile::Recor d (or ref's to same).

Somewhere should be something like this:

bool DescendingSecti onSort(const CIniFile::Recor d& X,
const CIniFile::Recor d& Y)
{
// compare code here
}

The same type of code for AscendingSectio nSort() goes here...

Then your sort statements would look like this:

std::sort(secti ons.begin(), sections.end(),
DescendingSecti onSort);

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<Stu ff> 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<Stu ff>::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.c om 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(Fil eName); // Get a
list of Sections
if(!sections.em pty()) // Is there anything to process?
{
if(Descending) // Descending or Ascending?
std::sort(secti ons.begin(), sections.end(),
DescendingSecti onSort());
else // Sort the Sections
std::sort(secti ons.begin(), sections.end(),
AcendingSection Sort());
....
}


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
"DescendingSect ionSort()" and "AscendingSecti onSort()",
should produce a compile-time error. There should not
be any "()" following "DescendingSect ionSort" and
"AscendingSecti onSort" in the "std::sort" statements.

The code should be:

if(Descending) // Descending or Ascending?
std::sort(secti ons.begin(), sections.end(),
DescendingSecti onSort );
else // Sort the Sections
std::sort(secti ons.begin(), sections.end(),
AcendingSection Sort );

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@trndd c08...
tj*****@gmail.c om wrote: As I said in my earlier post, the syntax of your "sort"
statements is incorrect.


They could be correct, if for example DecendingSectio nSort were a functor,
defined something like:
struct DecendingSectio nSort : public std::binary_fun ction<CIniFile: :Record,
CIniFile::Recor d, bool>
{
bool operator()(CIni File::Record& lhs, const CIniFile::Recor d& 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@trndd c08...
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 DecendingSectio nSort were a functor,
defined something like:
struct DecendingSectio nSort : public std::binary_fun ction<CIniFile: :Record,
CIniFile::Recor d, bool>
{
bool operator()(CIni File::Record& lhs, const CIniFile::Recor d& 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(f ooObject * 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.c om 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(f ooObject * 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(c onst 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

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

Similar topics

0
2926
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 particular standard library implementation (GNU C++ version 3.x), but perhaps it's more general than that. The following program (to be compiled with g++ 3.x, 2.9x is not enough) #include <iostream> #include <string>
5
2542
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 interface. While portability (from a C perspective) is going to hell just by using dlopen()/LoadLibrary() respectively, I'm still trying to get it as clean as possible. I have a number of different quantums of data and a number of plugins. Since any plugin can (and possibly will) touch any quantum...
46
5202
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 an interview..I can write a small program but I was told that wouldn't it be wise to first get the length of the strings..if it doesn't match then they are not the same..I agreed...then he said..but again that would be an overhead first measuring the length...and then doing a character by...
37
2832
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 raise a TypeError instead of the current behaviour, which is returning a value which is, really, id({}) < id(). He also said that default equality comparison will continue to be identity-based. This means that x == y will never raise an exception, as is the situation is now. Here's his reason:
14
5032
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) { const double *x = px, *y = py; return (*x > *y) - (*x < *y); }
6
2444
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 results. I googled it, with no result. Do you know something about that? Bye, Andrea
0
2588
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 someone here knows the answer. What I try to do is sort the records in a plain-text index file based on certain columns. The index file consists of records and fields within the records. The individual fields are separated by semicolons, the records by newlines. The index file is loaded into memory...
1
6332
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 need to compare the size of a given file against a software buffer of size 3MB. This is needed so as to see which function to use to read from the file. As the files used range from very large (>30GB) to very small (<3MB), I have enabled large file support and I obtain the file size by using the...
20
2255
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. -------------------- code ----------------------------------- #include <stdio.h> int main (void) { char xx="abcd"; char * p1 = xx;
0
9954
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10769
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10869
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10426
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9591
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7985
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7137
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...

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.