Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<map> as index for class

Status
Not open for further replies.

ARCITS

Programmer
Apr 4, 2002
99
GB
I need to devise a program to hold information about library books. (Basically a representation of a DBMS)

I want to use the C++ <MAP> functionality to enable searching.

How do I make the second element of my <MAP> point at the class holding the data I need?

For Example:

Class Book containing RefNo,Title,Author,Category, ISBN, etc
Would hold RefNo as LHS of MAP, but what in RHS?

Class functions would be display which would display all details based on say Title.

So you'd use the map to find the index for the title in question and then the second element would define the class instance which would hold all the data needed to be displayed.

Anyway I hope you get the idea of what I am trying to do?

Any tips, advice, help much appreciated.


Regards
 
I can't help you here but have a look at the code I posted for the List Manager. This allows you to create a list limited only by resources. You can then write the appropriate code to Find based on an ISBN, or a Title or Price even.

If you need any assistance then make a posting.

HTH
William
Software Engineer
ICQ No. 56047340
 
I'm probably being dense here.
You said have a look at the code i posted for list manager.
Where do i find this?


thanks
 
No you weren't being dense it had gone but I know I'd posted it since others have used it!

So here it is again starting with the header:

###### Header File ######

#ifndef ITEMLIST_H_
#define ITEMLIST_H_
#pragma once

class singleItem
{
public:
singleItem () ;
singleItem (int, char *) ;
singleItem& operator = (const singleItem&) ;
bool operator == (int n) const { return m_nID == n ; }
bool operator == (const singleItem& m) const { return m.m_nID == m_nID ; }
friend class itemList ;

private:
int m_nIndex ;
int m_nID ;
char m_szData[16] ;
singleItem* m_pNext ;
singleItem* m_pPrev ;
} ;

class itemList
{
public:
itemList();
~itemList();
singleItem* Add () ;
singleItem* Add (int, char *) ;
void Remove (int) ;
singleItem* Find (char *) const ;
void RemoveAll () ;
bool operator == (char *) const ;
private:
int m_nCounter ;
singleItem* m_pHead ;
singleItem* m_pTail ;
singleItem* Find (int ID) const ;
void Remove (singleItem *) ;
void Sort (singleItem *) ;
};

#endif

##### Source Code #####

#include &quot;memory.h&quot;
#include &quot;string.h&quot;
#include &quot;itemList.h&quot;

singleItem& singleItem :: operator = (const singleItem& m)
{
m_nID = m.m_nID ;
strcpy(m_szData, m.m_szData) ;
return * this ;
}

singleItem :: singleItem() : m_pNext (0), m_pPrev (0), m_nIndex(0)
{
m_nID = 0 ;
memset(m_szData, 0, sizeof(m_szData)) ;
}

singleItem :: singleItem (int ID, char * szData) : m_pNext (0), m_pPrev (0)
{
m_nID = ID ;
strcpy(m_szData, szData) ;
}

itemList::itemList() : m_pHead (0), m_pTail (0) { }

itemList::~itemList()
{ RemoveAll () ; }

singleItem * itemList :: Add ()
{
singleItem * pItem = 0 ;
if (pItem = new singleItem ())
{
pItem->m_nIndex = ++m_nCounter ;
Sort(pItem) ;
}
return pItem ;
}

singleItem * itemList :: Add (int ID, char* szData)
{
singleItem * pItem = 0 ;
if (pItem = new singleItem (ID, szData))
{
pItem->m_nIndex = ++m_nCounter ;
Sort(pItem) ;
}
return pItem ;
}

bool itemList::eek:perator == (char * p) const
{
singleItem * pItem = m_pTail ;
while (pItem)
{
if (strcmp(pItem->m_szData, p) == 0)
{ return true ; }
pItem = pItem->m_pNext ;
}
return false ;
}

void itemList :: Remove (int ID)
{
singleItem * pItem = m_pTail ;
while (pItem)
{
if (* pItem == ID)
{
Remove (pItem) ;
break ;
}
pItem = pItem -> m_pNext ;
}
}

void itemList :: RemoveAll()
{
singleItem * pItem = m_pTail ;
singleItem * pNext ;
while (pItem)
{
pNext = pItem -> m_pNext ;
delete pItem ;
pItem = pNext ;
}
m_pHead = m_pTail = 0 ;
}

void itemList::Sort(singleItem* pItem)
{
if (m_pHead)
{
m_pHead -> m_pNext = pItem ;
pItem -> m_pPrev = m_pHead ;
m_pHead = pItem ;
}
else
{
m_pHead = pItem ;
m_pTail = pItem ;
}
}

singleItem * itemList :: Find (int nID) const
{
singleItem * pItem = m_pTail ;
while (pItem)
{
if (*pItem == nID)
break ;
pItem = pItem -> m_pNext ;
}
return pItem ;
}

void itemList :: Remove (singleItem * pItem)
{
if (pItem -> m_pPrev)
pItem -> m_pPrev -> m_pNext = pItem -> m_pNext ;
else
m_pTail = pItem -> m_pNext ;
if (pItem -> m_pNext)
pItem -> m_pNext -> m_pPrev = pItem -> m_pPrev ;
else
m_pHead = pItem -> m_pPrev ;
delete pItem ;
}

As I said before if you need any help with it let me know.



William
Software Engineer
ICQ No. 56047340
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top