-1

I've already have some problems with std::vector inside std::vector if the internal vectors are redimensionned. I would like to know if it is perfectly secure to have a std::vector<std::string> (because the internal strings can be redimensionned) or std::vector<std::string*> is better ?

Vincent
  • 57,703
  • 61
  • 205
  • 388
  • 4
    What problems? There are no intrinsic problems with redimensioning the elements of vector, be them vectors or strings. – R. Martinho Fernandes Sep 06 '12 at 11:38
  • And is it ok to have a std::vector if MyClass contains std::string ? – Vincent Sep 06 '12 at 11:41
  • 1
    Yes it is perfectly safe, as is having a vector of vectors. – juanchopanza Sep 06 '12 at 11:42
  • 2
    What are these problems you've been having? From what you've been telling us you should not have been having any problems at all. So there's not much to answer here at the moment. – Bart Sep 06 '12 at 11:49
  • It is an old program of mine, in which I had something like std::vector with MyClass containing std::vector, and when vectors inside MyClass were redimensionned, the global vector was not redimensionned (and a memory problem occured). I don't remember exactly and maybe the structure that produced the bug was more complicated. – Vincent Sep 06 '12 at 12:04
  • @Vincent: if you don't remember exactly why it was a problem before, I don't think it's possible to say whether it will be a problem now. Certainly `string` can be resized, just like `vector`. In both cases, there is no need to resize the "outer" vector just because individual elements of it are resized. – Steve Jessop Sep 06 '12 at 12:06
  • 1
    You are being prematurely worried because of an old bug that you didn't fully grasp the cause of. It would have been better if you had drilled down into the old bug to find the actual cause (because the redimensioning of the vectors was not it, I guarantee you), then you wouldn't have been needlessly worried about this :) – Joris Timmermans Sep 06 '12 at 12:07

2 Answers2

3

This question is most likely consequence of the misconception that looks vector the same as arrays.

It is true that an array must contain elements of equal type and static size, as it is true that vector can be resized, but the static sizeof(vector<X>) does not depend on the run-time size of it. vector<vector<X> > contains internally just the pointer (plus some other descriptive data) to the dynamically allocated array of vector<X> each of which in turn contains just the pointer to its own dynamically allocated array of X.

The arrays are in fact made by the same elements, of the same size.

j_random_hacker
  • 50,331
  • 10
  • 105
  • 169
Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
1

It is perfectly safe to place in a vector any type that meets the Standard type requirements. These requirements are strictly specified on the interface, and basically sum up as "Movable". As std::string is movable, it can be placed in a vector. The fact that it might dynamically allocate other memory is irrelevant.

Puppy
  • 144,682
  • 38
  • 256
  • 465