Generic x-dimensional name values

Discussion in 'C++' started by Christopher, Jul 24, 2008.

  1. Christopher

    Christopher Guest

    Let's see if I can explain my goal....

    I need to devise a data structure that can contain a name and a data
    object of any type (at least built in types)

    For example:
    name - "Car1" value - "WGNZYH"
    name - "Latitude" value - 80.1

    I created an class that represents both members as a std::string and
    use boost's lexiconal cast along with template params for this class
    and it works great for 1 dimension. My co-worker seems to think using
    strings is inefficient memory wise, but I don't see an alternative, at
    least not without tracking types and using unions, which I do not want
    to do. We'll worry about efficiency later...

    My problem is on how to represent multidimensional constructs of this
    object where the dimension could be any number. At first, we assumed
    we would only have 3 at most, but it doesn't seem very robust to me.

    I went and made another class that wraps a map of the first. We'll
    call that MyObject2D.
    I went and made another class that wraps a map of the second. We'll
    call that MyObject3D.
    You see the problem..


    For explanation, here is some sample data:

    "car", "WGNYTH"
    "wheel", "0901929AB"
    "size", 18
    "tread", 2
    "psi", 35.001
    "color", "black"

    etc.

    I want the user to be able to look up and retrieve values, groups of
    values, or groups of groups, etc, by name.
    The user should already know what types to expect.

    usage like:
    MyObject2D wheel = MyObject3D.Get2D("wheel");

    int size;
    wheel.GetMyObject1D(size);

    I don't like having a seperate class for every dimension though. Any
    Ideas on how to do something like this another way?
     
    Christopher, Jul 24, 2008
    #1
    1. Advertising

  2. Christopher schrieb:
    > Let's see if I can explain my goal....
    >
    > I need to devise a data structure that can contain a name and a data
    > object of any type (at least built in types)
    >
    > For example:
    > name - "Car1" value - "WGNZYH"
    > name - "Latitude" value - 80.1
    >

    [...]
    > My problem is on how to represent multidimensional constructs of this
    > object where the dimension could be any number. At first, we assumed
    > we would only have 3 at most, but it doesn't seem very robust to me.
    >
    > I went and made another class that wraps a map of the first. We'll
    > call that MyObject2D.
    > I went and made another class that wraps a map of the second. We'll
    > call that MyObject3D.
    > You see the problem..


    Do you want a multidimensional data structure, or a hierarchical /
    recursive data structure?

    For the latter, you could make a std::map, where the value type is a
    boost::variant (google for it), which can be recursive.

    --
    Thomas
     
    Thomas J. Gritzan, Jul 24, 2008
    #2
    1. Advertising

  3. Christopher

    Christopher Guest

    On Jul 24, 1:43 pm, Victor Bazarov <> wrote:
    > Christopher wrote:
    > > Let's see if I can explain my goal....

    >
    > > I need to devise a data structure that can contain a name and a data
    > > object of any type (at least built in types)
    > > [..]

    >
    > Like std::map<std::string, boost::any> ?
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Almost, except
    "Discriminated types that contain values of different types but do not
    attempt conversion between them"

    I want to be able to convert from any built in type to any other built
    in type during sets and gets.
    I have that part down though. Storing as strings and using lexical
    casting seems to work pretty well.
    Its the grouping part I can't wrap my head around.

    I need name value pairs
    I need named groups of name value pairs
    I need named groups, of named groups, of name value pairs
    and on and on.
     
    Christopher, Jul 24, 2008
    #3
    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. Alf P. Steinbach
    Replies:
    0
    Views:
    455
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,958
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    689
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    1,019
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    229
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page