473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

suitable data structure

Hi Everyone,

I have the following set of data and currently i'm using an array of
structures to represent the same, i would like to know if any other
data structure would be suitable.

name category
sam friend
rahul family
alex office
selina friend
yukiko family
keith office
meet office

I can have a maximum of 200 records, and a maximum category of 10. My
search would be based on both name and category and i was wondering if
any other data structure could be used to reduce my search time on the
list of data.

Thanks in advance!!!

Sep 7 '07 #1
2 2083
On 2007-09-07 10:47, Rahul wrote:
Hi Everyone,

I have the following set of data and currently i'm using an array of
structures to represent the same, i would like to know if any other
data structure would be suitable.

name category
sam friend
rahul family
alex office
selina friend
yukiko family
keith office
meet office

I can have a maximum of 200 records, and a maximum category of 10. My
search would be based on both name and category and i was wondering if
any other data structure could be used to reduce my search time on the
list of data.
Unless you do something wrong or run on a handheld or embedded system
you should not notice any difference in the time needed to search a record.

If you really want to speed things up use different collections (note:
arrays are bad, use std::vector instead) for each category and sort the
collections, then you should be able to find a record in O(log N) which
is about as good as it gets.

If that is not useful enough you have to provide a more detailed
description of what you want to accomplish and how your data is stored
(giving the definition of the struct would help).

--
Erik Wikström
Sep 7 '07 #2
In article <11**********************@o80g2000hse.googlegroups .com>,
sa*****@yahoo.co.in says...
Hi Everyone,

I have the following set of data and currently i'm using an array of
structures to represent the same, i would like to know if any other
data structure would be suitable.

name category
sam friend
rahul family
alex office
selina friend
yukiko family
keith office
meet office

I can have a maximum of 200 records, and a maximum category of 10. My
search would be based on both name and category and i was wondering if
any other data structure could be used to reduce my search time on the
list of data.
I'd probably set up the categories as a vector of strings, and elsewhere
just store the index into that vector. Given a maximum of 200 items, the
data structure you use probably won't make a big difference -- almost
anything you do will be fairly fast.

OTOH, it won't hurt to use something like std::map (or multimap, if a
name can fall into more than one category). If you need to do lookups in
both directions fairly frequently, I'd consider building it as a two-way
mapping: the map holds names and for each an index into the vector of
categories. Each category would hold a string for the category name, and
a vector of iterators to names that fall into that category:

struct reverse_mapping;

typedef std::map<std::string, std::vector<reverse_mapping>::iterator>
name_map;

struct reverse_mapping {
std::string category;
std::vector<name_map::iteratorpeople;

reverse_mapping(std::string n) : category(n) {}
bool operator==(reverse_mapping const &other) {
return category == other.category;
}
};

name_map f_map;
std::vector<reverse_mappingr_map;

I'd guess the 10 categories are pre-set and remain constant, while
people can be added and deleted dynamically (though this doesn't make a
huge difference). Initializing the vector of category names is just a
matter of reading in the names (e.g. from a disk file or array) and
pushing each onto the back of r_map. From there, adding a new person
looks something like this:

std::vector<reverse_mapping>::iterator f =
std::find(r_map.begin(), r_map.end(), category);

// first update forward map
std::pair<name_map::iterator, boolpos =
f_map.insert(std::make_pair(name, f));

// then update reverse map with value returned by insert above
f->people.push_back(pos.first);

The category associated with a person is found at:

f_map.find(name)->second->category

The people in a category are found at:

std::vector<reverse_mapping>::iterator r =
std::find(r_map.begin(), r_map.end(), category);

// the names are in r->people->first

Note that since 'people' is a vector, this is a list of names you'd need
to iterate through to see them all.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Luong Phan | last post by:
--0-342085162-1061547060=:86593 Content-Type: text/plain; charset=us-ascii Hi all, In my web application, I use mysql supported by Redhat Linux 7.3 to store database, jakarta-tomcat-3.2.4,...
3
by: Mike Jones | last post by:
need help with data structures.Looking for ways to start, sample code, anything Program description: Design and implement a Visual C++ .NET program that inserts values into a data...
7
by: William Payne | last post by:
Hello, have you seen a recent files menu in a GUI application? In many GUI applications there's a menu the displays the most recent files that has been opened by the program. Say such a menu has...
2
by: yee young han | last post by:
I need a fast data structure and algorithm like below condition. (1) this data structure contain only 10,000 data entry. (2) data structure's one entry is like below typedef struct _DataEntry_...
12
by: Susan Baker | last post by:
Hi, I want to store data in a 3-tuple {a,b,c}. Element a is an enumeration, and element c is an enumeration which is specific (i.e. determined) by a. An example will help clarify further. I...
1
by: mark4asp | last post by:
Hello, I have an eBook which I need to create a CHM version for. It contains hundreds of HTML files. Can anyone recommend a HTML authoring tool which will allow me to create new directories...
3
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String...
2
by: Ivan | last post by:
I have a class Foo which have two property. I have a thread that do some process and update class Foo int B. I have a datagridview on main form. this datagridview data source to this class Foo and...
3
by: Rahul | last post by:
Hi Everyone, I have the following set of data and currently i'm using an array of structures to represent the same, i would like to know if any other data structure would be suitable. name...
8
by: K Viltersten | last post by:
I'll be working some XML's soon and i wonder if it's recommended to develop own parsers or if there exist good tools already in DotNet 3.0 or 3.5 under VS.NET 2005. Once the XML has been read,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.