Mega Search
23.2 Million


Sign Up

Make a donation  
STL Function object.  
News Group: borland.public.cppbuilder.language.cpp

 Hello Everyone,


 I wish to sort a two dimensional array by one field.


Here is the array;

struct stringlist{
 AnsiString filename;
 int str;
 int points;
 }stringlist1[11][2000];


Incorrect function object and sort command;

 struct stringlists{

 bool operator() (const stringlist& lhs, const stringlist& rhs) const
 {

  return (lhs.str < rhs.str);

 }

};

std::sort(stringlist1 , stringlist1 + 2, stringlists());



Example input data;

               i    j
stringlist1[0][1].filename = "file2";
stringlist1[0][1].str         = 2;
stringlist1[0][1].points    = 8;

stringlist1[0][2].filename = "file2";
stringlist1[0][2].str         = 1;
stringlist1[0][2].points    = 11;

stringlist1[1][1].filename = "file1";
stringlist1[1][1].str         = 2;
stringlist1[1][1].points    = 8;

stringlist1[1][2].filename = "file1";
stringlist1[1][2].str         = 1;
stringlist1[1][2].points    = 11;


Example output data;

 I wish to sort the jth elements by the str field and leave the ith elements 
unsorted as they
are in correct order. Note "filename" is the same for each value of  i, and 
is not sorted,
but str and points are sorted by str.

               i    j
stringlist1[0][1].filename = "file2";
stringlist1[0][1].str         = 1;
stringlist1[0][1].points    = 11;

stringlist1[0][2].filename = "file2";
stringlist1[0][2].str         = 2;
stringlist1[0][2].points    = 8;

stringlist1[1][1].filename = "file1";
stringlist1[1][1].str         = 1;
stringlist1[1][1].points    = 11;

stringlist1[1][2].filename = "file1";
stringlist1[1][2].str         = 2;
stringlist1[1][2].points    = 8;



What should my function object and sort command look like?

Maybe someone could give me Gillmers email address and I could ask him 
directly.

Thankyou,

Merry Christmas and a Happy New Year to everyone.

Best Regards Digby Millikan.





Vote for best question.
Score: 0  # Vote:  0
Date Posted: 28-Dec-2007, at 2:38 PM EST
From: Digby Millikan
 
Re: STL Function object.  
News Group: borland.public.cppbuilder.language.cpp
 For some reason the pointer strings1 is not reaching the data, any ideas 
why this
may be so?



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 31-Dec-2007, at 6:41 AM EST
From: Digby Millikan
 
Re: STL Function object.  
News Group: borland.public.cppbuilder.language.cpp
"Digby Millikan"  wrote in message 
news:4777e782$1@newsgroups.borland.com...
> It doesn't compile;
>
>
> SortStrings(strings1[1], string_files1[1].nrecords);
>
> void __fastcall SortStrings(struct strings *strings1, int nrecords) {
>
> struct stringssp{
>
> bool operator() (const strings& lhs, const strings& rhs) const
> {
>
>  return (lhs.str < rhs.str) ||
>         ((lhs.str == rhs.str) && (lhs.pt < rhs.pt));
> }
>
> };
>
> std::sort(strings1, strings1 + nrecords, stringssp());
>
> }
>
> [C++ Error] GraphG2D.cpp(19124): E2285 Could not find a match for 
> 'std::sort<_RanIt>(strings *,strings *,stringssp)

move the definition of the struct stringssp outside the function

see 14.3.1 /2
A local type, ... shall not be used as a template-argument for
a template type-parameter.

Cheers,
Serge




Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 30-Dec-2007, at 2:55 PM EST
From: Sergiy Kanilo
 
Re: STL Function object.  
News Group: borland.public.cppbuilder.language.cpp
It doesn't compile;


SortStrings(strings1[1], string_files1[1].nrecords);

void __fastcall SortStrings(struct strings *strings1, int nrecords) {

struct stringssp{

 bool operator() (const strings& lhs, const strings& rhs) const
 {

  return (lhs.str < rhs.str) ||
         ((lhs.str == rhs.str) && (lhs.pt < rhs.pt));
 }

};

std::sort(strings1, strings1 + nrecords, stringssp());

}

[C++ Error] GraphG2D.cpp(19124): E2285 Could not find a match for 
'std::sort<_RanIt>(strings *,strings *,stringssp) 



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 31-Dec-2007, at 5:16 AM EST
From: Digby Millikan
 
Re: STL Function object.  
News Group: borland.public.cppbuilder.language.cpp
Digby Millikan wrote:

>What should my function object and sort command look like?

What you are asking for is a one-dimensional sort on
each row of a two dimensional array:

for( int i=0; i<11; ++i )
	SortStringlist( stringlist1[i] );

void _stdcall SortStringlist( struct stringlist *s )
{
	sort( s.....);	//standard one-dimension
			//sort on stringlist[2000]
}



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 28-Dec-2007, at 10:29 AM EST
From: Bob Gonder