Vector of STL maps versus Vector of objects

Discussion in 'C++' started by amolpan@gmail.com, Jul 26, 2005.

  1. Guest

    I am using a vector of maps to store a sequence of key-value pairs. I
    fear usage of an STL map would lead to serious memory bottlenecks.
    Instead I was thinking of using an object to store key-value mappings.

    class StringPair
    {
    char *key;
    char *value;
    };


    I can now store objects of class StringPair rather than maps in the
    vector. Is this a feasible option from performance considerations (i
    mean memory usage & efficiency of inserts,updates & deletes) ?
     
    , Jul 26, 2005
    #1
    1. Advertising

  2. Alan Johnson Guest

    wrote:
    > I am using a vector of maps to store a sequence of key-value pairs. I
    > fear usage of an STL map would lead to serious memory bottlenecks.
    > Instead I was thinking of using an object to store key-value mappings.
    >
    > class StringPair
    > {
    > char *key;
    > char *value;
    > };
    >
    >
    > I can now store objects of class StringPair rather than maps in the
    > vector. Is this a feasible option from performance considerations (i
    > mean memory usage & efficiency of inserts,updates & deletes) ?
    >


    Certainly using a map just to store a single key/value pair is not what
    you want to be doing. I would suggest a vector of std::pair (which is
    the value_type of map, by the way).

    Your "StringPair" class may have a problem, depending on how exactly you
    intend to use it. Who allocates the strings you are storing? Who frees
    that memory? Maybe you have all that worked out already. If not, save
    yourself the headache and use std::string. Example:

    #include <vector>
    #include <string>
    #include <utility>
    std::vector< std::pair<std::string, std::string> > v ;

    // ...

    v.push_back(std::make_pair("key1", "value1")) ;
    v.push_back(std::make_pair("key2", "value2")) ;


    -Alan
     
    Alan Johnson, Jul 26, 2005
    #2
    1. Advertising

  3. Ian Guest

    wrote:
    > I am using a vector of maps to store a sequence of key-value pairs. I
    > fear usage of an STL map would lead to serious memory bottlenecks.
    > Instead I was thinking of using an object to store key-value mappings.
    >

    Why? Try it and see, don't optimise before you have to.

    Ian

    > class StringPair
    > {
    > char *key;
    > char *value;
    > };
    >
    >
    > I can now store objects of class StringPair rather than maps in the
    > vector. Is this a feasible option from performance considerations (i
    > mean memory usage & efficiency of inserts,updates & deletes) ?
    >
     
    Ian, Jul 26, 2005
    #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. Simon Elliott
    Replies:
    4
    Views:
    1,192
    Simon Elliott
    Mar 10, 2005
  2. Marcus
    Replies:
    2
    Views:
    622
    Marcus
    Dec 9, 2005
  3. Replies:
    8
    Views:
    2,005
    Csaba
    Feb 18, 2006
  4. Replies:
    2
    Views:
    414
    Juha Nieminen
    Sep 8, 2008
  5. Paul Butcher
    Replies:
    12
    Views:
    786
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page