Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-07-2005, 06:28 PM   #1
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
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
Old 07-07-2005, 06:59 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 5,394

Rep: Reputation: 409Reputation: 409Reputation: 409Reputation: 409Reputation: 409
Are you aware of UNIX db api ? Try man db, just to see
It can works with flat-file format
Old 07-08-2005, 07:29 AM   #3
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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.
Old 07-09-2005, 04:32 PM   #4
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
thanks, ta0kira, I'll look into that route


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
File System Advice AxXium Linux From Scratch 3 08-02-2005 12:30 PM
Advice on building a new system downfall Linux - Hardware 2 04-19-2005 02:06 AM
Building AMD 64 system. I need advice. DeadPenguin Linux - Hardware 5 12-29-2004 08:12 AM
file system advice sought jagmandan Linux - General 2 07-01-2004 09:37 PM
Advice for trying Linux on old system bkonrad Linux - Newbie 1 02-10-2004 03:38 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:51 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration