STL Container

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lourk
    New Member
    • Aug 2007
    • 7

    STL Container

    Hello Everybody!

    I am a newbie in c++. I want to create a simple application that insert, search and displays data. And I am planning to use an STL Container.

    What kind of STL Container am I going to use that stores 3 different data types?
    example: int, bool, double

    Thank you
  • afraze
    New Member
    • Aug 2007
    • 18

    #2
    lourk,

    you should look over url below.



    After that if you have any question, we can help u..

    Comment

    • Darryl
      New Member
      • May 2007
      • 86

      #3
      There are no Standard containers that allow multiple types. Try Boost or try using a variant or as a last resort a union

      Comment

      • JonLT
        New Member
        • Jul 2007
        • 41

        #4
        or you could just use class with the three types:
        [code=cpp]
        class myClass
        {
        public:
        int myInt;
        bool myBool;
        double myDouble;
        };
        [/code]
        and then use this class with a vector or the like:
        [code=cpp]
        std::vector<myC lass> myVector;
        [/code]
        or store pointers in the vector
        [code=cpp]
        std::vector<myC lass*> myVector;
        [/code]

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by lourk
          I want to create a simple application that insert, search and displays data. And I am planning to use an STL Container.
          If this made sense, the STL would have a container for it. However, with multiple types (like bool, int and double) you cannot do inserts as there is no way to compare a bool to a double or a double to an int. Comparison is also involved in doing a search.

          You might not even be able to display if you allow objects of multiple classes.

          I think your data model is incorrect.

          What is it you are trying to do??

          Comment

          • lourk
            New Member
            • Aug 2007
            • 7

            #6
            Thanks for the replies.

            I have use the STL Container Map that stores 2 values and it works.

            Below you'll find my code:

            class ContainerValues
            {
            public:
            ContainerValues (void);
            ~ContainerValue s(void);

            void Update(long dwHandle, double dValue);
            bool Remove(long Handle);
            double Sum();
            void Display();
            private:
            map<long, double>m_Map;
            typedef map<long, double>::iterat or IT;
            };

            void ContainerValues ::Update(long dwHandle, double dValue)
            {
            Remove(dwHandle );
            m_Map.insert(pa ir<long, double>(dwHandl e, dValue));
            } // end ContainerValues ::Update

            But I need to make it 3 values.
            like: map<long, double,double>m _Map;

            Then instead of pair
            m_Map.insert(pa ir<long, double>(dwHandl e, dValue));

            what am I going to use since I need to make it 3 values?

            Thanks

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by lourk
              map<long, double>m_Map;
              This does not store two values.

              It stores one value. A pair.

              In a map, the pair is a key/value object. That is, you find the double (a value) by asking for the long (a key).

              My Post #5 still holds.

              Comment

              • Darryl
                New Member
                • May 2007
                • 86

                #8
                Ok here is a union solution

                [CODE=cpp]
                #include <iostream>
                #include <vector>

                class multitype
                {
                friend std::ostream& operator<<(std: :ostream& os, const multitype& m);

                public:
                multitype()
                {
                value.i = 0;
                type = INT;
                };
                multitype(bool in)
                {
                value.b = in;
                type = BOOL;
                }
                multitype(int in)
                {
                value.i = in;
                type = INT;
                }
                multitype(doubl e in)
                {
                value.d = in;
                type = DOUBLE;
                }

                enum TYPE {BOOL, INT, DOUBLE};
                union VALUE
                {
                bool b;
                int i;
                double d;
                };


                private:

                VALUE value;
                TYPE type;
                };

                std::ostream& operator << (std::ostream& os, const multitype& m)
                {
                switch(m.type)
                {
                case multitype::BOOL :
                return os << m.value.b;
                //return os << (m.value.b ? "true" : "false");
                case multitype::INT:
                return os << m.value.i;
                case multitype::DOUB LE:
                return os << m.value.d;
                }
                }


                int main()
                {
                std::vector<mul titype> foo;

                foo.push_back(2 0);
                foo.push_back(t rue);
                foo.push_back(3 .14);

                std::cout << foo[0] << "\n" << foo[1] << "\n" << foo[2] << "\n";

                }
                [/CODE]

                Missing:

                Test what type is being held
                Assignment to the various types

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by Darryl
                  enum TYPE {BOOL, INT, DOUBLE};
                  union VALUE
                  {
                  bool b;
                  int i;
                  double d;
                  };


                  private:

                  VALUE value;
                  TYPE type;
                  You have just recoded the Microsoft VARIANT. The code for this on an MSDN web page so just copy if off.

                  Again, I say, you cannot do much with containers of multi-types, like VARIANT, since the elements are incompatible with each other. You can't compare elements so you can't sort them or use a tree to access them by key, or sort, or do any ordering or assignment whatsoever.

                  The VARIANT was invented for Visual Basic to communicate with C functions and not for containers of VARIANT.

                  Discriminated unions like this are a C solution for templates.

                  The OP has not given the reason for needing to store 3 types in one element of a container. I expect 3 containers are what's needed.

                  Comment

                  • Darryl
                    New Member
                    • May 2007
                    • 86

                    #10
                    I could think of a use, like a table or spreadsheet where different cells need different types of values yet a central storage.

                    btw, The MS VARIANT is probably more encompassing than my implementation, I have no provision for any kind of a string.

                    Comment

                    • lourk
                      New Member
                      • Aug 2007
                      • 7

                      #11
                      I have already tried the #4 and it is now working.

                      Thanks to everyone for all the help.

                      Comment

                      Working...