Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > map design

Reply
Thread Tools

map design

 
 
puzzlecracker
Guest
Posts: n/a
 
      09-08-2008
What is the best best way to map two string values to one string
value.

say I need to map first_name, last_name to account_name. What is the
best way to implement that?
 
Reply With Quote
 
 
 
 
puzzlecracker
Guest
Posts: n/a
 
      09-08-2008
On Sep 8, 10:56*am, puzzlecracker <ironsel2...@gmail.com> wrote:
> What is the best best way to map two string values to one string
> value.
>
> say I need to map first_name, last_name to account_name. What is the
> best way to implement that?


i have this right now:

map<std::string,map<std::string,std::string> > NameAccountMap;

And I don't like it
 
Reply With Quote
 
 
 
 
puzzlecracker
Guest
Posts: n/a
 
      09-08-2008
On Sep 8, 11:00*am, puzzlecracker <ironsel2...@gmail.com> wrote:
> On Sep 8, 10:56*am, puzzlecracker <ironsel2...@gmail.com> wrote:
>
> > What is the best best way to map two string values to one string
> > value.

>
> > say I need to map first_name, last_name to account_name. What is the
> > best way to implement that?

>
> i have this right now:
>
> map<std::string,map<std::string,std::string> > NameAccountMap;
>
> And I don't like it


Actually, let me change the statement of the problem:

I need to map first key to the second key (which only can take 1 or 0)
and map that to the 3rd value: string to map seem like an overkill.
suggest please
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-08-2008
puzzlecracker wrote:

> On Sep 8, 10:56*am, puzzlecracker <ironsel2...@gmail.com> wrote:
>> What is the best best way to map two string values to one string
>> value.
>>
>> say I need to map first_name, last_name to account_name. What is the
>> best way to implement that?

>
> i have this right now:
>
> map<std::string,map<std::string,std::string> > NameAccountMap;
>
> And I don't like it


"Best" is tricky. It depends on what searches are most common. If you _only_
need to support lookup of the account by complete name, then I would go
for:

map< pair< string, string >, string >


As soon as incomplete information or reverse lookups enter the picture, a
completely different datastructure might be best.



Best

Kai-Uwe Bux
 
Reply With Quote
 
Tonni Tielens
Guest
Posts: n/a
 
      09-08-2008
On Sep 8, 4:56*pm, puzzlecracker <ironsel2...@gmail.com> wrote:
> What is the best best way to map two string values to one string
> value.
>
> say I need to map first_name, last_name to account_name. What is the
> best way to implement that?


You can group the first and last name into a new Name class, overload
it's == operator to check if the first and last name match the first
and last name of the input Name object and put Name objects as keys in
the map. You can then do something like

std::string account = nameAccountMap[Name("Puzzle", "Cracker")];

to find the account name belonging to Puzzle Cracker.

Or you can introduce a Person class that holds all three and use
function objects to check if a Person object has a certain first name
and last name. This together with the STL's find_if function can give
you a Person object iterator that matches your first name and last
name.

Something like:

class Person
{
public:
std::string getFirstName();
std::string getLastName();
std::string getAccountName();
};

class HasPersonGotName
{
public:
HasPersonGotName(const std::string firstName, const std::string
lastName) :
firstName_(firstName), lastName_(lastName)
{
}

bool operator()(const Person* person) const
{
return ((firstName_ == person.getFirstName()) && (lastName_ ==
person.getLastName());
}
private:
std::string firstName_;
std::string lastName_;
};

void someFunc()
{
std::vector<Person*> persons;

// Assume some code fills your persons vector.

std::vector<Person*>::const_iterator = std::find_if(person.begin(),
person.end(), HasPersonGotName("Puzzle", "Cracker"));

if (it != persons.end())
{
std::cout << "Account found, account name is: " << (*it)-
>getAccountName() << std::endl;

}
else
{
std::cout << "Account not found." << std::endl;
}
}

Might seem a bit strange at first sight, but once you get the hang of
it, it helps a lot.

Good luck.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-08-2008
On Sep 8, 5:32 pm, Tonni Tielens <tonnitiel...@gmail.com> wrote:
> On Sep 8, 4:56 pm, puzzlecracker <ironsel2...@gmail.com> wrote:


> > What is the best best way to map two string values to one string
> > value.


> > say I need to map first_name, last_name to account_name.
> > What is the best way to implement that?


> You can group the first and last name into a new Name class,
> overload it's == operator to check if the first and last name
> match the first and last name of the input Name object and put
> Name objects as keys in the map.


If you want to use the type as a key in a map, you'll either
have to overload operator<, specialize std::less, or explicitly
specify the ordering function to use with std::map. I generally
prefer the latter, but in the case of first name/last name, a
name class with operator< seems like the simplest and best
solution.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      09-09-2008
On Sep 8, 11:32*am, Tonni Tielens <tonnitiel...@gmail.com> wrote:
> On Sep 8, 4:56*pm, puzzlecracker <ironsel2...@gmail.com> wrote:
>
> > What is the best best way to map two string values to one string
> > value.

>
> > say I need to map first_name, last_name to account_name. What is the
> > best way to implement that?

>
> You can group the first and last name into a new Name class, overload
> it's == operator to check if the first and last name match the first
> and last name of the input Name object and put Name objects as keys in
> the map. You can then do something like
>
> * std::string account = nameAccountMap[Name("Puzzle", "Cracker")];
>
> to find the account name belonging to Puzzle Cracker.
>
> Or you can introduce a Person class that holds all three and use
> function objects to check if a Person object has a certain first name
> and last name. This together with the STL's find_if function can give
> you a Person object iterator that matches your first name and last
> name.
>
> Something like:
>
> class Person
> {
> public:
> * std::string getFirstName();
> * std::string getLastName();
> * std::string getAccountName();
>
> };
>
> class HasPersonGotName
> {
> public:
> * HasPersonGotName(const std::string firstName, const std::string
> lastName) :
> * * firstName_(firstName), lastName_(lastName)
> * {
> * }
>
> * bool operator()(const Person* person) const
> * {
> * * return ((firstName_ == person.getFirstName()) && (lastName_ ==
> person.getLastName());
> * }
> private:
> * std::string firstName_;
> * std::string lastName_;
>
> };
>
> void someFunc()
> {
> * std::vector<Person*> persons;
>
> * // Assume some code fills your persons vector.
>
> * std::vector<Person*>::const_iterator = std::find_if(person.begin(),
> person.end(), HasPersonGotName("Puzzle", "Cracker"));
>
> * if (it != persons.end())
> * {
> * * std::cout << "Account found, account name is: " << (*it)->getAccountName() << std::endl;
>
> * }
> * else
> * {
> * * std::cout << "Account not found." << std::endl;
> * }
>
> }
>
> Might seem a bit strange at first sight, but once you get the hang of
> it, it helps a lot.
>
> Good luck.


return ((firstName_ == person.getFirstName()) && (lastName_ ==
person.getLastName());

you meant:
return ((firstName_ == person->getFirstName()) && (lastName_ ==
person->getLastName()); I hope
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::map::find() throws exception when map is empty? Matthias Hildebrand C++ 5 03-20-2012 06:09 AM
I can map all files (.*) to asp.net worker.How do I map NO FILE to asp.net worker? alex ASP .Net 1 02-04-2005 03:18 AM
searching keys in std::map using map::upper_bound Erik Arner C++ 0 11-02-2004 11:14 PM
map.insert(key,val) vs. map[key]=val ? Patrick Guio C++ 6 10-20-2004 01:54 PM
map that maps to iterators in the same map ? Vlad C++ 0 12-15-2003 08:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57