Vector of STL maps versus Vector of objects

A

amolpan

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) ?
 
A

Alan Johnson

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
 
I

Ian

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top