Diwa wrote:
I know I canreservespace for100stringsin avectorby doing
following:
std::vector<std::string> m_vec;
m_vec.reserve(100)
But I also know that each of the string will be exactly 20 chars.
Can I use this fact while reserving thevectorspace above?
If you know that all thestringswill be exactly 20 chars, and
especially if you know that will not change, you can use a char array
instead. Of course arrays cannot be used directly with std::vector, but
they can if you enclose them in a struct, like this:
struct My20CharString
{
char str[21];
My20CharString(): str() {}
};
std::vector<My20CharString> m_vec;
m_vec.reserve(100);
If you don't like having to write "m_vec[i].str" instead of just
"m_vec[i]", then you could do this:
struct My20CharString
{
char str[21];
My20CharString(): str() {}
operator const char*() const { return str; }
operator char*() { return str; }
};- Hide quoted text -
- Show quoted text -
Thanks Juha. Seems like I will go this route of a struct containing