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