473,395 Members | 1,631 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,395 software developers and data experts.

map design

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?
Sep 8 '08 #1
6 1284
On Sep 8, 10:56*am, puzzlecracker <ironsel2...@gmail.comwrote:
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
Sep 8 '08 #2
On Sep 8, 11:00*am, puzzlecracker <ironsel2...@gmail.comwrote:
On Sep 8, 10:56*am, puzzlecracker <ironsel2...@gmail.comwrote:
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
Sep 8 '08 #3
puzzlecracker wrote:
On Sep 8, 10:56*am, puzzlecracker <ironsel2...@gmail.comwrote:
>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
Sep 8 '08 #4
On Sep 8, 4:56*pm, puzzlecracker <ironsel2...@gmail.comwrote:
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.
Sep 8 '08 #5
On Sep 8, 5:32 pm, Tonni Tielens <tonnitiel...@gmail.comwrote:
On Sep 8, 4:56 pm, puzzlecracker <ironsel2...@gmail.comwrote:
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:ja*********@gmail.com
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
Sep 8 '08 #6
On Sep 8, 11:32*am, Tonni Tielens <tonnitiel...@gmail.comwrote:
On Sep 8, 4:56*pm, puzzlecracker <ironsel2...@gmail.comwrote:
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
Sep 9 '08 #7

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

Similar topics

2
by: adb | last post by:
I came up with a replication configuration that is basically the result of all the restrictions of replication as well as the restrictions of allowable software on work PC's and I was curious if...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
0
by: Edward Diener | last post by:
In Borland's VCL it was possible to divide a component into design time and run time DLLs. The design time DLL would only be necessary when the programmer was setting a component's properties or...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
2
by: Paul Cheetham | last post by:
Hi, I have moved an application from VS2003 to VS2005, and I am now unable to view most of my forms in the designer. The majority of the forms in my project are derived from class PACForm,...
1
by: Nogusta123 | last post by:
Hi, I have had a lot of problems getting web pages, master pages and content pages to render in VS2005 design view the same as they would in Internet Explorer. I did a lot of looking on the...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
10
by: vital | last post by:
Hi, I am designing the middle tier of a project. It has 6 classes and microsoft application data access block. The six classes are DBServices, Logger, ProjectServices ... etc. and all these...
4
by: Ken Fine | last post by:
I've been living with a frustrating issue with VS.NET for some months now and I need to figure out what the problem is. Hopefully someone has run into the same issue and can suggest a fix. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.