I need a simple class to wrap a c style array of pointer strings, namely char **
I need minimal operations on my wrapper including adding elements( const or non const ), iterating, random access and querying size. I started using a std::vector like below but I don't like it for reasons described below
This is my start
struct CstrVector
{
CstrVector(){}
char *convertToC(const std::string & str)
{
char *c = new char[str.size()+1];
std::strcpy(c, str.c_str());
return c;
}
void add( const char* c)
{
//??
}
void add(const std::string& str )
{
data.push_back(convertToC(str));
}
void add(const std::string& s, unsigned int pos )
{
if( pos < data.size())
{
data[pos] = convertToC(s);
}
}
~CstrVector()
{
for ( size_t i = 0 ; i < data.size() ; i++ )
{
delete [] data[i];
}
}
char ** vdata;
std::vector<char*> data; // I want to replace this with char ** vdata
};
Why not use
std::vector<char*>
I don't see a benefit it since I still have to allocate and free the memory outside the container ( std::vector ) anyway
Why not use
std::vector<std:string>
because I want to be able to easily pass the array to a C function that will manipulate / change it contents.
std::vector? I can see one: you don't have to write it.adding elements( const or non const )but why would you add on aconst?