Map-like container?

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

    #1

    Map-like container?

    Hi!

    I'm looking for something similar to the map container, but with a
    slight modification, a unique key, with multiple assignments to each
    one. I think some container in the STL has it, but I cannot see it.

    So I can assign to a key like a map:

    // pseudo c++ code
    container<int, int> m;

    // 1: <2, -3, 5>
    m[1] = 2;
    m[1] = -3;
    m[1] = 5;

    // 2: <7, 1, 0, -1, 8>
    m[2] = 7;
    m[2] = 1;
    m[2] = 0;
    m[2] = -1;
    m[2] = 8;

    And get an iterator for each key:

    for (it = m[1].begin(); it != m[1].end(); it++)
    cout << "this is " << (*it) << endl;


    The result would be something like a

    map< key_T, list<data_T> >

    without hassles :)

    Thanks to anyone!!

    --
    Sensei <senseiwa@mac.c om>

    The optimist thinks this is the best of all possible worlds.
    The pessimist fears it is true. [J. Robert Oppenheimer]

  • Jakob Bieling

    #2
    Re: Map-like container?

    Sensei <senseiwa@mac.c om> wrote:[color=blue]
    > Hi!
    >
    > I'm looking for something similar to the map container, but with a
    > slight modification, a unique key, with multiple assignments to each
    > one. I think some container in the STL has it, but I cannot see it.
    >
    > So I can assign to a key like a map:
    >
    > // pseudo c++ code
    > container<int, int> m;
    >
    > // 1: <2, -3, 5>
    > m[1] = 2;
    > m[1] = -3;
    > m[1] = 5;
    >
    > // 2: <7, 1, 0, -1, 8>
    > m[2] = 7;
    > m[2] = 1;
    > m[2] = 0;
    > m[2] = -1;
    > m[2] = 8;
    >
    > And get an iterator for each key:
    >
    > for (it = m[1].begin(); it != m[1].end(); it++)
    > cout << "this is " << (*it) << endl;
    >
    >
    > The result would be something like a
    >
    > map< key_T, list<data_T> >[/color]

    So why not use just that?

    regards
    --
    jb

    (reply address in rot13, unscramble first)


    Comment

    • Artie Gold

      #3
      Re: Map-like container?

      Sensei wrote:[color=blue]
      > Hi!
      >
      > I'm looking for something similar to the map container, but with a
      > slight modification, a unique key, with multiple assignments to each
      > one. I think some container in the STL has it, but I cannot see it.
      >
      > So I can assign to a key like a map:
      >
      > // pseudo c++ code
      > container<int, int> m;
      >
      > // 1: <2, -3, 5>
      > m[1] = 2;
      > m[1] = -3;
      > m[1] = 5;
      >
      > // 2: <7, 1, 0, -1, 8>
      > m[2] = 7;
      > m[2] = 1;
      > m[2] = 0;
      > m[2] = -1;
      > m[2] = 8;
      >
      > And get an iterator for each key:
      >
      > for (it = m[1].begin(); it != m[1].end(); it++)
      > cout << "this is " << (*it) << endl;
      >
      >
      > The result would be something like a
      >
      > map< key_T, list<data_T> >
      >
      > without hassles :)
      >[/color]

      Sounds like a multimap to me. ;-)

      HTH,
      --ag


      --
      Artie Gold -- Austin, Texas
      I will (ir)regularly write about things that are important to me -- that I hope interest you. Of course, since I see most things as being political, that will be most of it.

      "You can't KISS* unless you MISS**"
      [*-Keep it simple, stupid. **-Make it simple, stupid.]

      Comment

      • Victor Bazarov

        #4
        Re: Map-like container?

        Sensei wrote:[color=blue]
        > I'm looking for something similar to the map container, but with a
        > slight modification, a unique key, with multiple assignments to each
        > one.[/color]

        What does "multiple assignments to each one" mean? Are you trying to
        create some kind of hash table? Use 'hashtable' from SGI. Or use
        std::map<int, std::vector<int > >.
        [color=blue]
        > I think some container in the STL has it, but I cannot see it.
        >
        > So I can assign to a key like a map:
        >
        > // pseudo c++ code
        > container<int, int> m;
        >
        > // 1: <2, -3, 5>
        > m[1] = 2;
        > m[1] = -3;
        > m[1] = 5;
        >
        > // 2: <7, 1, 0, -1, 8>
        > m[2] = 7;
        > m[2] = 1;
        > m[2] = 0;
        > m[2] = -1;
        > m[2] = 8;
        >
        > And get an iterator for each key:
        >
        > for (it = m[1].begin(); it != m[1].end(); it++)
        > cout << "this is " << (*it) << endl;
        >
        >
        > The result would be something like a
        >
        > map< key_T, list<data_T> >
        >
        > without hassles :)[/color]

        <rant>
        What's a "hassle"? Do you want to avoid calling "push_back" ? Why don't
        you try to avoid programming at all, just think harder of what you want
        and it might just happen...
        </rant>

        Well, you could you invent your own class that when assigned to would grow
        its own internal collection:

        template<class T> struct auto_list
        {
        std::list<T> collection;
        public:
        auto_list& operator=(T t) { collection.push _back(t); }
        // ... whatever else you need
        };

        and then use it in your 'map':

        std::map<key_T, auto_list<data_ T> >

        Remember that free cheese is only found in the mousetrap.

        V
        --
        Please remove capital As from my address when replying by mail

        Comment

        • TB

          #5
          Re: Map-like container?

          Sensei skrev:[color=blue]
          > Hi!
          >
          > I'm looking for something similar to the map container, but with a
          > slight modification, a unique key, with multiple assignments to each
          > one. I think some container in the STL has it, but I cannot see it.
          >
          > So I can assign to a key like a map:
          >
          > // pseudo c++ code
          > container<int, int> m;
          >
          > // 1: <2, -3, 5>
          > m[1] = 2;
          > m[1] = -3;
          > m[1] = 5;
          >
          > // 2: <7, 1, 0, -1, 8>
          > m[2] = 7;
          > m[2] = 1;
          > m[2] = 0;
          > m[2] = -1;
          > m[2] = 8;
          >
          > And get an iterator for each key:
          >
          > for (it = m[1].begin(); it != m[1].end(); it++)
          > cout << "this is " << (*it) << endl;
          >
          >
          > The result would be something like a
          >
          > map< key_T, list<data_T> >
          >
          > without hassles :)
          >
          > Thanks to anyone!!
          >[/color]

          class SetWrapper {
          private:
          typedef std::set<int> seti;
          seti d_set;
          public:
          void operator=(int i) {
          d_set.insert(i) ;
          }
          seti::iterator begin() {
          return d_set.begin();
          }
          seti::iterator end() {
          return d_set.end();
          }
          };

          int main() {
          std::map<int,Se tWrapper> m;

          m[1] = 2;
          m[1] = -3;
          m[1] = 5;

          m[2] = 7;
          m[2] = 1;
          m[2] = 0;
          m[2] = -1;
          m[2] = 8;

          for (it = m[1].begin(); it != m[1].end(); it++) {
          cout << "this is " << (*it) << endl;
          }
          return 0;
          }

          --
          TB @ SWEDEN

          Comment

          • Sensei

            #6
            Re: Map-like container?

            On 2006-03-14 16:40:47 +0100, Victor Bazarov <v.Abazarov@com Acast.net> said:
            [color=blue][color=green]
            >> I'm looking for something similar to the map container, but with a
            >> slight modification, a unique key, with multiple assignments to each
            >> one.[/color]
            >
            > What does "multiple assignments to each one" mean? Are you trying to
            > create some kind of hash table? Use 'hashtable' from SGI. Or use
            > std::map<int, std::vector<int > >.[/color]

            I come from C and ASM, where nothing is defined, no lists, nothing more
            than memory allocation. I thought there were containers helping the
            programmer. This construct seems quite useful, that's why I'm asking.
            [color=blue]
            > <rant>
            > What's a "hassle"? Do you want to avoid calling "push_back" ? Why don't
            > you try to avoid programming at all, just think harder of what you want
            > and it might just happen...
            > </rant>[/color]

            A simple ``no'' would suffice, I don't really like classes and
            siblings, I feel better with memset and alike... but unfortunately for
            me, C++ is required, and I must adapt myself. Hassle (to me of course)
            is caring about copy constructors and whatever is more than
            malloc()/memmove()... that is hassle :)
            [color=blue]
            > Well, you could you invent your own class that when assigned to would grow
            > its own internal collection:
            >
            > template<class T> struct auto_list
            > {
            > std::list<T> collection;
            > public:
            > auto_list& operator=(T t) { collection.push _back(t); }
            > // ... whatever else you need
            > };
            >
            > and then use it in your 'map':
            >
            > std::map<key_T, auto_list<data_ T> >[/color]

            The class seems the same as a map of int and list<something> to me :)
            except of course, the = operator.
            [color=blue]
            > Remember that free cheese is only found in the mousetrap.[/color]

            This is very true.

            --
            Sensei <senseiwa@mac.c om>

            The optimist thinks this is the best of all possible worlds.
            The pessimist fears it is true. [J. Robert Oppenheimer]

            Comment

            • Rolf Magnus

              #7
              Re: Map-like container?

              Sensei wrote:
              [color=blue]
              > On 2006-03-14 16:40:47 +0100, Victor Bazarov <v.Abazarov@com Acast.net>
              > said:
              >[color=green][color=darkred]
              >>> I'm looking for something similar to the map container, but with a
              >>> slight modification, a unique key, with multiple assignments to each
              >>> one.[/color]
              >>
              >> What does "multiple assignments to each one" mean? Are you trying to
              >> create some kind of hash table? Use 'hashtable' from SGI. Or use
              >> std::map<int, std::vector<int > >.[/color]
              >
              > I come from C and ASM, where nothing is defined, no lists, nothing more
              > than memory allocation. I thought there were containers helping the
              > programmer.[/color]

              There are.
              [color=blue]
              > This construct seems quite useful, that's why I'm asking.
              >[color=green]
              >> <rant>
              >> What's a "hassle"? Do you want to avoid calling "push_back" ? Why don't
              >> you try to avoid programming at all, just think harder of what you want
              >> and it might just happen...
              >> </rant>[/color]
              >
              > A simple ``no'' would suffice, I don't really like classes and
              > siblings, I feel better with memset and alike... but unfortunately for
              > me, C++ is required, and I must adapt myself. Hassle (to me of course)
              > is caring about copy constructors and whatever is more than
              > malloc()/memmove()... that is hassle :)[/color]

              Hmm, the idea is actually the other way round. The constructors and that
              stuff do things automatically for you that you usually would have to care
              about yourself. You just push_back the object into your container and
              you're done. No malloc, no memmove needed.
              A copy constructor is mostly needed if you're handling some raw resources
              that themselves aren't represented by a class with a proper copy
              constructor.
              [color=blue][color=green]
              >> Well, you could you invent your own class that when assigned to would
              >> grow its own internal collection:
              >>
              >> template<class T> struct auto_list
              >> {
              >> std::list<T> collection;
              >> public:
              >> auto_list& operator=(T t) { collection.push _back(t); }
              >> // ... whatever else you need
              >> };
              >>
              >> and then use it in your 'map':
              >>
              >> std::map<key_T, auto_list<data_ T> >[/color]
              >
              > The class seems the same as a map of int and list<something> to me :)
              > except of course, the = operator.[/color]

              Well, it's still unclear what you want. What's wrong with the map of int and
              list<int> (or int and vector<int>)?

              Comment

              • Gernot Frisch

                #8
                Re: Map-like container?

                [color=blue]
                > Sounds like a multimap to me. ;-)[/color]

                a multimap has several identical entries for the key values. That's
                not what he wanted. He just wanted a map of array data:
                std::map<int, std::vector<int > >
                e.g.


                Comment

                • Rolf Magnus

                  #9
                  Re: Map-like container?

                  Gernot Frisch wrote:
                  [color=blue]
                  >[color=green]
                  >> Sounds like a multimap to me. ;-)[/color]
                  >
                  > a multimap has several identical entries for the key values. That's
                  > not what he wanted. He just wanted a map of array data:
                  > std::map<int, std::vector<int > >
                  > e.g.[/color]

                  Hmm, what would be the difference? You store several values of the same type
                  under one key.

                  Comment

                  • Gernot Frisch

                    #10
                    Re: Map-like container?

                    [color=blue]
                    > ... I feel better with memset and alike... but unfortunately for
                    > me, C++ is required, and I must adapt myself.[/color]

                    Oh boy! Get a C++ book and start thinking. You don't know what you're
                    missing. It's not C++ because classes and ineritance makes it more
                    complex, it's because it's more powerful and easier to maintain. I
                    know a good FORTRAN programmer can write FORTRAN code in any
                    language - but do you really want to?

                    C++ is _not_ C(++)

                    it is rather:

                    class Cpp : public C
                    {
                    public:
                    virtual Cpp& operator++() {return C::data++;}
                    };


                    -Gernot


                    Comment

                    • Gernot Frisch

                      #11
                      Re: Map-like container?

                      [color=blue][color=green]
                      >>[color=darkred]
                      >>> Sounds like a multimap to me. ;-)[/color]
                      >>
                      >> a multimap has several identical entries for the key values. That's
                      >> not what he wanted. He just wanted a map of array data:
                      >> std::map<int, std::vector<int > >
                      >> e.g.[/color]
                      >
                      > Hmm, what would be the difference? You store several values of the
                      > same type
                      > under one key.[/color]

                      ....now that I think of it... forget all I said.



                      Comment

                      • Sensei

                        #12
                        Re: Map-like container?

                        On 2006-03-14 16:35:45 +0100, Artie Gold <artiegold@aust in.rr.com> said:

                        [color=blue]
                        > Sounds like a multimap to me. ;-)[/color]


                        Can you explain this?

                        Following the SGI documentation, I understand it is possible to use
                        multimap with pairs, so the only way to achieve what I want is to be
                        tricky:

                        multimap<int, int> adj;

                        multimap<int, int>::iterator it_adj;

                        /*
                        1:
                        2, 4, 1

                        2:
                        6, 9, 4, 2, 1, 0
                        */

                        adj.insert(pair <int,int>(1,2)) ;
                        adj.insert(pair <int,int>(1,4)) ;
                        // ``2'' inserted before ending the key ``1''
                        adj.insert(pair <int,int>(2,6)) ;
                        adj.insert(pair <int,int>(2,9)) ;
                        adj.insert(pair <int,int>(2,4)) ;
                        adj.insert(pair <int,int>(2,1)) ;
                        adj.insert(pair <int,int>(2,0)) ;
                        // insert last ``1''
                        adj.insert(pair <int,int>(1,1)) ;
                        adj.insert(pair <int,int>(2,2)) ;

                        it_adj = adj.find(1);

                        while ((*it_adj).firs t == 1)
                        {
                        cout << "that's 2 key and value " << (*it_adj).secon d << endl;
                        it_adj++;
                        }



                        So I feel like there's no other way than this and a by-hand use of
                        map/lists... am I right? :)

                        --
                        Sensei <senseiwa@mac.c om>

                        The optimist thinks this is the best of all possible worlds.
                        The pessimist fears it is true. [J. Robert Oppenheimer]

                        Comment

                        • Sensei

                          #13
                          Re: Map-like container?

                          On 2006-03-14 17:54:50 +0100, "Gernot Frisch" <Me@Privacy.net > said:
                          [color=blue]
                          >[color=green]
                          >> ... I feel better with memset and alike... but unfortunately for
                          >> me, C++ is required, and I must adapt myself.[/color]
                          >
                          > Oh boy! Get a C++ book and start thinking. You don't know what you're missing.[/color]

                          I bought C++, third edition, from Bjarne Stroustrup himself... not that
                          I bought it from him I mean :)
                          [color=blue]
                          > It's not C++ because classes and ineritance makes it more complex, it's
                          > because it's more powerful and easier to maintain. I know a good
                          > FORTRAN programmer can write FORTRAN code in any language - but do you
                          > really want to?[/color]

                          Possibly not. So, I think I'm writing C code in C++...
                          [color=blue]
                          > C++ is _not_ C(++)
                          >
                          > it is rather:
                          >
                          > class Cpp : public C
                          > {
                          > public:
                          > virtual Cpp& operator++() {return C::data++;}
                          > };[/color]

                          Better read that book pronto, since that & at the end of Cpp for the ++
                          operator dazzles me :)

                          --
                          Sensei <senseiwa@mac.c om>

                          The optimist thinks this is the best of all possible worlds.
                          The pessimist fears it is true. [J. Robert Oppenheimer]

                          Comment

                          • Ben Pope

                            #14
                            Re: Map-like container?

                            Sensei wrote:[color=blue]
                            > On 2006-03-14 17:54:50 +0100, "Gernot Frisch" <Me@Privacy.net > said:
                            >[color=green]
                            >> C++ is _not_ C(++)
                            >>
                            >> it is rather:
                            >>
                            >> class Cpp : public C
                            >> {
                            >> public:
                            >> virtual Cpp& operator++() {return C::data++;}
                            >> };[/color]
                            >
                            > Better read that book pronto, since that & at the end of Cpp for the ++
                            > operator dazzles me :)[/color]

                            It's a reference. A bit like a pointer, but different.

                            Ben Pope
                            --
                            I'm not just a number. To many, I'm known as a string...

                            Comment

                            • Victor Bazarov

                              #15
                              Re: Map-like container?

                              Ben Pope wrote:[color=blue]
                              > Sensei wrote:[color=green]
                              >> [..]
                              >> Better read that book pronto, since that & at the end of Cpp for the
                              >> ++ operator dazzles me :)[/color]
                              >
                              >
                              > It's a reference. A bit like a pointer, but different.[/color]

                              Heh... "It's like a pointer, only it's not."

                              Comment

                              Working...