map design

Discussion in 'C++' started by puzzlecracker, Sep 8, 2008.

  1. 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?
     
    puzzlecracker, Sep 8, 2008
    #1
    1. Advertising

  2. On Sep 8, 10:56 am, puzzlecracker <> 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
     
    puzzlecracker, Sep 8, 2008
    #2
    1. Advertising

  3. On Sep 8, 11:00 am, puzzlecracker <> wrote:
    > On Sep 8, 10:56 am, puzzlecracker <> 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
     
    puzzlecracker, Sep 8, 2008
    #3
  4. puzzlecracker

    Kai-Uwe Bux Guest

    puzzlecracker wrote:

    > On Sep 8, 10:56 am, puzzlecracker <> 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
     
    Kai-Uwe Bux, Sep 8, 2008
    #4
  5. On Sep 8, 4:56 pm, puzzlecracker <> 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.
     
    Tonni Tielens, Sep 8, 2008
    #5
  6. puzzlecracker

    James Kanze Guest

    On Sep 8, 5:32 pm, Tonni Tielens <> wrote:
    > On Sep 8, 4:56 pm, puzzlecracker <> 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
     
    James Kanze, Sep 8, 2008
    #6
  7. On Sep 8, 11:32 am, Tonni Tielens <> wrote:
    > On Sep 8, 4:56 pm, puzzlecracker <> 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
     
    puzzlecracker, Sep 9, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alex
    Replies:
    1
    Views:
    677
    Lau Lei Cheong
    Feb 4, 2005
  2. Matthias Hildebrand
    Replies:
    5
    Views:
    8,104
    krogers
    Mar 20, 2012
  3. Vlad
    Replies:
    0
    Views:
    382
  4. Patrick Guio
    Replies:
    6
    Views:
    3,246
    chris
    Oct 20, 2004
  5. Erik Arner
    Replies:
    0
    Views:
    1,330
    Erik Arner
    Nov 2, 2004
Loading...

Share This Page