wat to use?

This is a discussion on wat to use? within the C++ Programming forums, part of the General Programming Boards category; i am in need of a simple scenarion.. i want to store string values tat is assoacitaed by two key ...

  1. #1
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    wat to use?

    i am in need of a simple scenarion..
    i want to store string values tat is assoacitaed by two key values which are characters
    say for eg:
    table[a][b]= stringvalue
    here a,b are char and string value is string
    wat data structure to use...or wat stl....
    am not sure whether map ll suit my scenario...i need two key values...can anyone explain me with an example for my scenario

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Code:
    struct key
    {
       char a;
       char b;
    }
    
    std::map<key,std::string> table;
    something like this?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    If you want to use something along the lines of what vart suggested, then you should define operator< for the struct, or otherwise provide a predicate to the map.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    thanks for the reply...
    how will my insert function will look like in this case...
    table.insert........

  5. #5
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    laserlight i dont get wat u say...can u elabrate on tat

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    The keys of a std::map must be "less than comparable", hence you need to define what "less than comparable" means for your struct (or std:air<char, char>, if you choose to use that instead).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    well laserlight my question can be still narrowed...
    how will i use the arrays(2-d) with character indices;
    string table[a][b]="jdjdjdjddk"

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Quote Originally Posted by dpp
    how will i use the arrays(2-d) with character indices;
    The same as you would deal with 2D arrays under other circumstances. Of course, this implies that you probably should be using unsigned char instead of char, since char may be signed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    can u explain with a simple example

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Quote Originally Posted by dpp
    can u explain with a simple example
    As you wish:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string strings[2][2] = {{"hello", "everybody"}, {"and", "goodbye"}};
        char a = '\0';
        char b = '\0';
        std::cout << strings[a][b] << std::endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    no i dint ask for this...anyways thanks for ur partience...i was asking with maps eg
    i hope u get my qstn...
    char a,b;
    Code:
    cin>>a>>b;
    table[a][b]="mkdkdk";
    first i defined char x[]={'a','b',............................}
    and used table[x[0]].....i need egs with map implemntation

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Something like this?

    Code:
    #include <iostream>
    #include <utility>
    #include <string>
    #include <map>
    
    typedef std::pair<char,char> p_key;
    
    struct ltp_key {
      bool operator() (p_key p1, p_key p2) const
      {
        if (p1.first > p2.first)
          return true;
        
        if (p1.first == p2.first && p1.second > p2.second)
          return true;
    
        return false;
      }
    };
    
    int
    main (int argc, char **argv) 
    {
      std::map<p_key, std::string, ltp_key> _map;
    
      _map.insert ( std::pair<p_key, std::string> (
            p_key ('a','b'), std::string ("Hello world!")
            )
          );
      
      _map.insert (std::pair<p_key, std::string> (
            p_key ('x','y'), std::string ("!dlrow olleH")
            )
          );
    
      std::cout << _map.find (p_key ('a','b'))->second << std::endl;
    
      return 0;
    }
    Last edited by edoceo; 03-18-2009 at 11:36 AM. Reason: typo in code

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    map already defines operator<, so struct lpt_key is not strictly necessary.

    It is also probably a bit unsafe to just dereference the result of map::find directly, unless you know for sure that this key exists ... in which case you might just as well use map's operator[].
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What about using a std::map< char, std::map< char, std::string > >
    I believe that should let you use it as m['a']['b'] = "A & B";

    Then you don't need to worry about an operator< or creating a new struct or std:: pair...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Quote Originally Posted by anon View Post
    map already defines operator<, so struct lpt_key is not strictly necessary.

    It is also probably a bit unsafe to just dereference the result of map::find directly, unless you know for sure that this key exists ... in which case you might just as well use map's operator[].
    Since I hardcoded this small "application" to add my value to '_map', and since it was only a demonstration of using std::pair as a key in a std::map I didn't see any reason to check if it was really added or not.

    Also, just because map have the operator< defined doesn't say that it will treat our key-value the way we want to. Code below as an example:

    Code:
    #include <iostream>
    #include <cstring>
    #include <map>
    
    struct ltcstring {
      bool operator() (const char *user1, const char *user2) const
      {
        return strcmp(user1, user2) < 0;
      }
    };
    
    int
    main (int argc, char **argv) 
    {
      const char *user1 = "edoceo";
      const char *user2 = "anon";
      const char *user3 = "dpp";
    
      std::map<const char *, unsigned short> table1;
      std::map<const char *, unsigned short, ltcstring> table2;
    
      std::map<const char *, unsigned short>::iterator it;
    
      /* insert things to table1 */
      table1.insert (std::pair<const char *, unsigned short> (user1, 13));
      table1.insert (std::pair<const char *, unsigned short> (user2, 2393));
      table1.insert (std::pair<const char *, unsigned short> (user3, 82));
    
      /* insert things to table2 */
      table2.insert (std::pair<const char *, unsigned short> (user1, 13));
      table2.insert (std::pair<const char *, unsigned short> (user2, 2393));
      table2.insert (std::pair<const char *, unsigned short> (user3, 82));
    
      std::cout << "Printing std::map<..> table1:" << std::endl;
      for(it = table1.begin (); it != table1.end (); it++) {
        std::cout << " " << it->first << std::endl;
      }
        
      std::cout << std::endl << "Printing std::map<..> table2:" << std::endl;
      for(it = table2.begin (); it != table2.end (); it++) {
        std::cout << " " << it->first << std::endl;
      }
    
      return 0;
    }
    output:
    Code:
    Printing std::map<..> table1:
     edoceo
     anon
     dpp
    
    Printing std::map<..> table2:
     anon
     dpp
     edoceo
    The reason for this is that std::map's operator< will treat our keys as numeric values, and therfor compare the address where our pointers are pointing. But we want to sort them alphabetically - and therefor we should write our own operator< for this.
    It is only proper to use std::less when dealing with numeric values, imo.

    Quote Originally Posted by cpjust
    What about using a std::map< char, std::map< char, std::string > >
    I believe that should let you use it as m['a']['b'] = "A & B";

    Then you don't need to worry about an operator< or creating a new struct or std:: pair...
    That is a pretty clean solution, codewise, but in memory it will be both slower and take up more space.
    And as 'anon' wrote, you shouldn't (in a real-life scenario) be sure that a key exists when using map.find() or the operator[] - what if memory runs out while trying to add a pair?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone tell me wat is the problem??
    By peter_hii in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2006, 07:03 AM
  2. wat is the easiest way to create a palindrome
    By Krazen in forum C Programming
    Replies: 4
    Last Post: 04-09-2006, 08:06 AM
  3. wat does this function do?
    By gamett711221 in forum C++ Programming
    Replies: 5
    Last Post: 02-22-2005, 12:56 PM
  4. wat is the use of sizeof(int)?
    By zell in forum C Programming
    Replies: 3
    Last Post: 01-24-2005, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21