-   Programming (
-   -   Need help/advice on system (

nodger 07-07-2005 06:28 PM

Need help/advice on system
Heres what I need to do:

I need to implement a system for storing strings, and I need to be able to easily add and remove strings from it as fast and efficently as posssible. Also each string will have an associated id (unsigned long), and I need to be able to find a string by its id aswell. I can't use databases, so it all has to be done with files.

I hope Im explaining this well. Basically the interface would consist of 4 functions:


void AddString(string)
void RemoveString(id)
string findStringById(id)
id findIdByString(string)

Im having problems figuring out how the data would be organised. Any advise would be appreciated

keefaz 07-07-2005 06:59 PM

Are you aware of UNIX db api ? Try man db, just to see
It can works with flat-file format

ta0kira 07-08-2005 07:29 AM

If you are using C++ and want to stay standard and portable, I'd suggest std::map and std::string, then make a wrapper class for the map:

//Off the top of my head; compiled, but not linked or run
#include <string>
#include <map>

class StringData
        typedef unsigned long      id;
        typedef std::string        data;
        typedef std::map <id, data> storage;

        AddString(data dData, id iId) //<- need to give the id also
        { MapData.insert( MapData.end(), storage::value_type(iId, dData) ); }

        RemoveString(id iId)
        { MapData.erase(iId); }

        findStringById(id iId)
        { return MapData[iId]; }

        findIdByString(data sString)
        //This isn't like normal iteration because of no < or - operators
        storage::iterator DataPoint = MapData.begin();
        id Curr = 0;

        while (Curr < MapData.size())
        if (DataPoint->second == sString) return DataPoint->first;


        return 0; //Default return


It was easier to just write something than try to explain what I meant. This isn't perfect, unless I got really lucky, but hopefully this gives you some ideas. If you go this route, you should wrap the size function and some of the other basic map functions. Take a look here:

PS I just realized you might be talking about a file, therefore you could use the above plus an interface class to read/write the file. That I'm not sure about though.

nodger 07-09-2005 04:32 PM

thanks, ta0kira, I'll look into that route

All times are GMT -5. The time now is 06:04 PM.