Thread: Re: [Dev-C++] Lookup tables, with bsearch, lsearch, qsort etc.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel G. <sp...@ho...> - 2003-01-16 08:12:25
|
I'll have to go look all these up. I want to try the lookup table because it was recommended by a peer, and I don't know how to do it so two very good reasons there. I'm using MFC, is STL included with that or do I need to #include some header files? >From: "Ioannis Vranos" <no...@ho...> >To: <dev...@li...> >Subject: Re: [Dev-C++] Lookup tables, with bsearch, lsearch, qsort etc. >Date: Wed, 15 Jan 2003 18:27:19 +0200 (GTB Standard Time) > >From standard library you can use map, etc. > > > >Ioannis Vranos > >* Programming pages: http://www.noicys.freeurl.com >* Alternative URL: http://run.to/noicys > > >-------Original Message------- > >From: Daniel Glenfield >Date: Wednesday, 15 January 2003 5:59:53 PM >To: dev...@li... >Subject: [Dev-C++] Lookup tables, with bsearch, lsearch, qsort etc. > >I'm trying to understand lookup tables and the bsearch, lsearch, qsort >functions. I'm trying to create a structure with an integer and a text >string, and I want to look up the number and get the text associated with >it. for example, at the moment I have > >// BIG switch here >switch (PaperSize) // can be 1 - 41 >{ >case 1: ListBox = "Paper Type: Letter"; >Listbox->AddString(ListBoxChoice); >break; > >case 2: //... the same again >//... >case 41: // same again > > >I'm trying to replace this with a lookup table but I'm finding it difficult >- examples on the net about bsearch are not quite what I need. > > >------------------------------------------------------- >This SF.NET email is sponsored by: Take your first step towards giving >your online business a competitive advantage. Test-drive a Thawte SSL >certificate - our easy online guide will show you how. Click here to get >started: http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0027en >_______________________________________________ >Dev-cpp-users mailing list >Dev...@li... >TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users _________________________________________________________________ Help STOP SPAM: Try the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=features/junkmail |
|
From: Daniel G. <sp...@ho...> - 2003-01-16 09:08:42
|
Thanks I'll do some research.
Can you tell me how to put these values in to a struct?
1, "Letter"
2, "Letter(small)"
3, "Tabloid"
etc. I'm having trouble with this.
struct PAPER
{
int Num;
char* PaperStrings[42][1];
} PaperTypes, *PaperStruct = &PaperTypes;
PaperTypes.Num = 1;
PaperTypes.PaperStrings[42][1] = "Letter";
PaperStruct->PaperStrings[40][1] = "Letter(small)";
As you can see this is very wrong. I feel an idiot, but all the examples of
structs I have seen do not use arrays or do anything like this, so I don't
know how to do it!
Can you help?
>From: Per Westermark <pw...@ia...>
>To: Daniel Glenfield <sp...@ho...>
>CC: dev...@li...
>Subject: Re: [Dev-C++] Lookup tables, with bsearch, lsearch, qsort etc.
>Date: Thu, 16 Jan 2003 10:43:12 +0100 (CET)
>
>In MFC applications, you might decide to use CMap instead.
>
>/Per W
>
>On Thu, 16 Jan 2003, Daniel Glenfield wrote:
>
> > I'll have to go look all these up. I want to try the lookup table
>because it
> > was recommended by a peer, and I don't know how to do it so two very
>good
> > reasons there.
> > I'm using MFC, is STL included with that or do I need to #include some
> > header files?
> >
> >
> > >From: "Ioannis Vranos" <no...@ho...>
> > >To: <dev...@li...>
> > >Subject: Re: [Dev-C++] Lookup tables, with bsearch, lsearch, qsort etc.
> > >Date: Wed, 15 Jan 2003 18:27:19 +0200 (GTB Standard Time)
> > >
> > >From standard library you can use map, etc.
> > >
> > >
> > >
> > >Ioannis Vranos
> > >
> > >* Programming pages: http://www.noicys.freeurl.com
> > >* Alternative URL: http://run.to/noicys
> > >
> > >
> > >-------Original Message-------
> > >
> > >From: Daniel Glenfield
> > >Date: Wednesday, 15 January 2003 5:59:53 PM
> > >To: dev...@li...
> > >Subject: [Dev-C++] Lookup tables, with bsearch, lsearch, qsort etc.
> > >
> > >I'm trying to understand lookup tables and the bsearch, lsearch, qsort
> > >functions. I'm trying to create a structure with an integer and a text
> > >string, and I want to look up the number and get the text associated
>with
> > >it. for example, at the moment I have
> > >
> > >// BIG switch here
> > >switch (PaperSize) // can be 1 - 41
> > >{
> > >case 1: ListBox = "Paper Type: Letter";
> > >Listbox->AddString(ListBoxChoice);
> > >break;
> > >
> > >case 2: //... the same again
> > >//...
> > >case 41: // same again
> > >
> > >
> > >I'm trying to replace this with a lookup table but I'm finding it
>difficult
> > >- examples on the net about bsearch are not quite what I need.
> > >
> > >
> > >-------------------------------------------------------
> > >This SF.NET email is sponsored by: Take your first step towards giving
> > >your online business a competitive advantage. Test-drive a Thawte SSL
> > >certificate - our easy online guide will show you how. Click here to
>get
> > >started: http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0027en
> > >_______________________________________________
> > >Dev-cpp-users mailing list
> > >Dev...@li...
> > >TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> > >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
> >
> > _________________________________________________________________
> > Help STOP SPAM: Try the new MSN 8 and get 2 months FREE*
> > http://join.msn.com/?page=features/junkmail
> >
> >
> >
> > -------------------------------------------------------
> > This SF.NET email is sponsored by: A Thawte Code Signing Certificate
> > is essential in establishing user confidence by providing assurance of
> > authenticity and code integrity. Download our Free Code Signing guide:
> > http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0028en
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
>
>
>
>-------------------------------------------------------
>This SF.NET email is sponsored by: A Thawte Code Signing Certificate
>is essential in establishing user confidence by providing assurance of
>authenticity and code integrity. Download our Free Code Signing guide:
>http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0028en
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail
|
|
From: Ioannis V. <no...@ho...> - 2003-01-16 13:35:09
|
-------Original Message-------=0D
=0D
From: Daniel Glenfield=0D
Date: Thursday, 16 January 2003 11:09:47 AM=0D
To: pw...@ia...=0D
Cc: dev...@li...=0D
Subject: Re: [Dev-C++] Lookup tables, with bsearch, lsearch, qsort etc.=0D
=0D
Thanks I'll do some research.=0D
Can you tell me how to put these values in to a struct?=0D
=0D
1, "Letter"=0D
2, "Letter(small)"=0D
3, "Tabloid"=0D
=0D
etc. I'm having trouble with this.=0D
=0D
// I asssume C=0D
=0D
=3D=3D> #include <string.h>=0D
=0D
=0D
struct PAPER=0D
{=0D
int Num;=0D
char* PaperStrings[42][1];=0D
=0D
=3D=3D>The 1 thing is reduntant!=0D
=0D
=3D=3D> char *PaperStrings[42];=0D
=0D
=0D
} PaperTypes, *PaperStruct =3D &PaperTypes;=0D
=0D
PaperTypes.Num =3D 1;=0D
=0D
PaperTypes.PaperStrings[42][1] =3D "Letter";=0D
=0D
=0D
=3D=3D> strncpy(PaperTypes.PaperStrings, "Letter", 42);=0D
=0D
=0D
PaperStruct->PaperStrings[40][1] =3D "Letter(small)";=0D
=0D
=3D=3D> /* ... */=0D
=0D
As you can see this is very wrong. I feel an idiot, but all the examples =
of =0D
structs I have seen do not use arrays or do anything like this, so I don'=
t =0D
know how to do it!=0D
=0D
=0D
=0D
=3D=3D> I also suggest "The C Programming Language" 2nd Edition by Brian
Kernighan - Dennis Ritchie (the creator of C).=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20 |
|
From: Per W. <pw...@ia...> - 2003-01-16 16:04:31
|
Correction about strncpy
strncpy(PaperTypes.PaperStrings,"Letter",42-1);
PaperTypes.PaperStrings[42-1] = '\0';
strncpy will not insert a null-termination in case the string gets
clipped.
However, in this case, I assume that the goal was an array of 42 strings,
and not one string of max 42-1 characters.
char *PaperTypes[42];
and in that case it's allowed to do
PaperTypes[0] = "Letter";
PaperTypes[1] = "Legal";
PaperTypes[2] = "A4";
...
Normally then, You would do:
// Number of elements of a static array.
#define NELEM(a) (sizeof(a)/sizeof(*(a)))
struct Format {
int id,
const char *name;
} formats[] = {
{1,"Letter"},
{2,"Legal"},
{3,"A4"}
};
int cmp(const void *a,const void *b) {
const Format *pa = (const Format*)a;
const Format *pb = (const Format*)b;
return pa->id - pb->id;
}
int locate(const void *a,const void *b) {
int key = *(const int*)a;
const Format *fmt = (const Format*)b;
return key - fmt->id;
}
qsort(formats,NELEM(formats),sizeof(*formats),cmp);
int key = 2;
Format *p = bsearch(&key,formats,NELEM(formats),sizeof(*formats),locate);
/Per W
On Thu, 16 Jan 2003, Ioannis Vranos wrote:
> -------Original Message-------
>
> From: Daniel Glenfield
> Date: Thursday, 16 January 2003 11:09:47 AM
> To: pw...@ia...
> Cc: dev...@li...
> Subject: Re: [Dev-C++] Lookup tables, with bsearch, lsearch, qsort etc.
>
> Thanks I'll do some research.
> Can you tell me how to put these values in to a struct?
>
> 1, "Letter"
> 2, "Letter(small)"
> 3, "Tabloid"
>
> etc. I'm having trouble with this.
>
> // I asssume C
>
> ==> #include <string.h>
>
>
> struct PAPER
> {
> int Num;
> char* PaperStrings[42][1];
>
> ==>The 1 thing is reduntant!
>
> ==> char *PaperStrings[42];
>
>
> } PaperTypes, *PaperStruct = &PaperTypes;
>
> PaperTypes.Num = 1;
>
> PaperTypes.PaperStrings[42][1] = "Letter";
>
>
> ==> strncpy(PaperTypes.PaperStrings, "Letter", 42);
>
>
> PaperStruct->PaperStrings[40][1] = "Letter(small)";
>
> ==> /* ... */
>
> As you can see this is very wrong. I feel an idiot, but all the examples of
> structs I have seen do not use arrays or do anything like this, so I don't
> know how to do it!
>
>
>
> ==> I also suggest "The C Programming Language" 2nd Edition by Brian
> Kernighan - Dennis Ritchie (the creator of C).
>
>
> Ioannis Vranos
>
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL: http://run.to/noicys
|
|
From: Ioannis V. <no...@ho...> - 2003-01-16 19:08:19
|
-------Original Message-------=0D
=0D
From: Per Westermark=0D
Date: Thursday, 16 January 2003 6:03:21 PM=0D
To: Ioannis Vranos=0D
Cc: dev...@li...=0D
Subject: Re: [Dev-C++] Lookup tables, with bsearch, lsearch, qsort etc.=0D
=0D
Correction about strncpy=0D
strncpy(PaperTypes.PaperStrings,"Letter",42-1);=0D
PaperTypes.PaperStrings[42-1] =3D '\0';=0D
=0D
strncpy will not insert a null-termination in case the string gets=0D
clipped.=0D
=0D
However, in this case, I assume that the goal was an array of 42 strings,=
=0D
and not one string of max 42-1 characters.=0D
=0D
char *PaperTypes[42];=0D
=0D
and in that case it's allowed to do=0D
=0D
PaperTypes[0] =3D "Letter";=0D
PaperTypes[1] =3D "Legal";=0D
PaperTypes[2] =3D "A4";=0D
=2E..=0D
=0D
Normally then, You would do:=0D
=0D
// Number of elements of a static array.=0D
#define NELEM(a) (sizeof(a)/sizeof(*(a)))=0D
=0D
struct Format {=0D
int id,=0D
const char *name;=0D
} formats[] =3D {=0D
{1,"Letter"},=0D
{2,"Legal"},=0D
{3,"A4"}=0D
};=0D
=0D
=0D
int cmp(const void *a,const void *b) {=0D
const Format *pa =3D (const Format*)a;=0D
const Format *pb =3D (const Format*)b;=0D
=0D
return pa->id - pb->id;=0D
}=0D
=0D
int locate(const void *a,const void *b) {=0D
int key =3D *(const int*)a;=0D
const Format *fmt =3D (const Format*)b;=0D
=0D
return key - fmt->id;=0D
}=0D
=0D
qsort(formats,NELEM(formats),sizeof(*formats),cmp);=0D
=0D
int key =3D 2;=0D
Format *p =3D bsearch(&key,formats,NELEM(formats),sizeof(*formats),locate=
);=0D
=0D
/Per W=0D
=0D
=0D
=3D=3D> Yes, you are right. Probably i had my mind to something else at t=
he time
:) I used a char *array as a char array, etc.=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20 |
|
From: Per W. <pw...@ia...> - 2003-01-16 08:50:21
|
In MFC applications, you might decide to use CMap instead. /Per W On Thu, 16 Jan 2003, Daniel Glenfield wrote: > I'll have to go look all these up. I want to try the lookup table because it > was recommended by a peer, and I don't know how to do it so two very good > reasons there. > I'm using MFC, is STL included with that or do I need to #include some > header files? > > > >From: "Ioannis Vranos" <no...@ho...> > >To: <dev...@li...> > >Subject: Re: [Dev-C++] Lookup tables, with bsearch, lsearch, qsort etc. > >Date: Wed, 15 Jan 2003 18:27:19 +0200 (GTB Standard Time) > > > >From standard library you can use map, etc. > > > > > > > >Ioannis Vranos > > > >* Programming pages: http://www.noicys.freeurl.com > >* Alternative URL: http://run.to/noicys > > > > > >-------Original Message------- > > > >From: Daniel Glenfield > >Date: Wednesday, 15 January 2003 5:59:53 PM > >To: dev...@li... > >Subject: [Dev-C++] Lookup tables, with bsearch, lsearch, qsort etc. > > > >I'm trying to understand lookup tables and the bsearch, lsearch, qsort > >functions. I'm trying to create a structure with an integer and a text > >string, and I want to look up the number and get the text associated with > >it. for example, at the moment I have > > > >// BIG switch here > >switch (PaperSize) // can be 1 - 41 > >{ > >case 1: ListBox = "Paper Type: Letter"; > >Listbox->AddString(ListBoxChoice); > >break; > > > >case 2: //... the same again > >//... > >case 41: // same again > > > > > >I'm trying to replace this with a lookup table but I'm finding it difficult > >- examples on the net about bsearch are not quite what I need. > > > > > >------------------------------------------------------- > >This SF.NET email is sponsored by: Take your first step towards giving > >your online business a competitive advantage. Test-drive a Thawte SSL > >certificate - our easy online guide will show you how. Click here to get > >started: http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0027en > >_______________________________________________ > >Dev-cpp-users mailing list > >Dev...@li... > >TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > _________________________________________________________________ > Help STOP SPAM: Try the new MSN 8 and get 2 months FREE* > http://join.msn.com/?page=features/junkmail > > > > ------------------------------------------------------- > This SF.NET email is sponsored by: A Thawte Code Signing Certificate > is essential in establishing user confidence by providing assurance of > authenticity and code integrity. Download our Free Code Signing guide: > http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0028en > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Ioannis V. <no...@ho...> - 2003-01-16 13:26:11
|
-------Original Message-------=0D =0D From: Daniel Glenfield=0D Date: Thursday, 16 January 2003 10:41:20 AM=0D To: dev...@li...=0D Subject: Re: [Dev-C++] Lookup tables, with bsearch, lsearch, qsort etc.=0D =0D I'll have to go look all these up. I want to try the lookup table because= it =0D was recommended by a peer, and I don't know how to do it so two very good= =0D reasons there.=0D I'm using MFC, is STL included with that or do I need to #include some =0D header files?=0D =0D =0D =3D=3D> STL, which is a subset of the standard library (but the largest p= ortion of it, so many confuse the term "standard library" with "STL") is part of the core language so it is available to every ANSI/ISO C++ compliant compiler (from small embedded devices to large mainframes).=0D =0D =0D Ioannis Vranos=0D =0D * Programming pages: http://www.noicys.freeurl.com=0D * Alternative URL: http://run.to/noicys=20 |