using map<char[256], T*>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puzzlecracker

    using map<char[256], T*>

    I need to be able to use map<char[256], T*>, I can make it
    std::map<std::s tring, T*>, however, Key is passed by char[], hence I
    would need to call c_str() all the time. What do you suggest?
  • Victor Bazarov

    #2
    Re: using map&lt;char[256], T*&gt;

    puzzlecracker wrote:
    I need to be able to use map<char[256], T*>, I can make it
    std::map<std::s tring, T*>, however, Key is passed by char[], hence I
    would need to call c_str() all the time. What do you suggest?
    There is no comparison function for arrays. There is one for pointers
    (and your arrays will be converted to pointers for that, unfortunately)
    but that's not what you want, I reckon. Now, regarding your
    requirements, what do you mean by "Key is passed by char[]"? Passed
    where? By whom? Show us how you intend to use your map.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • Obnoxious User

      #3
      Re: using map&lt;char[256], T*&gt;

      On Tue, 23 Sep 2008 07:13:28 -0700, puzzlecracker wrote:
      I need to be able to use map<char[256], T*>, I can make it
      std::map<std::s tring, T*>, however, Key is passed by char[], hence I
      would need to call c_str() all the time. What do you suggest?
      Use std::string every where possible. If your pseudo type is
      defined by 256 chars, then wrap it in a class definition.

      --
      OU
      Remember 18th of June 2008, Democracy died that afternoon.

      Comment

      • Kai-Uwe Bux

        #4
        Re: using map&lt;char[256], T*&gt;

        puzzlecracker wrote:
        I need to be able to use map<char[256], T*>, I can make it
        std::map<std::s tring, T*>, however, Key is passed by char[], hence I
        would need to call c_str() all the time. What do you suggest?
        Huh? The following seems to work:

        #include <map>
        #include <string>
        #include <iostream>
        #include <ostream>

        typedef char word [256];
        typedef std::map< std::string, int my_map;

        int main ( void ) {
        my_map the_map;
        word key1 = "abc";
        word key2 = "xyz";
        the_map[ key1 ] = 1;
        the_map[ key2 ] = 5;
        std::cout << the_map[ key1 ] << '\n';
        std::cout << the_map[ key2 ] << '\n';
        }

        So where exactly is the problem with using std::string as the key_type?


        Best

        Kai-Uwe Bux

        Comment

        • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

          #5
          Re: using map&lt;char[256], T*&gt;

          On 2008-09-23 16:13, puzzlecracker wrote:
          I need to be able to use map<char[256], T*>, I can make it
          std::map<std::s tring, T*>, however, Key is passed by char[], hence I
          would need to call c_str() all the time. What do you suggest?
          If you can find a really good reason to not use std::string (I doubt it)
          you could create a struct like so:

          struct Key
          {
          char str[256];
          bool operator<(const Key&) { /* ... */ }
          };

          --
          Erik Wikström

          Comment

          • Richard Herring

            #6
            Re: using map&lt;char[256], T*&gt;

            In message
            <f9991edd-b348-4ebe-aaf2-c159dbd2759e@j2 2g2000hsf.googl egroups.com>,
            puzzlecracker <ironsel2000@gm ail.comwrites
            >I need to be able to use map<char[256], T*>, I can make it
            >std::map<std:: string, T*>, however, Key is passed
            passed where?
            >by char[],
            which decays to char * -- did you omit "const" in there?
            >hence I
            >would need to call c_str() all the time.
            Only to convert from string to const char *. There's an implicit
            conversion in the other direction.

            But why would using c_str() be a problem?
            >What do you suggest?
            Use std::string unless you have a really really good reason not to.
            Because of the implicit conversion, with a map<string, T*you can
            interchangeably use std::string or const char * as the parameter to
            operator[], find(), count() etc.


            --
            Richard Herring

            Comment

            • Andrew Koenig

              #7
              Re: using map&lt;char[256], T*&gt;

              "puzzlecrac ker" <ironsel2000@gm ail.comwrote in message
              news:f9991edd-b348-4ebe-aaf2-c159dbd2759e@j2 2g2000hsf.googl egroups.com...
              >I need to be able to use map<char[256], T*>,
              Well, you can't. Array types are not suitable for use as map key types.

              If you happen to get such a type to compile, then your implementation
              happens to support a usage that is not part of standard C++, and there's no
              way to know what it will do without knowing the details of your
              implementation.


              Comment

              • tony_in_da_uk@yahoo.co.uk

                #8
                Re: using map&lt;char[256], T*&gt;

                On Sep 23, 11:13 pm, puzzlecracker <ironsel2...@gm ail.comwrote:
                I need to be able to use map<char[256], T*>, I can make it
                std::map<std::s tring, T*>, however, Key is passed by char[], hence I
                would need to call c_str() all the time. What do you suggest?
                I assume this is motivated by a concern that c_str() might be
                inefficient, allocating some new storage with space for the extra
                NUL. Don't worry about it - to the best of my knowledge, no STL past
                or present does that. You can call c_str() and it will perform fine.
                If your motivation is actually convenience, then consider that
                std::string omits operator const char* for a good reason, and don't
                fight decades of community experience. - Tony

                Comment

                Working...