468,513 Members | 1,866 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

STL map or hash map using struct as data and find it

kl
Hi,

I'm trying to learn some STL using map or hash_map would maybe even
better to use but I don't really know how to find a specific struct
depending on a DWORD value that is in range of two DWORD values (see
below for more).

So what I trying to achieve is creating a look up table of a IP adress
with a start adress (a DWORD value) and a end adress (a DWORD
value) which I would like to look up to the get the Country name
using a specific IP adress (also in DWORD) which is random access to
the table.

It is easy to use vector, linked list but I would like to try to use
map or hash map for more effecient way to look up or at least test it
out to compare some.
The code is basicly looks like this:

//Struct definition
struct IPCOUNTRY
{
DWORD startIP; //IP start adress in DWORD which is always unique
DWORD endIP; //IP end adress in DWORD which is always unique
char COUNTRYNAME[45]; //Country name like England, Germany Spain
etc...
};
typedef map <DWORD, IPCOUNTRYmIPC;
mIPC mIPcountry;

//Initilize the map when and call this function during start up
void initilizeMap()
{

//read data from file and insert it into a map
IPCOUNTRY g_structIP;
FILE *fp=NULL;
fp=fopen("ipcountry.dat", "rb");

if(fp!=NULL)
{
while( !feof( fp ) )
{
if(fread(&g_structIP, sizeof(g_structIP), 1, fp)!=0)
{

mIPcountry[g_structIP.startIP] = g_structIP;
}
}
}
fclose(fp);
}
struct compareIPC
{
bool operator()(const IPCOUNTRY* ipc1, const IPCOUNTRY* icp2) const
{

return strcmp(ipc1, ipc2) < 0;
}
};
//This function will be called with IPaddress set and set the
countryname depending which country it belongs
//to using the map look up table
int lookupCountryName(DWORD IPadress, char *countryname)
{
//ok here I'm lost what I should exactly do

mIPC::iterator mIPCbegin = mIPcountry.begin();
mIPC::iterator mIPCend = mIPcountry.end();

return 0;
}
Dec 31 '07 #1
2 4805
kl
On 31 Dec, 11:48, kl <kjell.ll...@gmail.comwrote:

Sorry I accidantly clicked on the send button.
Hi,

I'm trying to learn some STL using map or hash_map would maybe even
better to use but I don't really know how to find a specific struct
depending on a DWORD value that is in range of two DWORD values (see
below for more).

So what I trying to achieve is creating a look up table of a IP adress
with a start adress (a DWORD value) *and a end adress *(a DWORD
value) *which I would like to look up to the get the Country name
using a specific IP adress (also in DWORD) which is random access to
the table.

It is easy to use vector, linked list *but I would like to try to use
map or hash map for more effecient way to look up or at least test it
out to compare some.

The code is basicly looks like this:

//Struct definition
struct IPCOUNTRY
{
* * * * DWORD startIP; * //IP start adress in DWORD which is always unique
* * * * DWORD endIP; * *//IP end adress in DWORD which is always unique
* * * * char COUNTRYNAME[45]; //Country name like England, GermanySpain
etc...

};

typedef map <DWORD, IPCOUNTRYmIPC;
mIPC mIPcountry;

//Initilize the map when and call this function during start up
void initilizeMap()
{

* * * * * * * *//read data from file and insert it into a map
* * * * IPCOUNTRY g_structIP;
* * * * FILE *fp=NULL;
* * * * fp=fopen("ipcountry.dat", "rb");

* * * * if(fp!=NULL)
* * * * {
* * * * * * * * while( !feof( fp ) )
* * * * * * * * {
* * * * * * * * * * * * if(fread(&g_structIP, sizeof(g_structIP), 1, fp)!=0)
* * * * * * * * * * * * {

* * * * * * * * * * * * * * * * * mIPcountry[g_structIP.startIP] = g_structIP;
* * * * * * * * * * * * }
* * * * * * * * }
* * * * }
* * * * fclose(fp);

}
Here is a more correct comapare function:

struct compareIPC
{
* bool operator()(const IPCOUNTRY* ipc1, const IPCOUNTRY* ipc2) const
* {
if((ipc2.startIP>=ipc1.startIP) && (ipc2.startIP <=
ipc1.endIP))
return true;
return false;
* }

};

//This function will be called with IPaddress set and set the
// countryname depending which country it belongs
//to using the map look up table
int lookupCountryName(DWORD IPadress, char *countryname)
{
* *//ok here I'm lost what I should exactly do
IPCOUNTRY tempIPC;

tempIPC.startIP = IPadress;
tempIPC.endIP = IPadress;

* *mIPC::iterator mIPCbegin = mIPcountry.begin();
* *mIPC::iterator mIPCend = *mIPcountry.end();

//I would do something like this but also include the tempIPC to
find the correct item

mIPC::iterator iterResult = find_if(mIPCbegin,
mIPCend,insideiprange);

//code to get the result and set the country name

* *return 0;
}

I tried my best to explain what I'm trying to do and
hopefully some one can shed some light of this.
regards
Kl
Dec 31 '07 #2
kl wrote:
Hi,

I'm trying to learn some STL using map or hash_map would maybe even
better to use but I don't really know how to find a specific struct
depending on a DWORD value that is in range of two DWORD values (see
below for more).

So what I trying to achieve is creating a look up table of a IP adress
with a start adress (a DWORD value) and a end adress (a DWORD
value) which I would like to look up to the get the Country name
using a specific IP adress (also in DWORD) which is random access to
the table.
If I'm understanding you correctly, you want to find a range of elemets in a
map between an upper bound and a lower bound value. Luckily, std::map has
lower_bound() and upper_bound() which both return an iteratore.
lower_bound() returning an iterator to the lowest key that is equal or
greater than the value, upper_bound() returning an iterator to the highest
key equal or less than the value.

I do believe than an IP4 address has it's MSB as the left most of the IP
address so this should work fine for a range of IP addresses.

Of course you'll need to compare the iterators for not found (compare to
..end()).
Dec 31 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Markus Dehmann | last post: by
2 posts views Thread by Bryan Olson | last post: by
8 posts views Thread by Michael B Allen | last post: by
4 posts views Thread by Simone Battagliero | last post: by
7 posts views Thread by Diane | last post: by
3 posts views Thread by railrulez | last post: by
21 posts views Thread by Johan Tibell | last post: by
13 posts views Thread by ababeel | last post: by
3 posts views Thread by digz | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.