By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,120 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,120 IT Pros & Developers. It's quick & easy.

Vector of STL maps versus Vector of objects

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.