Say I need to store long strings in my vector. I can declare it as vector. However the better approach would be to store pointers to those strings and not the actual strings.
So should my declaration be something like vector & simply I do something like:
LOOP
//Accept String from a file in myString
vector<string *> v1;
string * sample = &myString;
v1.push_back(sample);
END LOOP
Is this appropriate or their exists a better way to achieve this? I may even be wrong completely out here.
There is no reason to store pointers to strings.
std::stringwill use heap allocated memory for “long” strings anyway, so the only thing to gain by storing pointers in your container is the headache of managing the memory yourself.On the other hand, if you need copies of those long strings in multiple locations, you could keep shared pointers to the strings in the vector: