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