471,066 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Vector of STL maps versus Vector of objects

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 '05 #1
2 7325
am*****@gmail.com 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
Jul 26 '05 #2
Ian
am*****@gmail.com 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) ?

Jul 26 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Bo Peng | last post: by
7 posts views Thread by deancoo | last post: by
1 post views Thread by juvi | last post: by
9 posts views Thread by kathy | last post: by
9 posts views Thread by Jeff | last post: by
13 posts views Thread by zaineb | last post: by
16 posts views Thread by xyz | last post: by
10 posts views Thread by oktayarslan | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.