8

I want to create a vector of elements representing a certain structure.

The thing is that I don't know how many elements the structure will have, since the number will change very often, and I don't really know how to create a vector.

How to make that?

In order to make it more clear:

I saw that when creating a vector, you do something like this:

std::vector<structureType> vectorName(nrOfElements);

I don't know the number of elements and what to write there, between brackets.

user3666197
  • 1
  • 6
  • 50
  • 92
user2399378
  • 829
  • 2
  • 10
  • 23
  • 1
    The whole point of `std::vector` is you don't need to know the size. Just skip the brackets altogether, then add to the vector using [`myVector.push_back();`](http://en.cppreference.com/w/cpp/container/vector/push_back). It will automatically expand its internal memory as necessary. You get the current size with [`myVector.size();`](http://en.cppreference.com/w/cpp/container/vector/size). – BoBTFish May 30 '13 at 13:31
  • 1
    possible duplicate of [Allocate memory for a vector](http://stackoverflow.com/questions/4427738/allocate-memory-for-a-vector) –  May 30 '13 at 13:37

3 Answers3

14

If you default construct the vector, you get an empty one:

std::vector<structureType> vectorName; // holds 0 elements

then you can push elements into the vector, increasing its size (see also other vector modifiers):

vectorName.push_back(someStructureTypeInstance);

This might suit your needs. If you are worried about future memory re-allocations, you can use std::vector::reserve after constructing the vector.

std::vector<structureType> vectorName; // holds 0 elements
vectorName.reserve(100); // still 0 elements, but capacity for 100
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • If I do what you suggested, menaing reserving space, what happens if for example I reserve space for 101 elements and then I add the 102th element? Also, In my program, I allocate some memory dynamically. If I choose to declare the vector like this: std::vector vectorName; , and then just add elements to it, is there any chance that this might overlap the manually allocated memory? – user2399378 May 30 '13 at 13:43
  • 4
    @user2399378 what happens when you insert the 102nd element is that the vector has to allocate a new chunk of memory that is large enough for 102 elements (usually it assigns around 2 times the required size), then copy or move all its elements from the original 101-sized chunk of memory to the new one, then de-allocate the original. These are the re-allocations I was talking about. You don't have to worry about memory management, but reallocations can be costly, so if you know how large the vector is going to be, you can avoid them by reserving the right amount. – juanchopanza May 30 '13 at 13:46
1

I don''t know what to write there between brackets

Write nothing )) In this case you'll create an empty vector, wich could be grown with std::vector::push_back()

Update: Do not forget to remove empty () to avoid vexing parse

borisbn
  • 4,988
  • 25
  • 42
  • 1
    [If you write nothing between the brackets, you hit the most vexing parse](https://ideone.com/B5sBce). Omit the brackets altogether (or use the curly brace `{}` uniform initialization syntax if you are using `c++11`). – BoBTFish May 30 '13 at 13:35
  • 1
    Actually, you will declare a function like that. So you need to omit the `()` too, or use `{}` if you have C++11. – juanchopanza May 30 '13 at 13:35
  • @BoBTFish yes, I know about vexing parse. And yes, I forgot to say to omit `()`. Thank you for comment. I'll update. – borisbn May 31 '13 at 06:07
1

You can change the number of elements the vector contains, by inserting and/or removing elements. You are specifically looking for vector's methods insert, push_back/emplace_back, resize, pop_back, erase.

You'll find descriptions of the methods in any C++ reference (e.g. have a look here in the "Modifiers" section) and in the C++ beginner's book of your choice.

Arne Mertz
  • 24,171
  • 3
  • 51
  • 90