map design

P

puzzlecracker

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?
 
P

puzzlecracker

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
 
P

puzzlecracker

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
 
K

Kai-Uwe Bux

puzzlecracker said:
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
 
T

Tonni Tielens

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.
 
J

James Kanze

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.
 
P

puzzlecracker

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top